Allow use of existing Redis client for checkpoint store (#96)
This commit is contained in:
parent
35c48ef1c9
commit
81a8ac4221
3 changed files with 57 additions and 10 deletions
13
store/redis/options.go
Normal file
13
store/redis/options.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,24 +10,38 @@ import (
|
||||||
const localhost = "127.0.0.1:6379"
|
const localhost = "127.0.0.1:6379"
|
||||||
|
|
||||||
// New returns a checkpoint that uses Redis for underlying storage
|
// 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")
|
addr := os.Getenv("REDIS_URL")
|
||||||
if addr == "" {
|
if addr == "" {
|
||||||
addr = localhost
|
addr = localhost
|
||||||
}
|
}
|
||||||
|
|
||||||
client := redis.NewClient(&redis.Options{Addr: addr})
|
client := redis.NewClient(&redis.Options{Addr: addr})
|
||||||
|
c.client = client
|
||||||
|
}
|
||||||
|
|
||||||
// verify we can ping server
|
// verify we can ping server
|
||||||
_, err := client.Ping().Result()
|
_, err := c.client.Ping().Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Checkpoint{
|
return c, nil
|
||||||
appName: appName,
|
|
||||||
client: client,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checkpoint stores and retreives the last evaluated key from a DDB scan
|
// Checkpoint stores and retreives the last evaluated key from a DDB scan
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,28 @@ package redis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"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) {
|
func Test_CheckpointLifecycle(t *testing.T) {
|
||||||
// new
|
// new
|
||||||
c, err := New("app")
|
c, err := New("app")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue