diff options
-rw-r--r-- | srv/src/cfg/radix_client.go | 55 | ||||
-rw-r--r-- | srv/src/chat/chat_it_test.go | 21 | ||||
-rw-r--r-- | srv/src/cmd/mediocre-blog/main.go | 27 |
3 files changed, 77 insertions, 26 deletions
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() +} diff --git a/srv/src/chat/chat_it_test.go b/srv/src/chat/chat_it_test.go index 1767d88..b0d4431 100644 --- a/srv/src/chat/chat_it_test.go +++ b/srv/src/chat/chat_it_test.go @@ -1,4 +1,5 @@ //go:build integration +// +build integration package chat @@ -9,6 +10,7 @@ import ( "time" "github.com/google/uuid" + cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg" "github.com/mediocregopher/mediocre-go-lib/v2/mlog" "github.com/mediocregopher/radix/v4" "github.com/stretchr/testify/assert" @@ -44,16 +46,25 @@ func (h *roomTestHarness) newMsg(t *testing.T) Message { } func newRoomTestHarness(t *testing.T) *roomTestHarness { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) t.Cleanup(cancel) - redis, err := radix.Dial(ctx, "tcp", "127.0.0.1:6379") - assert.NoError(t, err) - t.Cleanup(func() { redis.Close() }) + cfg := cfgpkg.NewBlogCfg(cfgpkg.Params{ + Args: []string{}, // prevents the test process args from interfering + }) + + var radixClient cfgpkg.RadixClient + radixClient.SetupCfg(cfg) + t.Cleanup(func() { radixClient.Close() }) + + if err := cfg.Init(ctx); err != nil { + t.Fatal(err) + } roomParams := RoomParams{ Logger: mlog.NewLogger(nil), - Redis: redis, + Redis: radixClient.Client, ID: uuid.New().String(), MaxMessages: roomTestHarnessMaxMsgs, } @@ -63,7 +74,7 @@ func newRoomTestHarness(t *testing.T) *roomTestHarness { assert.NoError(t, err) t.Cleanup(func() { - err := redis.Do(context.Background(), radix.Cmd( + err := radixClient.Client.Do(context.Background(), radix.Cmd( nil, "DEL", roomParams.streamKey(), )) assert.NoError(t, err) diff --git a/srv/src/cmd/mediocre-blog/main.go b/srv/src/cmd/mediocre-blog/main.go index f12f4af..69ee7b6 100644 --- a/srv/src/cmd/mediocre-blog/main.go +++ b/srv/src/cmd/mediocre-blog/main.go @@ -8,14 +8,12 @@ import ( "time" "github.com/mediocregopher/blog.mediocregopher.com/srv/api" - "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg" cfgpkg "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg" "github.com/mediocregopher/blog.mediocregopher.com/srv/chat" "github.com/mediocregopher/blog.mediocregopher.com/srv/mailinglist" "github.com/mediocregopher/blog.mediocregopher.com/srv/pow" "github.com/mediocregopher/mediocre-go-lib/v2/mctx" "github.com/mediocregopher/mediocre-go-lib/v2/mlog" - "github.com/mediocregopher/radix/v4" "github.com/tilinna/clock" ) @@ -23,7 +21,7 @@ func main() { ctx := context.Background() - cfg := cfg.NewBlogCfg(cfg.Params{}) + cfg := cfgpkg.NewBlogCfg(cfgpkg.Params{}) var dataDir cfgpkg.DataDir dataDir.SetupCfg(cfg) @@ -46,9 +44,10 @@ func main() { apiParams.SetupCfg(cfg) ctx = mctx.WithAnnotator(ctx, &apiParams) - redisProto := cfg.String("redis-proto", "tcp", "Network protocol to connect to redis over, can be tcp or unix") - redisAddr := cfg.String("redis-addr", "127.0.0.1:6379", "Address redis is expected to listen on") - redisPoolSize := cfg.Int("redis-pool-size", 5, "Number of connections in the redis pool to keep") + var radixClient cfgpkg.RadixClient + radixClient.SetupCfg(cfg) + defer radixClient.Close() + ctx = mctx.WithAnnotator(ctx, &radixClient) chatGlobalRoomMaxMsgs := cfg.Int("chat-global-room-max-messages", 1000, "Maximum number of messages the global chat room can retain") chatUserIDCalcSecret := cfg.String("chat-user-id-calc-secret", "", "Secret to use when calculating user ids") @@ -67,9 +66,6 @@ func main() { } ctx = mctx.Annotate(ctx, - "redisProto", *redisProto, - "redisAddr", *redisAddr, - "redisPoolSize", *redisPoolSize, "chatGlobalRoomMaxMsgs", *chatGlobalRoomMaxMsgs, ) @@ -103,20 +99,9 @@ func main() { ml := mailinglist.New(mlParams) - redis, err := (radix.PoolConfig{ - Size: *redisPoolSize, - }).New( - ctx, *redisProto, *redisAddr, - ) - - if err != nil { - logger.Fatal(ctx, "initializing redis pool", err) - } - defer redis.Close() - chatGlobalRoom, err := chat.NewRoom(ctx, chat.RoomParams{ Logger: logger.WithNamespace("global-chat-room"), - Redis: redis, + Redis: radixClient.Client, ID: "global", MaxMessages: *chatGlobalRoomMaxMsgs, }) |