-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
179 lines (150 loc) · 4.19 KB
/
main.go
File metadata and controls
179 lines (150 loc) · 4.19 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
// Package main demonstrates retry functionality.
package main
import (
"context"
"errors"
"fmt"
"math/rand"
"time"
"github.com/yigithankarabulut/wirekit"
"github.com/yigithankarabulut/wirekit/retry"
"go.uber.org/zap"
)
// BasicRetry demonstrates basic retry usage.
func BasicRetry(ctx context.Context) {
fmt.Println("\n--- Basic Retry ---")
attempt := 0
err := wirekit.Retry().
WithAttempts(5).
WithDelay(500*time.Millisecond).
WithBackoff(retry.ExponentialBackoff).
Do(ctx, func() error {
attempt++
wirekit.Debug(fmt.Sprintf("attempt %d", attempt))
// Simulate random failure
if rand.Float32() < 0.7 { //nolint:gosec
return errors.New("random failure")
}
return nil
})
if err != nil {
wirekit.Error("operation failed after all retries", zap.Error(err))
} else {
wirekit.Info(fmt.Sprintf("succeeded on attempt %d", attempt))
}
}
// RetryWithData demonstrates retry with data return.
func RetryWithData(ctx context.Context) {
fmt.Println("\n--- Retry With Data ---")
fetchAttempt := 0
data, err := retry.DoWithData(ctx, wirekit.Retry().WithAttempts(3), func() (string, error) {
fetchAttempt++
wirekit.Debug(fmt.Sprintf("fetch attempt %d", fetchAttempt))
if fetchAttempt < 2 {
return "", errors.New("fetch failed")
}
return "fetched data", nil
})
if err != nil {
wirekit.Error("failed to fetch data", zap.Error(err))
} else {
fmt.Println("Data:", data)
}
}
// ConditionalRetry demonstrates retry with condition.
func ConditionalRetry(ctx context.Context) {
fmt.Println("\n--- Conditional Retry ---")
ErrNotRetryable := errors.New("not retryable")
ErrRetryable := errors.New("retryable error")
retryAttempt := 0
err := wirekit.Retry().
WithAttempts(5).
WithRetryIf(func(err error) bool {
// Only retry specific errors
return !errors.Is(err, ErrNotRetryable)
}).
Do(ctx, func() error {
retryAttempt++
if retryAttempt == 1 {
return ErrRetryable // Will be retried
}
if retryAttempt == 2 {
return ErrNotRetryable // Will NOT be retried
}
return nil
})
if errors.Is(err, ErrNotRetryable) {
wirekit.Info("stopped retrying on non-retryable error")
}
}
// BackoffStrategies demonstrates different backoff strategies.
func BackoffStrategies() {
fmt.Println("\n--- Backoff Strategies ---")
// Constant delay
constantBackoff := wirekit.Retry().
WithAttempts(3).
WithDelay(time.Second).
WithBackoff(retry.ConstantBackoff)
fmt.Printf("Constant Backoff: %T\n", constantBackoff)
// Linear backoff: 1s, 2s, 3s, ...
linearBackoff := wirekit.Retry().
WithAttempts(5).
WithDelay(time.Second).
WithBackoff(retry.LinearBackoff)
fmt.Printf("Linear Backoff: %T\n", linearBackoff)
// Exponential backoff: 1s, 2s, 4s, 8s, ... (capped at maxDelay)
exponentialBackoff := wirekit.Retry().
WithAttempts(10).
WithDelay(time.Second).
WithMaxDelay(30 * time.Second).
WithBackoff(retry.ExponentialBackoff)
fmt.Printf("Exponential Backoff: %T\n", exponentialBackoff)
}
// QuickRetryHelpers demonstrates quick retry functions.
func QuickRetryHelpers(ctx context.Context) {
fmt.Println("\n--- Quick Retry Helpers ---")
// Try once (no retries)
_ = retry.Once(ctx, func() error {
return nil
})
fmt.Println("Once: completed")
// Try twice
_ = retry.Twice(ctx, func() error {
return nil
})
fmt.Println("Twice: completed")
// Try n times
_ = retry.Times(ctx, 5, func() error {
return nil
})
fmt.Println("Times(5): completed")
}
// ForeverRetry demonstrates infinite retry until success.
func ForeverRetry(ctx context.Context) {
fmt.Println("\n--- Forever Retry ---")
ctxWithTimeout, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
foreverAttempt := 0
err := retry.Forever(ctxWithTimeout, 500*time.Millisecond, func() error {
foreverAttempt++
if foreverAttempt < 3 {
return errors.New("not yet")
}
wirekit.Info("forever retry succeeded")
return nil
})
if err != nil {
wirekit.Error("forever retry cancelled", zap.Error(err))
}
}
func main() {
wirekit.Log(wirekit.Zap).WithLevel("debug").Init()
ctx := context.Background()
BasicRetry(ctx)
RetryWithData(ctx)
ConditionalRetry(ctx)
BackoffStrategies()
QuickRetryHelpers(ctx)
ForeverRetry(ctx)
wirekit.Info("retry example completed")
}