-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathattack.go
58 lines (54 loc) · 1.58 KB
/
attack.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
package hazana
import (
"context"
"errors"
"time"
)
// Attack must be implemented by a service client.
type Attack interface {
// Setup should establish the connection to the service
// It may want to access the config of the runner.
Setup(c Config) error
// Do performs one request and is executed in a separate goroutine.
// The context is used to cancel the request on timeout.
Do(ctx context.Context) DoResult
// Teardown can be used to close the connection to the service
Teardown() error
// Clone should return a fresh new Attack
// Make sure the new Attack has values for shared struct fields initialized at Setup.
Clone() Attack
}
var errAttackDoTimedOut = errors.New("hazana.Attack Do(ctx) timed out")
// attack calls attacker.Do upon each received next token, forever
// attack aborts the loop on a quit receive
// attack sends a result on the results channel after each call.
func attack(attacker Attack, next, quit <-chan bool, results chan<- result, timeout time.Duration) {
for {
select {
case <-next:
begin := time.Now()
done := make(chan DoResult)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
go func() {
done <- attacker.Do(ctx)
}()
var dor DoResult
// either get the result from the attacker or from the timeout
select {
case <-ctx.Done():
dor = DoResult{RequestLabel: "timeout", Error: errAttackDoTimedOut}
case dor = <-done:
}
end := time.Now()
results <- result{
doResult: dor,
begin: begin,
end: end,
elapsed: end.Sub(begin),
}
case <-quit:
return
}
}
}