http urls monitor.

options.go 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package redis
  2. import (
  3. "crypto/tls"
  4. "errors"
  5. "fmt"
  6. "net"
  7. "net/url"
  8. "runtime"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/go-redis/redis/internal/pool"
  13. )
  14. type Options struct {
  15. // The network type, either tcp or unix.
  16. // Default is tcp.
  17. Network string
  18. // host:port address.
  19. Addr string
  20. // Dialer creates new network connection and has priority over
  21. // Network and Addr options.
  22. Dialer func() (net.Conn, error)
  23. // Hook that is called when new connection is established.
  24. OnConnect func(*Conn) error
  25. // Optional password. Must match the password specified in the
  26. // requirepass server configuration option.
  27. Password string
  28. // Database to be selected after connecting to the server.
  29. DB int
  30. // Maximum number of retries before giving up.
  31. // Default is to not retry failed commands.
  32. MaxRetries int
  33. // Minimum backoff between each retry.
  34. // Default is 8 milliseconds; -1 disables backoff.
  35. MinRetryBackoff time.Duration
  36. // Maximum backoff between each retry.
  37. // Default is 512 milliseconds; -1 disables backoff.
  38. MaxRetryBackoff time.Duration
  39. // Dial timeout for establishing new connections.
  40. // Default is 5 seconds.
  41. DialTimeout time.Duration
  42. // Timeout for socket reads. If reached, commands will fail
  43. // with a timeout instead of blocking.
  44. // Default is 3 seconds.
  45. ReadTimeout time.Duration
  46. // Timeout for socket writes. If reached, commands will fail
  47. // with a timeout instead of blocking.
  48. // Default is ReadTimeout.
  49. WriteTimeout time.Duration
  50. // Maximum number of socket connections.
  51. // Default is 10 connections per every CPU as reported by runtime.NumCPU.
  52. PoolSize int
  53. // Minimum number of idle connections which is useful when establishing
  54. // new connection is slow.
  55. MinIdleConns int
  56. // Connection age at which client retires (closes) the connection.
  57. // Default is to not close aged connections.
  58. MaxConnAge time.Duration
  59. // Amount of time client waits for connection if all connections
  60. // are busy before returning an error.
  61. // Default is ReadTimeout + 1 second.
  62. PoolTimeout time.Duration
  63. // Amount of time after which client closes idle connections.
  64. // Should be less than server's timeout.
  65. // Default is 5 minutes. -1 disables idle timeout check.
  66. IdleTimeout time.Duration
  67. // Frequency of idle checks made by idle connections reaper.
  68. // Default is 1 minute. -1 disables idle connections reaper,
  69. // but idle connections are still discarded by the client
  70. // if IdleTimeout is set.
  71. IdleCheckFrequency time.Duration
  72. // Enables read only queries on slave nodes.
  73. readOnly bool
  74. // TLS Config to use. When set TLS will be negotiated.
  75. TLSConfig *tls.Config
  76. }
  77. func (opt *Options) init() {
  78. if opt.Network == "" {
  79. opt.Network = "tcp"
  80. }
  81. if opt.Dialer == nil {
  82. opt.Dialer = func() (net.Conn, error) {
  83. netDialer := &net.Dialer{
  84. Timeout: opt.DialTimeout,
  85. KeepAlive: 5 * time.Minute,
  86. }
  87. if opt.TLSConfig == nil {
  88. return netDialer.Dial(opt.Network, opt.Addr)
  89. } else {
  90. return tls.DialWithDialer(netDialer, opt.Network, opt.Addr, opt.TLSConfig)
  91. }
  92. }
  93. }
  94. if opt.PoolSize == 0 {
  95. opt.PoolSize = 10 * runtime.NumCPU()
  96. }
  97. if opt.DialTimeout == 0 {
  98. opt.DialTimeout = 5 * time.Second
  99. }
  100. switch opt.ReadTimeout {
  101. case -1:
  102. opt.ReadTimeout = 0
  103. case 0:
  104. opt.ReadTimeout = 3 * time.Second
  105. }
  106. switch opt.WriteTimeout {
  107. case -1:
  108. opt.WriteTimeout = 0
  109. case 0:
  110. opt.WriteTimeout = opt.ReadTimeout
  111. }
  112. if opt.PoolTimeout == 0 {
  113. opt.PoolTimeout = opt.ReadTimeout + time.Second
  114. }
  115. if opt.IdleTimeout == 0 {
  116. opt.IdleTimeout = 5 * time.Minute
  117. }
  118. if opt.IdleCheckFrequency == 0 {
  119. opt.IdleCheckFrequency = time.Minute
  120. }
  121. switch opt.MinRetryBackoff {
  122. case -1:
  123. opt.MinRetryBackoff = 0
  124. case 0:
  125. opt.MinRetryBackoff = 8 * time.Millisecond
  126. }
  127. switch opt.MaxRetryBackoff {
  128. case -1:
  129. opt.MaxRetryBackoff = 0
  130. case 0:
  131. opt.MaxRetryBackoff = 512 * time.Millisecond
  132. }
  133. }
  134. // ParseURL parses an URL into Options that can be used to connect to Redis.
  135. func ParseURL(redisURL string) (*Options, error) {
  136. o := &Options{Network: "tcp"}
  137. u, err := url.Parse(redisURL)
  138. if err != nil {
  139. return nil, err
  140. }
  141. if u.Scheme != "redis" && u.Scheme != "rediss" {
  142. return nil, errors.New("invalid redis URL scheme: " + u.Scheme)
  143. }
  144. if u.User != nil {
  145. if p, ok := u.User.Password(); ok {
  146. o.Password = p
  147. }
  148. }
  149. if len(u.Query()) > 0 {
  150. return nil, errors.New("no options supported")
  151. }
  152. h, p, err := net.SplitHostPort(u.Host)
  153. if err != nil {
  154. h = u.Host
  155. }
  156. if h == "" {
  157. h = "localhost"
  158. }
  159. if p == "" {
  160. p = "6379"
  161. }
  162. o.Addr = net.JoinHostPort(h, p)
  163. f := strings.FieldsFunc(u.Path, func(r rune) bool {
  164. return r == '/'
  165. })
  166. switch len(f) {
  167. case 0:
  168. o.DB = 0
  169. case 1:
  170. if o.DB, err = strconv.Atoi(f[0]); err != nil {
  171. return nil, fmt.Errorf("invalid redis database number: %q", f[0])
  172. }
  173. default:
  174. return nil, errors.New("invalid redis URL path: " + u.Path)
  175. }
  176. if u.Scheme == "rediss" {
  177. o.TLSConfig = &tls.Config{ServerName: h}
  178. }
  179. return o, nil
  180. }
  181. func newConnPool(opt *Options) *pool.ConnPool {
  182. return pool.NewConnPool(&pool.Options{
  183. Dialer: opt.Dialer,
  184. PoolSize: opt.PoolSize,
  185. MinIdleConns: opt.MinIdleConns,
  186. MaxConnAge: opt.MaxConnAge,
  187. PoolTimeout: opt.PoolTimeout,
  188. IdleTimeout: opt.IdleTimeout,
  189. IdleCheckFrequency: opt.IdleCheckFrequency,
  190. })
  191. }