-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.go
52 lines (42 loc) · 1.13 KB
/
control.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package mid
import (
"context"
"net/http"
"os"
"os/signal"
"sync/atomic"
"syscall"
"time"
)
// InterruptContext listing for os.Signal (i.e. CTRL+C) to cancel a
// context and end a server/daemon gracefully
func InterruptContext() context.Context {
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-quit
cancel()
}()
return ctx
}
// ListenWithContext starts the server while watching context to trigger graceful shutdown;
// Use with mid.InterruptContext() to control application lifecycle
func ListenWithContext(ctx context.Context, server *http.Server, healthy *int32) error {
done := make(chan error)
go func() {
select {
case <-ctx.Done():
gracefulCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
atomic.StoreInt32(healthy, 0)
server.SetKeepAlivesEnabled(false)
done <- server.Shutdown(gracefulCtx)
}
}()
atomic.StoreInt32(healthy, 1)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return err
}
return <-done
}