Skip to content

Commit

Permalink
rename Timeout to ShutdownTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
smintz committed Oct 12, 2023
1 parent a61167e commit 782bfbc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func main() {
Addr: ":8080",
}),
// Sets the timeout waiting for the server to stop.
orchestra.WithTimeout(time.Second * 5),
orchestra.WithShutdownTimeout(time.Second * 5),
)
err := orchestra.PlayUntilSignal(s, os.Interrupt, syscall.SIGTERM)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ import (
// ServerPlayer is a type that extends the *http.Server
type ServerPlayer struct {
*http.Server
Timeout time.Duration
ShutdownTimeout time.Duration
}

// ServerPlayerOption is a function interface to configure the ServerPlayer
type ServerPlayerOption func(s *ServerPlayer)

func NewServerPlayer(opts ...ServerPlayerOption) *ServerPlayer {
s := &ServerPlayer{
Server: &http.Server{},
Timeout: 10 * time.Second,
Server: &http.Server{},
ShutdownTimeout: 10 * time.Second,
}
for _, f := range opts {
f(s)
}
return s
}

// WithTimeout replaces the default timeout of ServerPlayer
func WithTimeout(timeout time.Duration) ServerPlayerOption {
// WithShutdownTimeout sets the shutdown timeout of ServerPlayer (10s by default)
func WithShutdownTimeout(timeout time.Duration) ServerPlayerOption {
return func(s *ServerPlayer) {
s.Timeout = timeout
s.ShutdownTimeout = timeout
}
}

Expand All @@ -55,7 +55,7 @@ func (s ServerPlayer) Play(ctxMain context.Context) error {

select {
case <-ctxMain.Done():
timeout := s.Timeout
timeout := s.ShutdownTimeout

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
Expand Down
8 changes: 4 additions & 4 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ func TestNewServerPlayer(t *testing.T) {
{
name: "default",
args: args{},
want: &ServerPlayer{Server: &http.Server{}, Timeout: time.Second * 10},
want: &ServerPlayer{Server: &http.Server{}, ShutdownTimeout: time.Second * 10},
},
{
name: "set timeout to 5s",
args: args{opts: []ServerPlayerOption{
WithTimeout(time.Second * 5),
WithShutdownTimeout(time.Second * 5),
}},
want: &ServerPlayer{Server: &http.Server{}, Timeout: time.Second * 5},
want: &ServerPlayer{Server: &http.Server{}, ShutdownTimeout: time.Second * 5},
},
{
name: "replace default http server",
args: args{opts: []ServerPlayerOption{
WithHTTPServer(&http.Server{Addr: ":4321"}),
}},
want: &ServerPlayer{Server: &http.Server{Addr: ":4321"}, Timeout: time.Second * 10},
want: &ServerPlayer{Server: &http.Server{Addr: ":4321"}, ShutdownTimeout: time.Second * 10},
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 782bfbc

Please sign in to comment.