Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gracefulOpts を unexport #9

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions graceful.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,36 @@ type Servers struct {
Servers []Server
}

// GracefulOpts represents configuration parameters for the Server.
// In default, GracefulOpts.Signals is []os.Signal{os.Interrupt} and GracefulOpts.ShutdownTimeout is 0.
type GracefulOpts struct {
// gracefulOpts represents configuration parameters for the Server.
// In default, gracefulOpts.Signals is []os.Signal{os.Interrupt} and gracefulOpts.ShutdownTimeout is 0.
type gracefulOpts struct {
Signals []os.Signal
ShutdownTimeout time.Duration
}

func defaultGracefulOpts() GracefulOpts {
return GracefulOpts{
func defaultGracefulOpts() gracefulOpts {
return gracefulOpts{
Signals: []os.Signal{syscall.SIGINT, syscall.SIGTERM},
ShutdownTimeout: 0,
}
}

// Option applies an option to the Server.
type Option func(*gracefulOpts)

// GracefulSignals sets signals to be received.
func GracefulSignals(signals ...os.Signal) func(*GracefulOpts) {
return func(o *GracefulOpts) { o.Signals = signals }
func GracefulSignals(signals ...os.Signal) Option {
return func(o *gracefulOpts) { o.Signals = signals }
}

// GracefulShutdownTimeout sets timeout for shutdown.
func GracefulShutdownTimeout(timeout time.Duration) func(*GracefulOpts) {
return func(o *GracefulOpts) { o.ShutdownTimeout = timeout }
func GracefulShutdownTimeout(timeout time.Duration) Option {
return func(o *gracefulOpts) { o.ShutdownTimeout = timeout }
}

// Graceful runs all servers contained in s, then waits signals.
// When receive an expected signal, s stops all servers gracefully.
func (s Servers) Graceful(ctx context.Context, options ...func(*GracefulOpts)) error {
// When receive an expected signal (in default, os.Interrupt), s stops all servers gracefully.
func (s Servers) Graceful(ctx context.Context, options ...Option) error {
opts := defaultGracefulOpts()
for _, f := range options {
f(&opts)
Expand Down
2 changes: 1 addition & 1 deletion gracefulgrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func ListenAndServe(
ctx context.Context,
addr string,
server *grpc.Server,
options ...func(*graceful.GracefulOpts),
options ...graceful.Option,
) error {
srv := graceful.Servers{Servers: []graceful.Server{&Server{Addr: addr, Server: server}}}
return srv.Graceful(ctx, options...)
Expand Down
2 changes: 1 addition & 1 deletion gracefulhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func ListenAndServe(
ctx context.Context,
addr string,
handler http.Handler,
options ...func(*graceful.GracefulOpts),
options ...graceful.Option,
) error {
httpSrv := &http.Server{
Addr: addr,
Expand Down
Loading