Skip to content

Instantly share code, notes, and snippets.

@nickstenning
Created July 17, 2024 15:03
Show Gist options
  • Save nickstenning/e9479b62b3609927e27dc95e05385250 to your computer and use it in GitHub Desktop.
Save nickstenning/e9479b62b3609927e27dc95e05385250 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"net"
"github.com/redis/go-redis/v9"
)
type dialerFunc func(ctx context.Context, network, addr string) (net.Conn, error)
func makeDialer(errorCount int) dialerFunc {
return func(ctx context.Context, network, addr string) (net.Conn, error) {
if errorCount > 0 {
errorCount--
return nil, fmt.Errorf("kaboom")
}
return net.Dial(network, addr)
}
}
func main() {
ctx := context.Background()
rdb1 := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Dialer: makeDialer(1),
PoolSize: 2,
})
rdb2 := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Dialer: makeDialer(2),
PoolSize: 2,
})
fmt.Println("With only one error, the client recovers:")
for i := 0; i < 10; i++ {
status, err := rdb1.Ping(ctx).Result()
fmt.Printf("%d status: %q, err: %v\n", i, status, err)
}
fmt.Println("But if we have >= PoolSize errors in a row, the client is broken forever:")
for i := 0; i < 10; i++ {
status, err := rdb2.Ping(ctx).Result()
fmt.Printf("%d status: %q, err: %v\n", i, status, err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment