Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #73 from aandryashin/master
Browse files Browse the repository at this point in the history
Add graceful shutdown
  • Loading branch information
vania-pooh authored Jun 4, 2017
2 parents aedb75b + 81674f9 commit 066f0f9
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import (
"path/filepath"
"strings"

"context"
"os"
"os/signal"
"syscall"
"time"
)

var (
listen string
quotaDir string
users string
timeout time.Duration
listen string
quotaDir string
users string
timeout time.Duration
gracefulPeriod time.Duration

startTime = time.Now()
lastReloadTime = time.Now()
Expand Down Expand Up @@ -74,6 +76,7 @@ func init() {
flag.StringVar(&quotaDir, "quotaDir", "quota", "quota directory")
flag.StringVar(&users, "users", ".htpasswd", "htpasswd auth file path")
flag.DurationVar(&timeout, "timeout", 300*time.Second, "session creation timeout in time.Duration format, e.g. 300s or 500ms")
flag.DurationVar(&gracefulPeriod, "graceful-period", 300*time.Second, "graceful shutdown period in time.Duration format, e.g. 300s or 500ms")
flag.BoolVar(&version, "version", false, "show version and exit")
flag.Parse()
if version {
Expand All @@ -95,5 +98,20 @@ func init() {
}

func main() {
log.Fatal(http.ListenAndServe(listen, mux()))
stop := make(chan os.Signal)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)

server := &http.Server{
Addr: listen,
Handler: mux(),
}
go server.ListenAndServe()

<-stop

ctx, cancel := context.WithTimeout(context.Background(), gracefulPeriod)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("graceful shutdown: %v\n", err)
}
}

0 comments on commit 066f0f9

Please sign in to comment.