-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.go
60 lines (49 loc) · 872 Bytes
/
proxy.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
package main
import (
"time"
"github.com/golang/glog"
)
const proxyChannelSize = 10000
type Stat struct {
req_num int
ok_num int
fail_num int
}
type Proxy struct {
ch chan int
s Stat
cache *Cache
}
func NewProxy(s *Server) *Proxy {
p := &Proxy{
ch: make(chan int, proxyChannelSize),
cache: NewCache(s),
}
// Simulate a proxy; receives a request and check quota.
glog.Infof("Start a proxy")
go func() {
for true {
_ = <-p.ch
p.s.req_num++
if p.cache.Check() {
p.s.ok_num++
} else {
p.s.fail_num++
}
}
}()
return p
}
// Send N requests to the proxy in d duration
func (p *Proxy) Send(n int, d time.Duration) {
s := d / time.Duration(n)
if glog.V(2) {
glog.Infof("Send n=%d in duration=%v: one in every %v", n, d, s)
}
go func() {
for i := 0; i < n; i++ {
p.ch <- 1
time.Sleep(s)
}
}()
}