Add GraceTimeOut support
Signed-off-by: Emile Vauge <emile@vauge.com>
This commit is contained in:
parent
516608d883
commit
d1b5cf99d0
1 changed files with 20 additions and 2 deletions
22
server.go
22
server.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"golang.org/x/net/context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
@ -97,20 +98,37 @@ func (server *Server) Start() {
|
||||||
|
|
||||||
// Stop stops the server
|
// Stop stops the server
|
||||||
func (server *Server) Stop() {
|
func (server *Server) Stop() {
|
||||||
for _, serverEntryPoint := range server.serverEntryPoints {
|
for serverEntryPointName, serverEntryPoint := range server.serverEntryPoints {
|
||||||
serverEntryPoint.httpServer.BlockingClose()
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(server.globalConfiguration.GraceTimeOut)*time.Second)
|
||||||
|
go func() {
|
||||||
|
log.Debugf("Waiting %d seconds before killing connections on entrypoint %s...", 30, serverEntryPointName)
|
||||||
|
serverEntryPoint.httpServer.BlockingClose()
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
<-ctx.Done()
|
||||||
}
|
}
|
||||||
server.stopChan <- true
|
server.stopChan <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close destroys the server
|
// Close destroys the server
|
||||||
func (server *Server) Close() {
|
func (server *Server) Close() {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(server.globalConfiguration.GraceTimeOut)*time.Second)
|
||||||
|
go func(ctx context.Context) {
|
||||||
|
<-ctx.Done()
|
||||||
|
if ctx.Err() == context.Canceled {
|
||||||
|
return
|
||||||
|
} else if ctx.Err() == context.DeadlineExceeded {
|
||||||
|
log.Debugf("I love you all :'( ✝")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}(ctx)
|
||||||
server.routinesPool.Stop()
|
server.routinesPool.Stop()
|
||||||
close(server.configurationChan)
|
close(server.configurationChan)
|
||||||
close(server.configurationValidatedChan)
|
close(server.configurationValidatedChan)
|
||||||
close(server.signals)
|
close(server.signals)
|
||||||
close(server.stopChan)
|
close(server.stopChan)
|
||||||
server.loggerMiddleware.Close()
|
server.loggerMiddleware.Close()
|
||||||
|
cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *Server) startHTTPServers() {
|
func (server *Server) startHTTPServers() {
|
||||||
|
|
Loading…
Reference in a new issue