This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsandbox.go
196 lines (162 loc) · 3.23 KB
/
sandbox.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"errors"
_ "launchpad.net/tomb"
"log"
_ "math"
"strconv"
"sync"
"time"
)
type SandboxInfo struct {
Id int
Properties map[string]string
Duration time.Duration
StartTime time.Time
Emitter LatencyEmitter
WaitGroup *sync.WaitGroup
Factory BehaviorFactory
}
type sandbox struct {
id int
props map[string]string
d time.Duration
start time.Time
emitter LatencyEmitter
wg *sync.WaitGroup
behavior Behavior
factory BehaviorFactory
stall bool
opsPerStall int
stall_counter int
stalls int
}
func NewSandbox(info *SandboxInfo) *sandbox {
return &sandbox{
id: info.Id,
props: info.Properties,
d: info.Duration,
start: info.StartTime,
emitter: info.Emitter,
wg: info.WaitGroup,
factory: info.Factory,
}
}
func (this *sandbox) Start() {
go this.loop()
}
// func (this *sandbox) Stop() (err error) {
// this.t.Kill(nil)
// return this.t.Wait()
// }
func (this *sandbox) loop() {
//defer this.t.Done()
this.setup()
defer this.teardown()
for !this.expired() {
this.update()
}
}
func (this *sandbox) setup() {
err := this.parseProperties()
if err != nil {
log.Fatal(err)
}
res, err := this.init()
if err != nil {
log.Fatal(err)
}
this.behavior = res
}
func (this *sandbox) init() (res Behavior, err error) {
defer func() {
e := recover()
if e != nil {
if u, ok := e.(error); ok {
err = u
}
}
return
}()
res = this.factory()
err = res.Init(this.props)
if err != nil {
return
}
return
}
func (this *sandbox) expired() (ok bool) {
return time.Since(this.start) > this.d
}
func (this *sandbox) update() {
err := this.work()
if err != nil {
log.Fatalf("behavior panic during Work: %+v", err)
}
}
func (this *sandbox) work() (err error) {
defer func() {
e := recover()
if e != nil {
if u, ok := e.(error); ok {
err = u
}
}
return
}()
t0 := time.Now()
res := this.behavior.Work(t0)
d := time.Since(t0)
usec := int64(d / time.Microsecond)
// If stalling is enabled, then introduce a 1s pause
// every N ops. Hopefully, this will fix the issue with
// some Work operations completing far too fast for
// the progress/stats reporting goroutines to work.
// I'm not sure why the select{} construct isn't picking
// the other channels...
if this.stall {
this.stall_counter += 1
if this.stall_counter > this.opsPerStall {
this.stalls += 1
this.stall_counter = 0
<-time.After(1 * time.Second)
}
}
this.emitter.PublishResponseTime(this.id, usec, res)
return
}
func (this *sandbox) teardown() {
this.close()
this.wg.Done()
}
func (this *sandbox) close() (err error) {
defer func() {
e := recover()
if e != nil {
if u, ok := e.(error); ok {
err = u
}
}
return
}()
this.behavior.Close()
this.behavior = nil
return
}
func (this *sandbox) parseProperties() (err error) {
props := this.props
const (
MinOpsPerStall = 100
)
if v, ok := props["internals.OpsPerStall"]; ok {
u, err := strconv.Atoi(v)
if err != nil || u < MinOpsPerStall {
return errors.New("internals.OpsPerStall must be >= 100 ops")
}
this.stall = true
this.opsPerStall = u
} else {
this.stall = false
}
return
}