-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathretrier.go
43 lines (37 loc) · 929 Bytes
/
retrier.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
package main
import (
"fmt"
"time"
)
func retryOperation(operation func() error, maxRetries int, interval time.Duration) error {
var err error
for i := 0; i < maxRetries; i++ {
err = operation()
if err == nil {
return nil // 操作成功,直接返回
}
fmt.Printf("Operation failed: %s. Retrying...\n", err)
time.Sleep(interval)
}
return err
}
func main() {
maxRetries := 3
interval := 2 * time.Second
err := retryOperation(func() error {
fmt.Println("Performing operation...")
return performOperation() // 假设 performOperation 是一个可能失败的操作
}, maxRetries, interval)
if err != nil {
fmt.Printf("Operation failed after %d retries: %s\n", maxRetries, err)
} else {
fmt.Println("Operation successful!")
}
}
func performOperation() error {
// 模拟一个可能失败的操作
if time.Now().Unix()%2 == 0 {
return fmt.Errorf("Random error occurred")
}
return nil
}