-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
282 lines (226 loc) · 6.23 KB
/
example_test.go
File metadata and controls
282 lines (226 loc) · 6.23 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package breaker_test
import (
"context"
"errors"
"fmt"
"time"
"github.com/bjaus/breaker"
)
// ExampleNew demonstrates creating a circuit breaker with default settings.
func ExampleNew() {
circuit := breaker.New("my-service")
err := circuit.Do(context.Background(), func(ctx context.Context) error {
return nil
})
fmt.Println("Error:", err)
fmt.Println("State:", circuit.State())
// Output:
// Error: <nil>
// State: closed
}
// ExampleNew_withOptions demonstrates creating a circuit breaker with custom settings.
func ExampleNew_withOptions() {
circuit := breaker.New("payment-service",
breaker.WithFailureThreshold(3),
breaker.WithSuccessThreshold(2),
breaker.WithOpenDuration(30*time.Second),
)
fmt.Println("Name:", circuit.Name())
fmt.Println("State:", circuit.State())
// Output:
// Name: payment-service
// State: closed
}
// ExampleCircuit_Do demonstrates basic circuit breaker usage.
func ExampleCircuit_Do() {
circuit := breaker.New("api",
breaker.WithFailureThreshold(2),
)
attempts := 0
for range 5 {
err := circuit.Do(context.Background(), func(ctx context.Context) error {
attempts++
return errors.New("service unavailable")
})
if breaker.IsOpen(err) {
fmt.Println("Circuit is open, skipping call")
}
}
fmt.Println("Attempts:", attempts)
fmt.Println("State:", circuit.State())
// Output:
// Circuit is open, skipping call
// Circuit is open, skipping call
// Circuit is open, skipping call
// Attempts: 2
// State: open
}
// ExampleRun demonstrates the generic helper for returning values.
func ExampleRun() {
circuit := breaker.New("user-service")
user, err := breaker.Run(context.Background(), circuit, func(ctx context.Context) (string, error) {
return "john_doe", nil
})
fmt.Println("User:", user)
fmt.Println("Error:", err)
// Output:
// User: john_doe
// Error: <nil>
}
// ExampleIsOpen demonstrates checking if an error is due to an open circuit.
func ExampleIsOpen() {
circuit := breaker.New("service",
breaker.WithFailureThreshold(1),
)
_ = circuit.Do(context.Background(), func(ctx context.Context) error {
return errors.New("fail")
})
err := circuit.Do(context.Background(), func(ctx context.Context) error {
return nil
})
if breaker.IsOpen(err) {
fmt.Println("Circuit is open, using fallback")
}
// Output:
// Circuit is open, using fallback
}
// ExampleCircuit_Reset demonstrates manually resetting a circuit.
func ExampleCircuit_Reset() {
circuit := breaker.New("service",
breaker.WithFailureThreshold(1),
)
_ = circuit.Do(context.Background(), func(ctx context.Context) error {
return errors.New("fail")
})
fmt.Println("Before reset:", circuit.State())
circuit.Reset()
fmt.Println("After reset:", circuit.State())
// Output:
// Before reset: open
// After reset: closed
}
// ExampleIf demonstrates custom failure conditions.
func ExampleIf() {
transient := errors.New("transient error")
circuit := breaker.New("api",
breaker.WithFailureThreshold(2),
breaker.If(func(err error) bool {
return errors.Is(err, transient)
}),
)
_ = circuit.Do(context.Background(), func(ctx context.Context) error {
return errors.New("permanent error")
})
_ = circuit.Do(context.Background(), func(ctx context.Context) error {
return errors.New("permanent error")
})
fmt.Println("After permanent errors:", circuit.State())
_ = circuit.Do(context.Background(), func(ctx context.Context) error {
return transient
})
_ = circuit.Do(context.Background(), func(ctx context.Context) error {
return transient
})
fmt.Println("After transient errors:", circuit.State())
// Output:
// After permanent errors: closed
// After transient errors: open
}
// ExampleOnStateChange demonstrates the state change hook.
func ExampleOnStateChange() {
circuit := breaker.New("service",
breaker.WithFailureThreshold(1),
breaker.OnStateChange(func(name string, from, to breaker.State) {
fmt.Printf("Circuit %s: %s -> %s\n", name, from, to)
}),
)
_ = circuit.Do(context.Background(), func(ctx context.Context) error {
return errors.New("fail")
})
// Output:
// Circuit service: closed -> open
}
// ExampleOnCall demonstrates the call hook for metrics.
func ExampleOnCall() {
successCount := 0
failureCount := 0
circuit := breaker.New("service",
breaker.OnCall(func(name string, state breaker.State, err error) {
if err != nil {
failureCount++
} else {
successCount++
}
}),
)
_ = circuit.Do(context.Background(), func(ctx context.Context) error {
return nil
})
_ = circuit.Do(context.Background(), func(ctx context.Context) error {
return errors.New("fail")
})
_ = circuit.Do(context.Background(), func(ctx context.Context) error {
return nil
})
fmt.Println("Successes:", successCount)
fmt.Println("Failures:", failureCount)
// Output:
// Successes: 2
// Failures: 1
}
// ExampleOnReject demonstrates the reject hook.
func ExampleOnReject() {
rejectCount := 0
circuit := breaker.New("service",
breaker.WithFailureThreshold(1),
breaker.OnReject(func(name string) {
rejectCount++
}),
)
_ = circuit.Do(context.Background(), func(ctx context.Context) error {
return errors.New("fail")
})
for range 3 {
_ = circuit.Do(context.Background(), func(ctx context.Context) error {
return nil
})
}
fmt.Println("Rejected:", rejectCount)
// Output:
// Rejected: 3
}
// Example_fallback demonstrates graceful degradation when circuit is open.
func Example_fallback() {
circuit := breaker.New("user-service",
breaker.WithFailureThreshold(1),
)
getUser := func(ctx context.Context, _ int) (string, error) {
user, err := breaker.Run(ctx, circuit, func(ctx context.Context) (string, error) {
return "", errors.New("service unavailable")
})
if breaker.IsOpen(err) {
return "guest", nil
}
if err != nil {
return "", err
}
return user, nil
}
_, err1 := getUser(context.Background(), 1)
user2, _ := getUser(context.Background(), 2)
fmt.Println("User 1 error:", err1 != nil)
fmt.Println("User 2:", user2)
// Output:
// User 1 error: true
// User 2: guest
}
// ExampleState_String demonstrates state string representation.
func ExampleState_String() {
fmt.Println(breaker.Closed.String())
fmt.Println(breaker.Open.String())
fmt.Println(breaker.HalfOpen.String())
// Output:
// closed
// open
// half-open
}