-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcodel.go
90 lines (74 loc) · 2.33 KB
/
codel.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
package main
import (
"context"
"fmt"
"time"
"github.com/slok/goresilience"
"github.com/slok/goresilience/concurrencylimit"
"github.com/slok/goresilience/concurrencylimit/execute"
"github.com/slok/goresilience/concurrencylimit/limit"
)
const (
workers = 7
rate = 4
rateDuration = 1 * time.Second
burst = 300
burstRateDuration = 50 * time.Millisecond
burstTime = 20 * time.Second
)
func main() {
runner := concurrencylimit.New(concurrencylimit.Config{
Executor: execute.NewAdaptiveLIFOCodel(execute.AdaptiveLIFOCodelConfig{}),
Limiter: limit.NewStatic(workers),
})
fmt.Printf("CoDel runner ready with %d workers...======================================\n", workers)
// Create a Regular rate of request.
fmt.Printf("start receiving a execution rate of %d/s...======================================\n", rate)
go runRate(runner, rate, rateDuration, 0)
time.Sleep(5 * time.Second)
// Execute a burst that will make the CoDel start working.
fmt.Printf("receiving a burst execution rate of %d/s for %s...======================================\n", burst, burstTime)
runRate(runner, burst, burstRateDuration, burstTime)
fmt.Printf("stop burst...\n")
time.Sleep(2 * time.Minute)
}
// runRate rate will create a rate (N per second) of executions.
// if duration is different from 0 it will set a max duration for the rate.
func runRate(runner goresilience.Runner, rate int, rateDuration time.Duration, duration time.Duration) {
rateTick := time.NewTicker(rateDuration)
var timeout = make(<-chan time.Time)
if duration != 0 {
timeout = time.After(duration)
}
for {
select {
case <-rateTick.C:
for i := 0; i < rate; i++ {
go handler(runner, "executed (in %s)\n", "load shedded (in %s)\n")
}
case <-timeout:
return
}
}
}
// handler is the bussiness logic to execute using the runner.
func handler(runner goresilience.Runner, okmsg, failmsg string) {
start := time.Now()
err := runner.Run(context.TODO(), func(_ context.Context) error {
lat := 1 * time.Second
jitter := time.Duration(time.Now().UnixNano()%300) * time.Millisecond
if time.Now().UnixNano()%2 == 0 {
lat += jitter
} else {
lat -= jitter
}
time.Sleep(lat)
if okmsg != "" {
fmt.Printf(okmsg, time.Since(start))
}
return nil
})
if err != nil {
fmt.Printf(failmsg, time.Since(start))
}
}