-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest3_printserver.go
130 lines (98 loc) · 2.92 KB
/
test3_printserver.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
/*
*
* Program to do a load test, and validate the response team of our api
*
* @package main
* @author @jeffotoni
* @size 29/07/2017
*
*/
// _[Rate limiting](http://en.wikipedia.org/wiki/Rate_limiting)_
// is an important mechanism for controlling resource
// utilization and maintaining quality of service. Go
// elegantly supports rate limiting with goroutines,
// channels, and [tickers](tickers).
package main
import (
"fmt"
Shoot "github.com/jeffotoni/printserver/pkg"
// "os"
"time"
)
func main() {
endPoint1 := "http://localhost:9001/ping"
//endPoint2 = "http://localhost:9001/ping2"
// First we'll look at basic rate limiting. Suppose
// we want to limit our handling of incoming requests.
// We'll serve these requests off a channel of the
// same name.
requests := make(chan int, 20)
//
// Requests quantity channel
//
go func(requests chan int) {
for i := 1; i <= 20; i++ {
println("Loading requests: ", fmt.Sprintf("%d", i))
time.Sleep(time.Millisecond * 50)
requests <- i
}
close(requests)
}(requests)
//
//
//
go func(requests chan int) {
// This `limiter` channel will receive a value
// every 300 milliseconds. This is the regulator in
// our rate limiting scheme.
limiter := time.Tick(time.Millisecond * 200)
// limiter2 := time.Tick(time.Millisecond * 500)
time1 := time.Now()
// By blocking on a receive from the `limiter` channel
// before serving each request, we limit ourselves to
// 1 request every 200 milliseconds.
for req := range requests {
<-limiter
msg := Shoot.ShootUrl(endPoint1)
fmt.Println("request: ", req, "msg: ", msg)
}
time2 := time.Now()
diff := time2.Sub(time1)
fmt.Println(diff)
}(requests)
println("Enter enter to finish")
var input string
fmt.Scanln(&input)
// // We may want to allow short bursts of requests in
// // our rate limiting scheme while preserving the
// // overall rate limit. We can accomplish this by
// // buffering our limiter channel. This `burstyLimiter`
// // channel will allow bursts of up to 3 events.
// burstyLimiter := make(chan time.Time, 3)
// // Fill up the channel to represent allowed bursting.
// for i := 0; i < 3; i++ {
// // println("Loading limit")
// burstyLimiter <- time.Now()
// }
// // Every 300 milliseconds we'll try to add a new
// // value to `burstyLimiter`, up to its limit of 3.
// go func() {
// for t := range time.Tick(time.Millisecond * 300) {
// println("Loading limit range")
// burstyLimiter <- t
// // Shoot.ShootUrl(endPoint1)
// }
// }()
// // Now simulate 5 more incoming requests. The first
// // 3 of these will benefit from the burst capability
// // of `burstyLimiter`.
// burstyRequests := make(chan int, 5)
// for i := 1; i <= 5; i++ {
// burstyRequests <- i
// }
// close(burstyRequests)
// for req := range burstyRequests {
// <-burstyLimiter
// fmt.Println("request", req, time.Now())
// }
}