|
1 | 1 | package redis
|
2 | 2 |
|
3 |
| -import "time" |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "net" |
| 6 | + "time" |
4 | 7 |
|
5 |
| -// Config Redis Configuration |
| 8 | + "github.com/go-redis/redis" |
| 9 | +) |
| 10 | + |
| 11 | +// Config Redis parameter options |
6 | 12 | type Config struct {
|
7 | 13 | // The network type, either tcp or unix.
|
8 | 14 | // Default is tcp.
|
9 | 15 | Network string
|
10 | 16 | // host:port address.
|
11 | 17 | Addr string
|
12 | 18 |
|
13 |
| - // An optional password. Must match the password specified in the |
| 19 | + // Dialer creates new network connection and has priority over |
| 20 | + // Network and Addr options. |
| 21 | + Dialer func() (net.Conn, error) |
| 22 | + |
| 23 | + // Optional password. Must match the password specified in the |
14 | 24 | // requirepass server configuration option.
|
15 | 25 | Password string
|
16 |
| - // A database to be selected after connecting to server. |
| 26 | + // Database to be selected after connecting to the server. |
17 | 27 | DB int
|
18 | 28 |
|
19 |
| - // The maximum number of retries before giving up. |
| 29 | + // Maximum number of retries before giving up. |
20 | 30 | // Default is to not retry failed commands.
|
21 | 31 | MaxRetries int
|
| 32 | + // Minimum backoff between each retry. |
| 33 | + // Default is 8 milliseconds; -1 disables backoff. |
| 34 | + MinRetryBackoff time.Duration |
| 35 | + // Maximum backoff between each retry. |
| 36 | + // Default is 512 milliseconds; -1 disables backoff. |
| 37 | + MaxRetryBackoff time.Duration |
22 | 38 |
|
23 |
| - // Sets the deadline for establishing new connections. If reached, |
24 |
| - // dial will fail with a timeout. |
| 39 | + // Dial timeout for establishing new connections. |
25 | 40 | // Default is 5 seconds.
|
26 | 41 | DialTimeout time.Duration
|
27 |
| - // Sets the deadline for socket reads. If reached, commands will |
28 |
| - // fail with a timeout instead of blocking. |
| 42 | + // Timeout for socket reads. If reached, commands will fail |
| 43 | + // with a timeout instead of blocking. |
| 44 | + // Default is 3 seconds. |
29 | 45 | ReadTimeout time.Duration
|
30 |
| - // Sets the deadline for socket writes. If reached, commands will |
31 |
| - // fail with a timeout instead of blocking. |
| 46 | + // Timeout for socket writes. If reached, commands will fail |
| 47 | + // with a timeout instead of blocking. |
| 48 | + // Default is ReadTimeout. |
32 | 49 | WriteTimeout time.Duration
|
33 | 50 |
|
34 |
| - // The maximum number of socket connections. |
35 |
| - // Default is 10 connections. |
| 51 | + // Maximum number of socket connections. |
| 52 | + // Default is 10 connections per every CPU as reported by runtime.NumCPU. |
36 | 53 | PoolSize int
|
37 |
| - // Specifies amount of time client waits for connection if all |
38 |
| - // connections are busy before returning an error. |
39 |
| - // Default is 1 second. |
| 54 | + // Amount of time client waits for connection if all connections |
| 55 | + // are busy before returning an error. |
| 56 | + // Default is ReadTimeout + 1 second. |
40 | 57 | PoolTimeout time.Duration
|
| 58 | + // Amount of time after which client closes idle connections. |
| 59 | + // Should be less than server's timeout. |
| 60 | + // Default is 5 minutes. |
| 61 | + IdleTimeout time.Duration |
| 62 | + // Frequency of idle checks. |
| 63 | + // Default is 1 minute. |
| 64 | + // When minus value is set, then idle check is disabled. |
| 65 | + IdleCheckFrequency time.Duration |
| 66 | + |
| 67 | + // TLS Config to use. When set TLS will be negotiated. |
| 68 | + TLSConfig *tls.Config |
| 69 | +} |
| 70 | + |
| 71 | +func (o *Config) redisOptions() *redis.Options { |
| 72 | + return &redis.Options{ |
| 73 | + Network: o.Network, |
| 74 | + Addr: o.Addr, |
| 75 | + Dialer: o.Dialer, |
| 76 | + Password: o.Password, |
| 77 | + DB: o.DB, |
| 78 | + MaxRetries: o.MaxRetries, |
| 79 | + MinRetryBackoff: o.MinRetryBackoff, |
| 80 | + MaxRetryBackoff: o.MaxRetryBackoff, |
| 81 | + DialTimeout: o.DialTimeout, |
| 82 | + ReadTimeout: o.ReadTimeout, |
| 83 | + WriteTimeout: o.WriteTimeout, |
| 84 | + PoolSize: o.PoolSize, |
| 85 | + PoolTimeout: o.PoolTimeout, |
| 86 | + IdleTimeout: o.IdleTimeout, |
| 87 | + IdleCheckFrequency: o.IdleCheckFrequency, |
| 88 | + TLSConfig: o.TLSConfig, |
| 89 | + } |
41 | 90 | }
|
0 commit comments