- Fix Slowloris attacks on HTTP servers - Force set default of 5 minutes for pprof read timeout - Change `ShutdownTimeout` to time.Duration since it cannot be set to 0
43 lines
1008 B
Go
43 lines
1008 B
Go
package healthcheck
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func (s *Server) Run(ctx context.Context, done chan<- struct{}) {
|
|
defer close(done)
|
|
|
|
loopDone := make(chan struct{})
|
|
go s.runHealthcheckLoop(ctx, loopDone)
|
|
|
|
server := http.Server{
|
|
Addr: s.config.ServerAddress,
|
|
Handler: s.handler,
|
|
ReadHeaderTimeout: s.config.ReadHeaderTimeout,
|
|
ReadTimeout: s.config.ReadTimeout,
|
|
}
|
|
serverDone := make(chan struct{})
|
|
go func() {
|
|
defer close(serverDone)
|
|
<-ctx.Done()
|
|
const shutdownGraceDuration = 2 * time.Second
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownGraceDuration)
|
|
defer cancel()
|
|
if err := server.Shutdown(shutdownCtx); err != nil {
|
|
s.logger.Error("failed shutting down: " + err.Error())
|
|
}
|
|
}()
|
|
|
|
s.logger.Info("listening on " + s.config.ServerAddress)
|
|
err := server.ListenAndServe()
|
|
if err != nil && !errors.Is(ctx.Err(), context.Canceled) {
|
|
s.logger.Error(err.Error())
|
|
}
|
|
|
|
<-loopDone
|
|
<-serverDone
|
|
}
|