Support systemd socket-activation
Co-authored-by: Michael <michael.matur@gmail.com>
This commit is contained in:
parent
9e0800f938
commit
b7de043991
6 changed files with 91 additions and 20 deletions
|
@ -48,8 +48,15 @@ const (
|
|||
var (
|
||||
clientConnectionStates = map[string]*connState{}
|
||||
clientConnectionStatesMu = sync.RWMutex{}
|
||||
|
||||
socketActivationListeners map[string]net.Listener
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Populates pre-defined socketActivationListeners by socket activation.
|
||||
populateSocketActivationListeners()
|
||||
}
|
||||
|
||||
type connState struct {
|
||||
State string
|
||||
KeepAliveState string
|
||||
|
@ -96,6 +103,7 @@ func NewTCPEntryPoints(entryPointsConfig static.EntryPoints, hostResolverConfig
|
|||
return clientConnectionStates
|
||||
}))
|
||||
}
|
||||
|
||||
serverEntryPointsTCP := make(TCPEntryPoints)
|
||||
for entryPointName, config := range entryPointsConfig {
|
||||
protocol, err := config.GetProtocol()
|
||||
|
@ -113,7 +121,7 @@ func NewTCPEntryPoints(entryPointsConfig static.EntryPoints, hostResolverConfig
|
|||
OpenConnectionsGauge().
|
||||
With("entrypoint", entryPointName, "protocol", "TCP")
|
||||
|
||||
serverEntryPointsTCP[entryPointName], err = NewTCPEntryPoint(ctx, config, hostResolverConfig, openConnectionsGauge)
|
||||
serverEntryPointsTCP[entryPointName], err = NewTCPEntryPoint(ctx, entryPointName, config, hostResolverConfig, openConnectionsGauge)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error while building entryPoint %s: %w", entryPointName, err)
|
||||
}
|
||||
|
@ -169,10 +177,10 @@ type TCPEntryPoint struct {
|
|||
}
|
||||
|
||||
// NewTCPEntryPoint creates a new TCPEntryPoint.
|
||||
func NewTCPEntryPoint(ctx context.Context, configuration *static.EntryPoint, hostResolverConfig *types.HostResolverConfig, openConnectionsGauge gokitmetrics.Gauge) (*TCPEntryPoint, error) {
|
||||
func NewTCPEntryPoint(ctx context.Context, name string, config *static.EntryPoint, hostResolverConfig *types.HostResolverConfig, openConnectionsGauge gokitmetrics.Gauge) (*TCPEntryPoint, error) {
|
||||
tracker := newConnectionTracker(openConnectionsGauge)
|
||||
|
||||
listener, err := buildListener(ctx, configuration)
|
||||
listener, err := buildListener(ctx, name, config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error preparing server: %w", err)
|
||||
}
|
||||
|
@ -181,19 +189,19 @@ func NewTCPEntryPoint(ctx context.Context, configuration *static.EntryPoint, hos
|
|||
|
||||
reqDecorator := requestdecorator.New(hostResolverConfig)
|
||||
|
||||
httpServer, err := createHTTPServer(ctx, listener, configuration, true, reqDecorator)
|
||||
httpServer, err := createHTTPServer(ctx, listener, config, true, reqDecorator)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error preparing http server: %w", err)
|
||||
}
|
||||
|
||||
rt.SetHTTPForwarder(httpServer.Forwarder)
|
||||
|
||||
httpsServer, err := createHTTPServer(ctx, listener, configuration, false, reqDecorator)
|
||||
httpsServer, err := createHTTPServer(ctx, listener, config, false, reqDecorator)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error preparing https server: %w", err)
|
||||
}
|
||||
|
||||
h3Server, err := newHTTP3Server(ctx, configuration, httpsServer)
|
||||
h3Server, err := newHTTP3Server(ctx, config, httpsServer)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error preparing http3 server: %w", err)
|
||||
}
|
||||
|
@ -206,7 +214,7 @@ func NewTCPEntryPoint(ctx context.Context, configuration *static.EntryPoint, hos
|
|||
return &TCPEntryPoint{
|
||||
listener: listener,
|
||||
switcher: tcpSwitcher,
|
||||
transportConfiguration: configuration.Transport,
|
||||
transportConfiguration: config.Transport,
|
||||
tracker: tracker,
|
||||
httpServer: httpServer,
|
||||
httpsServer: httpsServer,
|
||||
|
@ -460,17 +468,29 @@ func buildProxyProtocolListener(ctx context.Context, entryPoint *static.EntryPoi
|
|||
return proxyListener, nil
|
||||
}
|
||||
|
||||
func buildListener(ctx context.Context, entryPoint *static.EntryPoint) (net.Listener, error) {
|
||||
listenConfig := newListenConfig(entryPoint)
|
||||
listener, err := listenConfig.Listen(ctx, "tcp", entryPoint.GetAddress())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening listener: %w", err)
|
||||
func buildListener(ctx context.Context, name string, config *static.EntryPoint) (net.Listener, error) {
|
||||
var listener net.Listener
|
||||
var err error
|
||||
|
||||
// if we have predefined listener from socket activation
|
||||
if ln, ok := socketActivationListeners[name]; ok {
|
||||
listener = ln
|
||||
} else {
|
||||
if len(socketActivationListeners) > 0 {
|
||||
log.Warn().Str("name", name).Msg("Unable to find socket activation listener for entryPoint")
|
||||
}
|
||||
|
||||
listenConfig := newListenConfig(config)
|
||||
listener, err = listenConfig.Listen(ctx, "tcp", config.GetAddress())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening listener: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
listener = tcpKeepAliveListener{listener.(*net.TCPListener)}
|
||||
|
||||
if entryPoint.ProxyProtocol != nil {
|
||||
listener, err = buildProxyProtocolListener(ctx, entryPoint, listener)
|
||||
if config.ProxyProtocol != nil {
|
||||
listener, err = buildProxyProtocolListener(ctx, config, listener)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating proxy protocol listener: %w", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue