Skip to content

Commit 136395c

Browse files
committed
Update example to demonstrate non-blocking server start
1 parent a65d5ee commit 136395c

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

graceful/graceful_example_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,18 @@ func ExampleGroup_Run() {
3939
g := graceful.Group{
4040
graceful.RunnerType{
4141
StartFunc: func(ctx context.Context) error {
42-
return s.ListenAndServe()
42+
errCh := make(chan error)
43+
go func() {
44+
errCh <- s.ListenAndServe()
45+
close(errCh)
46+
}()
47+
48+
select {
49+
case <-ctx.Done():
50+
return ctx.Err()
51+
case err := <-errCh:
52+
return err
53+
}
4354
},
4455
StopFunc: func(ctx context.Context) error {
4556
if err := s.Shutdown(ctx); err != nil {
@@ -49,14 +60,13 @@ func ExampleGroup_Run() {
4960
return nil
5061
},
5162
},
52-
// TODO: populate with more runners.
5363
}
5464

5565
if err := g.Run(ctx,
5666
graceful.WithStopSignals(syscall.SIGTERM, syscall.SIGINT),
5767
graceful.WithStopTimeout(1*time.Minute),
5868
graceful.WithStoppingCh(stoppingCh),
5969
); err != nil {
60-
panic(err) // TODO: handle error appropriately.
70+
panic(err)
6171
}
6272
}

0 commit comments

Comments
 (0)