summaryrefslogtreecommitdiff
path: root/plugins/redis/clients.go
blob: d0a184d2fb82b4ec95b1cfb39b8f643f18a01b88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package redis

import (
	"github.com/go-redis/redis/v8"
	"github.com/spiral/errors"
)

// RedisClient return a client based on the provided section key
// key sample: kv.some-section.redis
// kv.redis
// redis (root)
func (p *Plugin) RedisClient(key string) (redis.UniversalClient, error) {
	const op = errors.Op("redis_get_client")

	if !p.cfgPlugin.Has(key) {
		return nil, errors.E(op, errors.Errorf("no such section: %s", key))
	}

	cfg := &Config{}

	err := p.cfgPlugin.UnmarshalKey(key, cfg)
	if err != nil {
		return nil, errors.E(op, err)
	}

	cfg.InitDefaults()

	uc := redis.NewUniversalClient(&redis.UniversalOptions{
		Addrs:              cfg.Addrs,
		DB:                 cfg.DB,
		Username:           cfg.Username,
		Password:           cfg.Password,
		SentinelPassword:   cfg.SentinelPassword,
		MaxRetries:         cfg.MaxRetries,
		MinRetryBackoff:    cfg.MaxRetryBackoff,
		MaxRetryBackoff:    cfg.MaxRetryBackoff,
		DialTimeout:        cfg.DialTimeout,
		ReadTimeout:        cfg.ReadTimeout,
		WriteTimeout:       cfg.WriteTimeout,
		PoolSize:           cfg.PoolSize,
		MinIdleConns:       cfg.MinIdleConns,
		MaxConnAge:         cfg.MaxConnAge,
		PoolTimeout:        cfg.PoolTimeout,
		IdleTimeout:        cfg.IdleTimeout,
		IdleCheckFrequency: cfg.IdleCheckFreq,
		ReadOnly:           cfg.ReadOnly,
		RouteByLatency:     cfg.RouteByLatency,
		RouteRandomly:      cfg.RouteRandomly,
		MasterName:         cfg.MasterName,
	})

	return uc, nil
}

func (p *Plugin) DefaultClient() redis.UniversalClient {
	cfg := &Config{}
	cfg.InitDefaults()

	uc := redis.NewUniversalClient(&redis.UniversalOptions{
		Addrs:              cfg.Addrs,
		DB:                 cfg.DB,
		Username:           cfg.Username,
		Password:           cfg.Password,
		SentinelPassword:   cfg.SentinelPassword,
		MaxRetries:         cfg.MaxRetries,
		MinRetryBackoff:    cfg.MaxRetryBackoff,
		MaxRetryBackoff:    cfg.MaxRetryBackoff,
		DialTimeout:        cfg.DialTimeout,
		ReadTimeout:        cfg.ReadTimeout,
		WriteTimeout:       cfg.WriteTimeout,
		PoolSize:           cfg.PoolSize,
		MinIdleConns:       cfg.MinIdleConns,
		MaxConnAge:         cfg.MaxConnAge,
		PoolTimeout:        cfg.PoolTimeout,
		IdleTimeout:        cfg.IdleTimeout,
		IdleCheckFrequency: cfg.IdleCheckFreq,
		ReadOnly:           cfg.ReadOnly,
		RouteByLatency:     cfg.RouteByLatency,
		RouteRandomly:      cfg.RouteRandomly,
		MasterName:         cfg.MasterName,
	})

	return uc
}