Skip to content

Commit d407ed9

Browse files
DOC-5885 improved Go error handling page
1 parent 293a1d1 commit d407ed9

File tree

1 file changed

+8
-58
lines changed

1 file changed

+8
-58
lines changed

content/develop/clients/go/error-handling.md

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -82,37 +82,12 @@ return result, nil
8282

8383
### Pattern 3: Retry with backoff
8484

85-
Retry on temporary errors (see
85+
Retry on temporary errors such as timeouts (see
8686
[Pattern 3: Retry with backoff]({{< relref "/develop/clients/error-handling#pattern-3-retry-with-backoff" >}})
87-
for a full description):
88-
89-
```go
90-
import "time"
91-
92-
func getWithRetry(ctx context.Context, key string, maxRetries int) (string, error) {
93-
retryDelay := 100 * time.Millisecond
94-
95-
for attempt := 0; attempt < maxRetries; attempt++ {
96-
result, err := rdb.Get(ctx, key).Result()
97-
if err == nil {
98-
return result, nil
99-
}
100-
101-
// Check if error is temporary
102-
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
103-
if attempt < maxRetries-1 {
104-
time.Sleep(retryDelay)
105-
retryDelay *= 2 // Exponential backoff
106-
continue
107-
}
108-
}
109-
110-
return "", err
111-
}
112-
113-
return "", fmt.Errorf("max retries exceeded")
114-
}
115-
```
87+
for a full description). go-redis has built-in retry logic with
88+
configurable timing and backoffs. See [Retries]({{< relref "/develop/clients/go/produsage#retries" >}}) for more information. Note also that
89+
you can configure timeouts (which are one of the most common causes of
90+
temporary errors) for connections and commands. See [Timeouts]({{< relref "/develop/clients/go/produsage#timeouts" >}}) for more information.
11691

11792
### Pattern 4: Log and continue
11893

@@ -132,36 +107,11 @@ if err != nil {
132107
}
133108
```
134109

135-
## Connection pool errors
136-
137-
go-redis manages a connection pool automatically. If the pool is exhausted, operations will timeout. You can configure the pool size to avoid this if
138-
necessary:
139-
140-
```go
141-
rdb := redis.NewClient(&redis.Options{
142-
Addr: "localhost:6379",
143-
PoolSize: 10, // Number of connections
144-
})
145-
```
146-
147-
## Context-based cancellation
148-
149-
go-redis respects context cancellation. Use context timeouts for error handling:
150-
151-
```go
152-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
153-
defer cancel()
154-
155-
result, err := rdb.Get(ctx, key).Result()
156-
if err == context.DeadlineExceeded {
157-
logger.Warn("Operation timeout")
158-
// Handle timeout
159-
}
160-
```
110+
Note that go-redis also supports [OpenTelemetry](https://opentelemetry.io/)
111+
instrumentation to monitor performance and trace the execution of Redis commands. See [Observability]({{< relref "/develop/clients/go#observability" >}}) for more information.
161112

162113
## See also
163114

164115
- [Error handling]({{< relref "/develop/clients/error-handling" >}})
165116
- [Production usage]({{< relref "/develop/clients/go/produsage" >}})
166-
- [Connection pooling]({{< relref "/develop/clients/pools-and-muxing" >}})
167-
117+
- [Observability]({{< relref "/develop/clients/go#observability" >}})

0 commit comments

Comments
 (0)