-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctl.go
43 lines (35 loc) · 1.18 KB
/
ctl.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
package raft
import "context"
// asyncCtl is used to control an operation's cancellation in an asynchronous manner.
type asyncCtl struct {
ctx context.Context
cancel context.CancelFunc
waitCh chan struct{}
}
func newAsyncCtl() *asyncCtl {
ctx, cancel := context.WithCancel(context.Background())
return &asyncCtl{ctx: ctx, cancel: cancel, waitCh: make(chan struct{}, 1)}
}
// Cancel is used to signal the operation to cancel/stop. This function does not
// guarantee that the operation is immediately cancelled/stopped after the call.
func (c *asyncCtl) Cancel() {
c.cancel()
}
// Cancelled is used with select to check if Cancel() is called.
// Operation should prepare to cancel/stop when the channel returned is closed.
func (c *asyncCtl) Cancelled() <-chan struct{} {
return c.ctx.Done()
}
func (c *asyncCtl) Context() context.Context {
return c.ctx
}
// Release is used to inform that the operation is cancelled/stopped.
// Should only be called once in one asyncCtl's lifecycle.
func (c *asyncCtl) Release() {
c.cancel()
close(c.waitCh)
}
// WaitRelease is used with select to wait for the operation to cancel/stop.
func (c *asyncCtl) WaitRelease() <-chan struct{} {
return c.waitCh
}