-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple endpoints #880
Multiple endpoints #880
Changes from all commits
12c7d5d
7bc02be
8ba494d
f2c7bfe
c7c5042
505380f
63888ba
c1daf50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,83 +39,102 @@ func diagConn(c net.Conn, s http.ConnState) { | |
} | ||
|
||
func runServer(ctx context.Context, router http.Handler, cfg *config.Server) error { | ||
addr := cfg.BindAddress() | ||
listeners := cfg.BindEndpoints() | ||
rdto := cfg.Timeouts.Read | ||
wrto := cfg.Timeouts.Write | ||
idle := cfg.Timeouts.Idle | ||
rdhr := cfg.Timeouts.ReadHeader | ||
mhbz := cfg.Limits.MaxHeaderByteSize | ||
bctx := func(net.Listener) context.Context { return ctx } | ||
|
||
log.Info(). | ||
Str("bind", addr). | ||
Dur("rdTimeout", rdto). | ||
Dur("wrTimeout", wrto). | ||
Msg("server listening") | ||
|
||
server := http.Server{ | ||
Addr: addr, | ||
ReadTimeout: rdto, | ||
WriteTimeout: wrto, | ||
IdleTimeout: idle, | ||
ReadHeaderTimeout: rdhr, | ||
Handler: router, | ||
BaseContext: bctx, | ||
ConnState: diagConn, | ||
MaxHeaderBytes: mhbz, | ||
ErrorLog: errLogger(), | ||
} | ||
errChan := make(chan error) | ||
cancelCtx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
forceCh := make(chan struct{}) | ||
defer close(forceCh) | ||
|
||
// handler to close server | ||
go func() { | ||
select { | ||
case <-ctx.Done(): | ||
log.Debug().Msg("force server close on ctx.Done()") | ||
server.Close() | ||
case <-forceCh: | ||
log.Debug().Msg("go routine forced closed on exit") | ||
for _, addr := range listeners { | ||
log.Info(). | ||
Str("bind", addr). | ||
Dur("rdTimeout", rdto). | ||
Dur("wrTimeout", wrto). | ||
Msg("server listening") | ||
|
||
server := http.Server{ | ||
Addr: addr, | ||
ReadTimeout: rdto, | ||
WriteTimeout: wrto, | ||
IdleTimeout: idle, | ||
ReadHeaderTimeout: rdhr, | ||
Handler: router, | ||
BaseContext: bctx, | ||
ConnState: diagConn, | ||
MaxHeaderBytes: mhbz, | ||
ErrorLog: errLogger(), | ||
} | ||
}() | ||
|
||
var listenCfg net.ListenConfig | ||
|
||
ln, err := listenCfg.Listen(ctx, "tcp", addr) | ||
if err != nil { | ||
return err | ||
} | ||
forceCh := make(chan struct{}) | ||
defer close(forceCh) | ||
|
||
// Bind the deferred Close() to the stack variable to handle case where 'ln' is wrapped | ||
defer func() { ln.Close() }() | ||
// handler to close server | ||
go func() { | ||
select { | ||
case <-ctx.Done(): | ||
log.Debug().Msg("force server close on ctx.Done()") | ||
server.Close() | ||
case <-forceCh: | ||
log.Debug().Msg("go routine forced closed on exit") | ||
} | ||
}() | ||
|
||
// Conn Limiter must be before the TLS handshake in the stack; | ||
// The server should not eat the cost of the handshake if there | ||
// is no capacity to service the connection. | ||
// Also, it appears the HTTP2 implementation depends on the tls.Listener | ||
// being at the top of the stack. | ||
ln = wrapConnLimitter(ctx, ln, cfg) | ||
var listenCfg net.ListenConfig | ||
|
||
if cfg.TLS != nil && cfg.TLS.IsEnabled() { | ||
commonTlsCfg, err := tlscommon.LoadTLSServerConfig(cfg.TLS) | ||
ln, err := listenCfg.Listen(ctx, "tcp", addr) | ||
if err != nil { | ||
return err | ||
} | ||
server.TLSConfig = commonTlsCfg.ToConfig() | ||
|
||
// Must enable http/2 in the configuration explicitly. | ||
// (see https://golang.org/pkg/net/http/#Server.Serve) | ||
server.TLSConfig.NextProtos = []string{"h2", "http/1.1"} | ||
// Bind the deferred Close() to the stack variable to handle case where 'ln' is wrapped | ||
defer func() { ln.Close() }() | ||
|
||
ln = tls.NewListener(ln, server.TLSConfig) | ||
// Conn Limiter must be before the TLS handshake in the stack; | ||
// The server should not eat the cost of the handshake if there | ||
// is no capacity to service the connection. | ||
// Also, it appears the HTTP2 implementation depends on the tls.Listener | ||
// being at the top of the stack. | ||
ln = wrapConnLimitter(ctx, ln, cfg) | ||
|
||
if cfg.TLS != nil && cfg.TLS.IsEnabled() { | ||
commonTlsCfg, err := tlscommon.LoadTLSServerConfig(cfg.TLS) | ||
if err != nil { | ||
return err | ||
} | ||
server.TLSConfig = commonTlsCfg.ToConfig() | ||
|
||
// Must enable http/2 in the configuration explicitly. | ||
// (see https://golang.org/pkg/net/http/#Server.Serve) | ||
server.TLSConfig.NextProtos = []string{"h2", "http/1.1"} | ||
|
||
ln = tls.NewListener(ln, server.TLSConfig) | ||
|
||
} else { | ||
log.Warn().Msg("Exposed over insecure HTTP; enablement of TLS is strongly recommended") | ||
} | ||
|
||
log.Debug().Msgf("Listening on %s", addr) | ||
|
||
go func(ctx context.Context, errChan chan error, ln net.Listener) { | ||
if err := server.Serve(ln); err != nil && err != http.ErrServerClosed { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we do direct a direct comparison of the error ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you mean replacing |
||
errChan <- err | ||
} | ||
}(cancelCtx, errChan, ln) | ||
|
||
} else { | ||
log.Warn().Msg("exposed over insecure HTTP; enablement of TLS is strongly recommended") | ||
} | ||
|
||
if err := server.Serve(ln); err != nil && err != http.ErrServerClosed { | ||
return err | ||
select { | ||
case err := <-errChan: | ||
if err != context.Canceled { | ||
return err | ||
} | ||
case <-cancelCtx.Done(): | ||
} | ||
|
||
return nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused what the purpose of
forceCh
and this message are, does this occur when the server closes with a noncontext.Cancelled
error?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i'm not really sure why this is here. i understand it closes goroutine with main func but not sure why this was a problem and why this was not addressed with cancel contexts.
not touching this atm