/
burn_target.go
55 lines (41 loc) · 980 Bytes
/
burn_target.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 trunks
import (
"errors"
"sync"
"github.com/golang/protobuf/proto"
)
var errNoRequest = errors.New("no available request")
// Gtarget represents an attack target i.e one gRPC endpoint
type Gtarget struct {
// gRPC method name;
// Setter & Getter
MethodName string
// the gRPC request message(s); Could be multiple.
// This enables `trunks` to read and store multiple
// requests to burn one enpoint.
// Setter & Getter
Requests []proto.Message
// the gRPC response message
// Getter
Response proto.Message
ipick uint64 // index of the request to pick next
ipickMx sync.Mutex
}
func (t *Gtarget) pick(loop bool) (proto.Message, error) {
nReq := uint64(len(t.Requests))
if nReq == 0 {
return nil, errNoRequest
}
if nReq == 1 {
return t.Requests[0], nil
}
t.ipickMx.Lock()
defer t.ipickMx.Unlock()
if !loop && t.ipick >= nReq {
return nil, errNoRequest
}
defer func() {
t.ipick++
}()
return t.Requests[t.ipick%nReq], nil
}