-
Notifications
You must be signed in to change notification settings - Fork 35
/
evacuation_context.go
55 lines (44 loc) · 1.14 KB
/
evacuation_context.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
package evacuation_context
import "sync"
//go:generate counterfeiter -o fake_evacuation_context/fake_evacuatable.go . Evacuatable
type Evacuatable interface {
Evacuate()
}
//go:generate counterfeiter -o fake_evacuation_context/fake_evacuation_reporter.go . EvacuationReporter
type EvacuationReporter interface {
Evacuating() bool
}
//go:generate counterfeiter -o fake_evacuation_context/fake_evacuation_notifier.go . EvacuationNotifier
type EvacuationNotifier interface {
EvacuateNotify() <-chan struct{}
}
type evacuationContext struct {
evacuated chan struct{}
mu sync.Mutex
}
func New() (Evacuatable, EvacuationReporter, EvacuationNotifier) {
evacuationContext := &evacuationContext{
evacuated: make(chan struct{}),
}
return evacuationContext, evacuationContext, evacuationContext
}
func (e *evacuationContext) Evacuate() {
e.mu.Lock()
defer e.mu.Unlock()
select {
case <-e.evacuated:
default:
close(e.evacuated)
}
}
func (e *evacuationContext) Evacuating() bool {
select {
case <-e.evacuated:
return true
default:
return false
}
}
func (e *evacuationContext) EvacuateNotify() <-chan struct{} {
return e.evacuated
}