-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.go
More file actions
57 lines (48 loc) · 1.6 KB
/
Copy pathmain.go
File metadata and controls
57 lines (48 loc) · 1.6 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
// Copyright (c) 2026 Onur Cinar.
// The source code is provided under MIT License.
// https://github.com/cinar/resile
package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/cinar/resile"
"github.com/cinar/resile/circuit"
)
func main() {
ctx := context.Background()
fmt.Println("--- Layered Defense with Sliding Window Circuit Breaker ---")
// Create a circuit breaker with a Count-based sliding window.
// It will trip if 50% of the last 10 calls fail, provided at least 4 calls were made.
cb := circuit.New(circuit.Config{
WindowType: circuit.WindowCountBased,
WindowSize: 10,
FailureRateThreshold: 50.0,
MinimumCalls: 4,
ResetTimeout: 5 * time.Second,
HalfOpenMaxCalls: 2,
})
action := func(ctx context.Context) error {
fmt.Println("Attempting operation...")
return errors.New("service temporarily unavailable")
}
// In this example, each iteration performs 2 retry attempts.
// Since 2 attempts will fail in the first iteration, and 2 in the second,
// the total failures will reach 4 (MinimumCalls) and the failure rate will be 100% (>50%).
for i := 0; i < 5; i++ {
fmt.Printf("\nIteration %d:\n", i+1)
err := resile.DoErr(ctx, action,
resile.WithCircuitBreaker(cb),
resile.WithMaxAttempts(2),
resile.WithBaseDelay(0), // Skip delay for simulation.
)
if errors.Is(err, circuit.ErrCircuitOpen) {
fmt.Printf("FAIL: Operation fast-failed because the circuit breaker is OPEN!\n")
} else if err != nil {
fmt.Printf("FAIL: Operation failed: %v\n", err)
} else {
fmt.Println("SUCCESS: Operation succeeded!")
}
}
}