-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Joeky <joeky5888@gmail.com>
- Loading branch information
Showing
3 changed files
with
60 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/appleboy/graceful" | ||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
func wait(server *fasthttp.Server) <-chan struct{} { | ||
graceful.NewManager().AddRunningJob(func(ctx context.Context) error { | ||
<-ctx.Done() | ||
server.DisableKeepalive = true | ||
if err := server.Shutdown(); err != nil { | ||
Warn("Shutdown err: %s", err) | ||
defer os.Exit(1) | ||
} else { | ||
Info("gracefully stopped") | ||
} | ||
return nil | ||
}) | ||
|
||
return graceful.NewManager().Done() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
func wait(server *fasthttp.Server) <-chan struct{} { | ||
ctx := make(chan os.Signal) | ||
sig := make(chan struct{}) | ||
signal.Notify(ctx, os.Interrupt, syscall.SIGTERM) | ||
go func() { | ||
<-ctx | ||
server.DisableKeepalive = true | ||
if err := server.Shutdown(); err != nil { | ||
Warn("Shutdown err: %s", err) | ||
defer os.Exit(1) | ||
} else { | ||
Info("gracefully stopped") | ||
} | ||
sig <- struct{}{} | ||
}() | ||
|
||
return sig | ||
} |