From ddb126db1709b1c7679b8370b508639991387bf0 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sun, 8 May 2022 16:36:08 -0600 Subject: Move radix config into cfg, use that for integration tests --- srv/src/cfg/radix_client.go | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 srv/src/cfg/radix_client.go (limited to 'srv/src/cfg/radix_client.go') diff --git a/srv/src/cfg/radix_client.go b/srv/src/cfg/radix_client.go new file mode 100644 index 0000000..55ff5d7 --- /dev/null +++ b/srv/src/cfg/radix_client.go @@ -0,0 +1,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() +} -- cgit v1.2.3