-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
68 lines (54 loc) · 1.54 KB
/
Copy pathmain.go
File metadata and controls
68 lines (54 loc) · 1.54 KB
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
// Copyright (c) 2026 Onur Cinar.
// The source code is provided under MIT License.
// https://github.com/cinar/resile
package main
import (
"context"
"errors"
"fmt"
"math/rand"
"sync"
"time"
"github.com/cinar/resile"
)
func main() {
// Create an adaptive concurrency limiter
al := resile.NewAdaptiveLimiter()
// Create a policy using the adaptive limiter
p := resile.NewPolicy(
resile.WithAdaptiveLimiterInstance(al),
)
fmt.Println("Starting simulation with adaptive concurrency...")
fmt.Printf("Initial Max Concurrency: %d\n", al.GetMaxConcurrency())
ctx := context.Background()
var wg sync.WaitGroup
// Simulate periodic bursts of traffic
for i := 0; i < 50; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
// Each request has some random processing time
processTime := time.Duration(10+rand.Intn(20)) * time.Millisecond
// Periodically simulate a latency spike
if i > 25 && i < 35 {
processTime = time.Duration(100+rand.Intn(50)) * time.Millisecond
}
err := p.DoErr(ctx, func(innerCtx context.Context) error {
time.Sleep(processTime)
return nil
})
if err != nil {
if errors.Is(err, resile.ErrShedLoad) {
fmt.Printf("Request %d: Shed Load (Max: %d)\n", id, al.GetMaxConcurrency())
} else {
fmt.Printf("Request %d Error: %v\n", id, err)
}
} else if id%10 == 0 {
fmt.Printf("Request %d Successful (Max: %d)\n", id, al.GetMaxConcurrency())
}
}(i)
time.Sleep(10 * time.Millisecond)
}
wg.Wait()
fmt.Printf("Final Max Concurrency: %d\n", al.GetMaxConcurrency())
}