failover_timer.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "os"
  7. "time"
  8. "github.com/go-redis/redis/v8"
  9. "github.com/pborman/getopt/v2"
  10. )
  11. var ctx = context.Background()
  12. func main() {
  13. helpFlag := getopt.BoolLong("help", 'h', "display help")
  14. redisHost := getopt.StringLong("host", 's', "", "name or IP of any cluster node")
  15. redisPort := getopt.IntLong("port", 'p', 6379, "port for the cluster node")
  16. redisPassword := getopt.StringLong("password", 'a', "", "cluster password")
  17. messageCount := getopt.IntLong("message-count", 'm', 10000, "Message count")
  18. minBackOff := getopt.IntLong("min-backoff", 'i', 500, "ms to start backoff")
  19. maxBackOff := getopt.IntLong("max-backoff", 'x', 1000, "ms to start backoff")
  20. getopt.Parse()
  21. if *helpFlag || *redisHost == "" {
  22. getopt.PrintUsage(os.Stderr)
  23. os.Exit(1)
  24. }
  25. client := redis.NewClusterClient(&redis.ClusterOptions{
  26. Addrs: []string{fmt.Sprintf("%s:%d", *redisHost, *redisPort)},
  27. MaxRedirects: 3,
  28. Password: *redisPassword,
  29. PoolSize: 8,
  30. MinRetryBackoff: time.Duration(*minBackOff) * time.Millisecond,
  31. MaxRetryBackoff: time.Duration(*maxBackOff) * time.Millisecond,
  32. MinIdleConns: 5,
  33. PoolTimeout: 0,
  34. ReadTimeout: 4 * time.Millisecond,
  35. WriteTimeout: 4 * time.Millisecond,
  36. IdleTimeout: 20 * time.Second,
  37. DialTimeout: 2 * time.Second,
  38. })
  39. client.Ping(ctx)
  40. for i := 0; i < *messageCount; i++ {
  41. start := time.Now()
  42. misses := client.PoolStats().Misses
  43. _, err := client.Set(ctx, "DTM", "DTM", 0).Result()
  44. elapsed := time.Since(start)
  45. if err != nil {
  46. fmt.Println(err)
  47. }
  48. if client.PoolStats().Misses > misses && elapsed.Milliseconds() > 1 {
  49. log.Printf("Failover: Client took %d ms to complete operation %+v\n", elapsed.Milliseconds(), client.PoolStats())
  50. }
  51. }
  52. }