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
|
package cfg
import (
"context"
"fmt"
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
"github.com/mediocregopher/radix/v4"
)
// RadixClient is a single redis client which can be configured.
type RadixClient struct {
radix.Client
proto, addr string
poolSize int
}
// SetupCfg implement the cfg.Cfger interface.
func (c *RadixClient) SetupCfg(cfg *Cfg) {
cfg.StringVar(&c.proto, "redis-proto", "tcp", "Network protocol to connect to redis over, can be tcp or unix")
cfg.StringVar(&c.addr, "redis-addr", "127.0.0.1:6379", "Address redis is expected to listen on")
cfg.IntVar(&c.poolSize, "redis-pool-size", 5, "Number of connections in the redis pool to keep")
cfg.OnInit(func(ctx context.Context) error {
client, err := (radix.PoolConfig{
Size: c.poolSize,
}).New(
ctx, c.proto, c.addr,
)
if err != nil {
return fmt.Errorf(
"initializing redis pool of size %d at %s://%s: %w",
c.poolSize, c.proto, c.addr, err,
)
}
c.Client = client
return nil
})
}
// Annotate implements mctx.Annotator interface.
func (c *RadixClient) Annotate(a mctx.Annotations) {
a["redisProto"] = c.proto
a["redisAddr"] = c.addr
a["redisPoolSize"] = c.poolSize
}
// Close cleans up the radix client.
func (c *RadixClient) Close() error {
return c.Client.Close()
}
|