-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_source.go
57 lines (47 loc) · 1.33 KB
/
redis_source.go
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
56
57
package gofourit
import (
"log"
"time"
"github.com/bsm/redislock"
)
// redisSource is our implementation for a Redis based RemoteSource.
type redisSource struct {
locker *redislock.Client
}
// TODO(ttacon): should support log options, e.g. for connection failures
// NewRedisRemoteSource returns a new RemoteSource based on the given redis
// client.
func NewRedisRemoteSource(client redislock.RedisClient) RemoteSource {
return &redisSource{
locker: redislock.New(client),
}
}
// TODO(ttacon): should support options to GrabLock, e.g.:
// - keepRenewingLock until released (this way we run a go routine renewing the
// lock until it is released)
// - TTL for lock
// - option key prefixes for namespacing
// GrabLock acquires a unique lock based on the given key.
func (r *redisSource) GrabLock(key string) Lock {
lock, err := r.locker.Obtain(key, 15*time.Second, nil)
if err == redislock.ErrNotObtained {
return nil
} else if err != nil {
// TODO(ttacon): replace with a call to the provided logger,
// if it exists.
log.Println(err)
return nil
}
return &redisLock{
lock: lock,
}
}
// redisLock is our wrapping implementation to allow us to release the
// generated redislock Lock.
type redisLock struct {
lock *redislock.Lock
}
// Release releases the given lock.
func (r *redisLock) Release() {
r.lock.Release()
}