redis_sentinel_store.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package redis3
  2. import (
  3. "time"
  4. "github.com/go-redis/redis/v8"
  5. "github.com/go-redsync/redsync/v4"
  6. "github.com/go-redsync/redsync/v4/redis/goredis/v8"
  7. "github.com/seaweedfs/seaweedfs/weed/filer"
  8. "github.com/seaweedfs/seaweedfs/weed/util"
  9. )
  10. func init() {
  11. filer.Stores = append(filer.Stores, &Redis3SentinelStore{})
  12. }
  13. type Redis3SentinelStore struct {
  14. UniversalRedis3Store
  15. }
  16. func (store *Redis3SentinelStore) GetName() string {
  17. return "redis3_sentinel"
  18. }
  19. func (store *Redis3SentinelStore) Initialize(configuration util.Configuration, prefix string) (err error) {
  20. return store.initialize(
  21. configuration.GetStringSlice(prefix+"addresses"),
  22. configuration.GetString(prefix+"masterName"),
  23. configuration.GetString(prefix+"username"),
  24. configuration.GetString(prefix+"password"),
  25. configuration.GetInt(prefix+"database"),
  26. )
  27. }
  28. func (store *Redis3SentinelStore) initialize(addresses []string, masterName string, username string, password string, database int) (err error) {
  29. store.Client = redis.NewFailoverClient(&redis.FailoverOptions{
  30. MasterName: masterName,
  31. SentinelAddrs: addresses,
  32. Username: username,
  33. Password: password,
  34. DB: database,
  35. MinRetryBackoff: time.Millisecond * 100,
  36. MaxRetryBackoff: time.Minute * 1,
  37. ReadTimeout: time.Second * 30,
  38. WriteTimeout: time.Second * 5,
  39. })
  40. store.redsync = redsync.New(goredis.NewPool(store.Client))
  41. return
  42. }