Implemented graceful shutdown via go 1.8 Server.Shutdown(ctx)

This commit is contained in:
Dino Omanovic
2017-05-26 11:45:56 +02:00
parent 91b029ebe1
commit 70eb75cfda
3 changed files with 23 additions and 8 deletions
+1
View File
@@ -61,6 +61,7 @@ _Note for Caddy users_: Not all parameters are available in Caddy. See the table
| -success-url | string | "/" | X | The url to redirect after login |
| -template | string | | X | An alternative template for the login form |
| -text-logging | boolean | true | - | Log in text format instead of json |
| -grace-period | go duration | 5s | - | Duration to wait after SIGINT/SIGTERM for existing requests. No new requests are accepted. |
### Environment Variables
All of the above Config Options can also be applied as environment variable, where the name is written in the way: `LOGINSRV_OPTION_NAME`.
+3
View File
@@ -34,6 +34,7 @@ func DefaultConfig() *Config {
CookieHTTPOnly: true,
Backends: Options{},
Oauth: Options{},
GracePeriod: 5 * time.Second,
}
}
@@ -57,6 +58,7 @@ type Config struct {
CookieHTTPOnly bool
Backends Options
Oauth Options
GracePeriod time.Duration
}
// Options is the configuration structure for oauth and backend provider
@@ -101,6 +103,7 @@ func (c *Config) ConfigureFlagSet(f *flag.FlagSet) {
f.StringVar(&c.LogoutURL, "logout-url", c.LogoutURL, "The url or path to redirect after logout")
f.StringVar(&c.Template, "template", c.Template, "An alternative template for the login form")
f.StringVar(&c.LoginPath, "login-path", c.LoginPath, "The path of the login resource")
f.DurationVar(&c.GracePeriod, "grace-period", c.GracePeriod, "Graceful shutdown grace period")
// the -backends is deprecated, but we support it for backwards compatibility
deprecatedBackends := setFunc(func(optsKvList string) error {
+19 -8
View File
@@ -6,6 +6,8 @@ import (
"github.com/tarent/loginsrv/login"
"context"
"fmt"
"github.com/tarent/loginsrv/logging"
"net/http"
"os"
@@ -21,8 +23,6 @@ func main() {
exit(nil, err)
}
logShutdownEvent()
configToLog := *config
configToLog.JwtSecret = "..."
logging.LifecycleStart(applicationName, configToLog)
@@ -34,15 +34,26 @@ func main() {
handlerChain := logging.NewLogMiddleware(h)
exit(nil, http.ListenAndServe(config.Host+":"+config.Port, handlerChain))
}
stop := make(chan os.Signal)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
port := config.Port
if port != "" {
port = fmt.Sprintf(":%s", port)
}
httpSrv := &http.Server{Addr: port, Handler: handlerChain}
func logShutdownEvent() {
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
exit(<-c, nil)
if err := httpSrv.ListenAndServe(); err != nil {
logging.LifecycleStop(applicationName, nil, err)
}
}()
logging.LifecycleStop(applicationName, <-stop, nil)
ctx, _ := context.WithTimeout(context.Background(), config.GracePeriod)
httpSrv.Shutdown(ctx)
}
var exit = func(signal os.Signal, err error) {