-
Notifications
You must be signed in to change notification settings - Fork 0
/
doorman_client.go
128 lines (111 loc) · 3.62 KB
/
doorman_client.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
package main
import (
"expvar"
"flag"
"fmt"
"math/rand"
"net/http"
"time"
log "github.com/golang/glog"
"github.com/pborman/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/youtube/doorman/go/client/doorman"
"github.com/youtube/doorman/go/ratelimiter"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
var (
port = flag.Int("port", 0, "port to bind to")
addr = flag.String("addr", "", "address of the Doorman server")
resource = flag.String("resource", "", "name of requested resource")
count = flag.Int("count", 1, "how many clients to simulate")
initialCapacity = flag.Float64("initial_capacity", 0, "initial capacity to request")
maxCapacity = flag.Float64("max_capacity", 0, "maximum capacity to request")
minCapacity = flag.Float64("min_capacity", 0, "minimum capacity to request")
increaseChance = flag.Float64("increase_chance", 0, "chance the requested capacity will increase")
decreaseChance = flag.Float64("decrease_chance", 0, "chance the requested capacity will decrease")
step = flag.Float64("step", 0, "amount of capacity change")
interval = flag.Duration("interval", 1*time.Minute, "how often attempt changing requested capacity")
target = flag.String("target", "", "target greeter service")
workers = flag.Int("workers", 4, "number of worker goroutines")
)
var (
received = expvar.NewMap("received")
requested = expvar.NewMap("requested")
askErrors = expvar.NewInt("ask-errors")
)
func manipulateCapacity(res doorman.Resource, current float64, id string) {
clientRequested := new(expvar.Float)
for range time.Tick(*interval) {
r := rand.Float64()
log.V(2).Infof("r=%v decreaseChance=%v increaseChance=%v", r, *decreaseChance, *increaseChance)
switch {
case r < *decreaseChance:
current -= *step
log.Infof("client %v will request less: %v.", id, current)
case r < *decreaseChance+*increaseChance:
log.Infof("client %v will request more: %v.", id, current)
current += *step
default:
log.V(2).Infof("client %v not changing requested capacity", id)
continue
}
if current > *maxCapacity {
current = *maxCapacity
}
if current < *minCapacity {
current = *minCapacity
}
log.Infof("client %v will request %v", id, current)
if err := res.Ask(current); err != nil {
log.Errorf("res.Ask(%v): %v", current, err)
askErrors.Add(1)
continue
}
clientRequested.Set(current)
requested.Set(id, clientRequested)
}
}
func main() {
flag.Parse()
log.Infof("Simulating %v clients.", *count)
for i := 0; i < *count; i++ {
id := uuid.New()
log.Infof("client %v with id %v", i, id)
client, err := doorman.NewWithID(*addr, id, doorman.DialOpts(grpc.WithInsecure()))
if err != nil {
log.Exit(err)
}
defer client.Close()
res, err := client.Resource(*resource, *initialCapacity)
if err != nil {
log.Exit(err)
}
go manipulateCapacity(res, *initialCapacity, id)
conn, err := grpc.Dial(*target, grpc.WithInsecure())
if err != nil {
log.Exitf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
rl := ratelimiter.NewQPS(res)
for i := 0; i < *workers; i++ {
go func() {
ctx := context.Background()
for {
if err := rl.Wait(ctx); err != nil {
log.Exitf("rl.Wait: %v", err)
}
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
if _, err := c.SayHello(ctx, &pb.HelloRequest{Name: *resource}); err != nil {
log.Error(err)
}
cancel()
}
}()
}
}
http.Handle("/metrics", prometheus.Handler())
http.ListenAndServe(fmt.Sprintf(":%v", *port), nil)
}