Skip to content

Commit

Permalink
cmd/flatend, flathttp: set kepe alives disabled upon shutdown, and ad…
Browse files Browse the repository at this point in the history
…d graceful shutdown support
  • Loading branch information
lithdew committed Jun 18, 2020
1 parent aa79fc5 commit 3f7d06e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
23 changes: 21 additions & 2 deletions cmd/flatend/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/tls"
"errors"
"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -135,7 +136,16 @@ func main() {
}

defer func() {
check(srv.Close())
srv.SetKeepAlivesEnabled(false)

timeout := cfg.GetShutdownTimeout()
if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
check(srv.Shutdown(ctx))
cancel()
} else {
check(srv.Close())
}
}()

addrs := cfg.GetAddrs()
Expand Down Expand Up @@ -172,7 +182,16 @@ func main() {
}

defer func() {
check(redirect.Close())
redirect.SetKeepAlivesEnabled(false)

timeout := cfg.GetShutdownTimeout()
if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
check(redirect.Shutdown(ctx))
cancel()
} else {
check(redirect.Close())
}
}()

for _, addr := range addrs {
Expand Down
9 changes: 9 additions & 0 deletions flathttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var Methods = map[string]struct{}{
http.MethodPatch: {},
}

const DefaultShutdownTimeout = 10 * time.Second

type Config struct {
Addr string
HTTP []ConfigHTTP
Expand Down Expand Up @@ -76,6 +78,13 @@ type ConfigHTTP struct {
Routes []ConfigRoute
}

func (h ConfigHTTP) GetShutdownTimeout() time.Duration {
if h.Timeout.Shutdown.Duration < 0 {
return DefaultShutdownTimeout
}
return h.Timeout.Shutdown.Duration
}

func (h ConfigHTTP) GetDomains() []string {
if h.Domain != "" {
return []string{h.Domain}
Expand Down

0 comments on commit 3f7d06e

Please sign in to comment.