-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex02_retry.go
More file actions
50 lines (41 loc) · 1.45 KB
/
ex02_retry.go
File metadata and controls
50 lines (41 loc) · 1.45 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
package ctxexercises
// Context: Context-Aware Retry Loops
// You are building an SDK that fetches user data from an unreliable 3rd-party API.
// Because it fails often, you want to retry up to 5 times, with a 1-second delay
// between retries.
//
// Why this matters: If the parent HTTP request was canceled by the user, or
// a global timeout was reached, your retry loop should NOT blindly sleep
// for another 1 second and try again. It should abort immediately!
//
// Requirements:
// 1. Refactor `FetchWithRetry` to respect `ctx.Done()`.
// 2. Instead of `time.Sleep(1 * time.Second)`, use a `select` statement that
// waits for *either* the sleep timer to finish *or* the context to cancel.
// 3. If the context cancels during the "sleep", return `ctx.Err()` immediately.
import (
"context"
"errors"
"time"
)
var ErrServerDown = errors.New("500 internal server error")
func UnreliableAPI() (string, error) {
return "", ErrServerDown
}
func FetchWithRetry(ctx context.Context) (string, error) {
// BUG: This loop sleeps and retries even if the context was already canceled!
// TODO: Replace time.Sleep with a select on time.After and ctx.Done().
for i := 0; i < 5; i++ {
// Check context before even making the call
if err := ctx.Err(); err != nil {
return "", err
}
res, err := UnreliableAPI()
if err == nil {
return res, nil
}
// Wait 1 second before retrying
time.Sleep(1 * time.Second)
}
return "", errors.New("max retries exceeded")
}