forked from looplab/eventhorizon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graceful.go
83 lines (68 loc) · 1.74 KB
/
graceful.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package memory
import (
"context"
"time"
)
// GracefulContext is a context that can be gracefully cancelled after the original
// context has been canceled, but with a grace period to let operations finish.
// The KeepAlive method should be used to signal that operations are still ongoing.
type GracefulContext struct {
context.Context
doneCh chan struct{}
}
// NewGracefulContext will create a GracefulContext with an idle time which should be
// longer than a typical operation cadence, and a "force after" duration which
// should be long enough to let some amount of operations to finish.
func NewGracefulContext(ctx context.Context, idle, forceAfter time.Duration) (
c context.Context,
keepAlive func(),
cancel func(),
) {
keepAliveCh := make(chan struct{})
stopCh := make(chan struct{})
doneCh := make(chan struct{})
go func() {
defer close(doneCh)
select {
case <-ctx.Done():
// Will trigger the graceful shutdown.
case <-stopCh:
// Will skip graceful shutdown.
return
}
idleT := time.NewTimer(idle)
forceShutdown := time.After(forceAfter)
for {
select {
case <-keepAliveCh: // Reset the idle timer on activity.
if !idleT.Stop() {
<-idleT.C
}
idleT.Reset(idle)
continue
case <-idleT.C: // If there is no more activity, cancel.
return
case <-forceShutdown: // If there is too much activity, cancel anyway.
return
}
}
}()
c = &GracefulContext{
Context: ctx,
doneCh: doneCh,
}
keepAlive = func() {
select {
case keepAliveCh <- struct{}{}:
default:
}
}
cancel = func() {
close(stopCh)
}
return c, keepAlive, cancel
}
// Done implements the Done method of the context.Context interface.
func (g *GracefulContext) Done() <-chan struct{} {
return g.doneCh
}