Allow use of existing Redis client for checkpoint store (#96)

This commit is contained in:
Patrick Moore 2019-08-05 15:04:27 -07:00 committed by Harlow Ward
parent 35c48ef1c9
commit 81a8ac4221
3 changed files with 57 additions and 10 deletions

13
store/redis/options.go Normal file
View file

@ -0,0 +1,13 @@
package redis
import redis "github.com/go-redis/redis"
// Option is used to override defaults when creating a new Redis checkpoint
type Option func(*Checkpoint)
// WithClient overrides the default client
func WithClient(client *redis.Client) Option {
return func(c *Checkpoint) {
c.client = client
}
}

View file

@ -10,24 +10,38 @@ import (
const localhost = "127.0.0.1:6379"
// New returns a checkpoint that uses Redis for underlying storage
func New(appName string) (*Checkpoint, error) {
func New(appName string, opts ...Option) (*Checkpoint, error) {
if appName == "" {
return nil, fmt.Errorf("must provide app name")
}
c := &Checkpoint{
appName: appName,
}
// override defaults
for _, opt := range opts {
opt(c)
}
// default client if none provided
if c.client == nil {
addr := os.Getenv("REDIS_URL")
if addr == "" {
addr = localhost
}
client := redis.NewClient(&redis.Options{Addr: addr})
c.client = client
}
// verify we can ping server
_, err := client.Ping().Result()
_, err := c.client.Ping().Result()
if err != nil {
return nil, err
}
return &Checkpoint{
appName: appName,
client: client,
}, nil
return c, nil
}
// Checkpoint stores and retreives the last evaluated key from a DDB scan

View file

@ -2,8 +2,28 @@ package redis
import (
"testing"
"github.com/alicebob/miniredis"
redis "github.com/go-redis/redis"
)
func Test_CheckpointOptions(t *testing.T) {
s, err := miniredis.Run()
if err != nil {
panic(err)
}
defer s.Close()
client := redis.NewClient(&redis.Options{
Addr: s.Addr(),
})
_, err = New("app", WithClient(client))
if err != nil {
t.Fatalf("new checkpoint error: %v", err)
}
}
func Test_CheckpointLifecycle(t *testing.T) {
// new
c, err := New("app")