gredis_config.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gredis
  7. import (
  8. "context"
  9. "crypto/tls"
  10. "time"
  11. "github.com/gogf/gf/v2/container/gmap"
  12. "github.com/gogf/gf/v2/errors/gcode"
  13. "github.com/gogf/gf/v2/errors/gerror"
  14. "github.com/gogf/gf/v2/internal/intlog"
  15. "github.com/gogf/gf/v2/util/gconv"
  16. )
  17. // Config is redis configuration.
  18. type Config struct {
  19. // Address It supports single and cluster redis server. Multiple addresses joined with char ','. Eg: 192.168.1.1:6379, 192.168.1.2:6379.
  20. Address string `json:"address"`
  21. Db int `json:"db"` // Redis db.
  22. User string `json:"user"` // Username for AUTH.
  23. Pass string `json:"pass"` // Password for AUTH.
  24. MinIdle int `json:"minIdle"` // Minimum number of connections allowed to be idle (default is 0)
  25. MaxIdle int `json:"maxIdle"` // Maximum number of connections allowed to be idle (default is 10)
  26. MaxActive int `json:"maxActive"` // Maximum number of connections limit (default is 0 means no limit).
  27. MaxConnLifetime time.Duration `json:"maxConnLifetime"` // Maximum lifetime of the connection (default is 30 seconds, not allowed to be set to 0)
  28. IdleTimeout time.Duration `json:"idleTimeout"` // Maximum idle time for connection (default is 10 seconds, not allowed to be set to 0)
  29. WaitTimeout time.Duration `json:"waitTimeout"` // Timed out duration waiting to get a connection from the connection pool.
  30. DialTimeout time.Duration `json:"dialTimeout"` // Dial connection timeout for TCP.
  31. ReadTimeout time.Duration `json:"readTimeout"` // Read timeout for TCP. DO NOT set it if not necessary.
  32. WriteTimeout time.Duration `json:"writeTimeout"` // Write timeout for TCP.
  33. MasterName string `json:"masterName"` // Used in Redis Sentinel mode.
  34. TLS bool `json:"tls"` // Specifies whether TLS should be used when connecting to the server.
  35. TLSSkipVerify bool `json:"tlsSkipVerify"` // Disables server name verification when connecting over TLS.
  36. TLSConfig *tls.Config `json:"-"` // TLS Config to use. When set TLS will be negotiated.
  37. SlaveOnly bool `json:"slaveOnly"` // Route all commands to slave read-only nodes.
  38. Cluster bool `json:"cluster"` // Specifies whether cluster mode be used.
  39. Protocol int `json:"protocol"` // Specifies the RESP version (Protocol 2 or 3.)
  40. }
  41. const (
  42. DefaultGroupName = "default" // Default configuration group name.
  43. )
  44. var (
  45. // Configuration groups.
  46. localConfigMap = gmap.NewStrAnyMap(true)
  47. )
  48. // SetConfig sets the global configuration for specified group.
  49. // If `name` is not passed, it sets configuration for the default group name.
  50. func SetConfig(config *Config, name ...string) {
  51. group := DefaultGroupName
  52. if len(name) > 0 {
  53. group = name[0]
  54. }
  55. localConfigMap.Set(group, config)
  56. intlog.Printf(context.TODO(), `SetConfig for group "%s": %+v`, group, config)
  57. }
  58. // SetConfigByMap sets the global configuration for specified group with map.
  59. // If `name` is not passed, it sets configuration for the default group name.
  60. func SetConfigByMap(m map[string]interface{}, name ...string) error {
  61. group := DefaultGroupName
  62. if len(name) > 0 {
  63. group = name[0]
  64. }
  65. config, err := ConfigFromMap(m)
  66. if err != nil {
  67. return err
  68. }
  69. localConfigMap.Set(group, config)
  70. return nil
  71. }
  72. // ConfigFromMap parses and returns config from given map.
  73. func ConfigFromMap(m map[string]interface{}) (config *Config, err error) {
  74. config = &Config{}
  75. if err = gconv.Scan(m, config); err != nil {
  76. err = gerror.NewCodef(gcode.CodeInvalidConfiguration, `invalid redis configuration: %#v`, m)
  77. }
  78. if config.DialTimeout < time.Second {
  79. config.DialTimeout = config.DialTimeout * time.Second
  80. }
  81. if config.WaitTimeout < time.Second {
  82. config.WaitTimeout = config.WaitTimeout * time.Second
  83. }
  84. if config.WriteTimeout < time.Second {
  85. config.WriteTimeout = config.WriteTimeout * time.Second
  86. }
  87. if config.ReadTimeout < time.Second {
  88. config.ReadTimeout = config.ReadTimeout * time.Second
  89. }
  90. if config.IdleTimeout < time.Second {
  91. config.IdleTimeout = config.IdleTimeout * time.Second
  92. }
  93. if config.MaxConnLifetime < time.Second {
  94. config.MaxConnLifetime = config.MaxConnLifetime * time.Second
  95. }
  96. if config.Protocol != 2 && config.Protocol != 3 {
  97. config.Protocol = 3
  98. }
  99. return
  100. }
  101. // GetConfig returns the global configuration with specified group name.
  102. // If `name` is not passed, it returns configuration of the default group name.
  103. func GetConfig(name ...string) (config *Config, ok bool) {
  104. group := DefaultGroupName
  105. if len(name) > 0 {
  106. group = name[0]
  107. }
  108. if v := localConfigMap.Get(group); v != nil {
  109. return v.(*Config), true
  110. }
  111. return &Config{}, false
  112. }
  113. // RemoveConfig removes the global configuration with specified group.
  114. // If `name` is not passed, it removes configuration of the default group name.
  115. func RemoveConfig(name ...string) {
  116. group := DefaultGroupName
  117. if len(name) > 0 {
  118. group = name[0]
  119. }
  120. localConfigMap.Remove(group)
  121. intlog.Printf(context.TODO(), `RemoveConfig: %s`, group)
  122. }
  123. // ClearConfig removes all configurations of redis.
  124. func ClearConfig() {
  125. localConfigMap.Clear()
  126. }