Created
July 17, 2024 15:03
-
-
Save nickstenning/3bf7f266ae12fdc15823a11bd5289b7c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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