-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
45 lines (35 loc) · 1.13 KB
/
main.go
File metadata and controls
45 lines (35 loc) · 1.13 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
// 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"
)
func main() {
ctx := context.Background()
fmt.Println("--- Basic Retry Example ---")
// 1. Simple retry for an operation that returns only an error.
err := resile.DoErr(ctx, func(ctx context.Context) error {
fmt.Println("Pinging database...")
// Simulate a transient failure.
return errors.New("connection reset by peer")
}, resile.WithMaxAttempts(3), resile.WithBaseDelay(100*time.Millisecond))
if err != nil {
fmt.Printf("Database ping failed after retries: %v\n", err)
}
fmt.Println("\n--- Value-Yielding Retry Example ---")
// 2. Retry for an operation that returns a value and an error.
// Type safety is guaranteed by generics.
val, err := resile.Do(ctx, func(ctx context.Context) (string, error) {
fmt.Println("Fetching configuration...")
// Use a neutral configuration string.
return "DATABASE_URL=postgres://localhost:5432/mydb", nil
})
if err == nil {
fmt.Printf("Configuration fetched: %s\n", val)
}
}