ServeTCP (tcp/switcher.go)

traefik · pkg/tcp/switcher.go · go · MIT
infra, tag:knn-classified

TCP handler switcher forwarding connections to active handler.

// Source: https://github.com/traefik/traefik | License: MIT | Copyright 2016-2020 Containous SAS; 2020-2025 Traefik Labs
// Provided via justclaw.it.com; keep this notice with the code.
package tcp

import (
	"github.com/traefik/traefik/v3/pkg/safe"
)

// HandlerSwitcher is a TCP handler switcher.
type HandlerSwitcher struct {
	router safe.Safe
}

// ServeTCP forwards the TCP connection to the current active handler.
func (s *HandlerSwitcher) ServeTCP(conn WriteCloser) {
	handler := s.router.Get()
	h, ok := handler.(Handler)
	if ok {
		h.ServeTCP(conn)
	} else {
		conn.Close()
	}
}

// Switch sets the new TCP handler to use for new connections.
func (s *HandlerSwitcher) Switch(handler Handler) {
	s.router.Set(handler)
}

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key