forked from feyeleanor/GoLightly
-
Notifications
You must be signed in to change notification settings - Fork 2
/
termination_condition.go
91 lines (73 loc) · 1.97 KB
/
termination_condition.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
84
85
86
87
88
89
90
91
package govirtual
import (
"time"
)
type TerminationCondition interface {
ShouldTerminate(*Processor) bool
}
type AndTerminationCondition []TerminationCondition
type OrTerminationCondition []TerminationCondition
type NotTerminationCondition struct {
NotCondition *TerminationCondition
}
func AndTerminate(term ...TerminationCondition) *AndTerminationCondition {
out := AndTerminationCondition(term)
return &out
}
func OrTerminate(term ...TerminationCondition) *OrTerminationCondition {
out := OrTerminationCondition(term)
return &out
}
func NotTerminate(term *TerminationCondition) *NotTerminationCondition {
return &NotTerminationCondition{term}
}
func (term AndTerminationCondition) ShouldTerminate(p *Processor) bool {
for _, x := range term {
if !(x).ShouldTerminate(p) {
return false
}
}
return true
}
func (term OrTerminationCondition) ShouldTerminate(p *Processor) bool {
for _, x := range term {
if (x).ShouldTerminate(p) {
return true
}
}
return false
}
func (term *NotTerminationCondition) ShouldTerminate(p *Processor) bool {
return !(*(*term).NotCondition).ShouldTerminate(p)
}
type TimeTerminationCondition struct {
MaxTime time.Duration
StartTime int
}
func NewTimeTerminationCondition(maxTime time.Duration) *TimeTerminationCondition {
return &TimeTerminationCondition{maxTime, int(time.Now().UnixNano())}
}
func (term *TimeTerminationCondition) Reset() {
term.StartTime = int(time.Now().UnixNano())
}
func (term *TimeTerminationCondition) ShouldTerminate(p *Processor) bool {
if int(term.MaxTime)+term.StartTime < int(time.Now().UnixNano()) {
term.StartTime = int(time.Now().UnixNano())
return true
} else {
return false
}
}
type ChannelTerminationCondition chan bool
func NewChannelTerminationCondition() *ChannelTerminationCondition {
x := make(ChannelTerminationCondition, 1)
return &x
}
func (term *ChannelTerminationCondition) ShouldTerminate(p *Processor) bool {
select {
case x := <-(*term):
return x
default:
return false
}
}