| 1 |
// Package goredisstore offers Redis-based store implementation for throttled using go-redis. |
| 2 |
package goredisstore // import "github.com/throttled/throttled/store/goredisstore" |
| 3 |
|
| 4 |
import ( |
| 5 |
"strings" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/go-redis/redis" |
| 9 |
) |
| 10 |
|
| 11 |
const ( |
| 12 |
redisCASMissingKey = "key does not exist" |
| 13 |
redisCASScript = ` |
| 14 |
local v = redis.call('get', KEYS[1]) |
| 15 |
if v == false then |
| 16 |
return redis.error_reply("key does not exist") |
| 17 |
end |
| 18 |
if v ~= ARGV[1] then |
| 19 |
return 0 |
| 20 |
end |
| 21 |
redis.call('setex', KEYS[1], ARGV[3], ARGV[2]) |
| 22 |
return 1 |
| 23 |
` |
| 24 |
) |
| 25 |
|
| 26 |
// GoRedisStore implements a Redis-based store using go-redis. |
| 27 |
type GoRedisStore struct { |
| 28 |
client *redis.Client |
| 29 |
prefix string |
| 30 |
} |
| 31 |
|
| 32 |
// New creates a new Redis-based store, using the provided pool to get |
| 33 |
// its connections. The keys will have the specified keyPrefix, which |
| 34 |
// may be an empty string, and the database index specified by db will |
| 35 |
// be selected to store the keys. Any updating operations will reset |
| 36 |
// the key TTL to the provided value rounded down to the nearest |
| 37 |
// second. Depends on Redis 2.6+ for EVAL support. |
| 38 |
func New(client *redis.Client, keyPrefix string) (*GoRedisStore, error) { |
| 39 |
return &GoRedisStore{ |
| 40 |
client: client, |
| 41 |
prefix: keyPrefix, |
| 42 |
}, nil |
| 43 |
} |
| 44 |
|
| 45 |
// GetWithTime returns the value of the key if it is in the store |
| 46 |
// or -1 if it does not exist. It also returns the current time at |
| 47 |
// the redis server to microsecond precision. |
| 48 |
func (r *GoRedisStore) GetWithTime(key string) (int64, time.Time, error) { |
| 49 |
key = r.prefix + key |
| 50 |
|
| 51 |
pipe := r.client.Pipeline() |
| 52 |
timeCmd := pipe.Time() |
| 53 |
getKeyCmd := pipe.Get(key) |
| 54 |
_, err := pipe.Exec() |
| 55 |
|
| 56 |
now, err := timeCmd.Result() |
| 57 |
if err != nil { |
| 58 |
return 0, now, err |
| 59 |
} |
| 60 |
|
| 61 |
v, err := getKeyCmd.Int64() |
| 62 |
if err == redis.Nil { |
| 63 |
return -1, now, nil |
| 64 |
} else if err != nil { |
| 65 |
return 0, now, err |
| 66 |
} |
| 67 |
|
| 68 |
return v, now, nil |
| 69 |
} |
| 70 |
|
| 71 |
// SetIfNotExistsWithTTL sets the value of key only if it is not |
| 72 |
// already set in the store it returns whether a new value was set. |
| 73 |
// If a new value was set, the ttl in the key is also set, though this |
| 74 |
// operation is not performed atomically. |
| 75 |
func (r *GoRedisStore) SetIfNotExistsWithTTL(key string, value int64, ttl time.Duration) (bool, error) { |
| 76 |
key = r.prefix + key |
| 77 |
|
| 78 |
updated, err := r.client.SetNX(key, value, 0).Result() |
| 79 |
if err != nil { |
| 80 |
return false, err |
| 81 |
} |
| 82 |
|
| 83 |
ttlSeconds := time.Duration(ttl.Seconds()) |
| 84 |
|
| 85 |
// An `EXPIRE 0` will delete the key immediately, so make sure that we set |
| 86 |
// expiry for a minimum of one second out so that our results stay in the |
| 87 |
// store. |
| 88 |
if ttlSeconds < 1 { |
| 89 |
ttlSeconds = 1 |
| 90 |
} |
| 91 |
|
| 92 |
err = r.client.Expire(key, ttlSeconds*time.Second).Err() |
| 93 |
return updated, err |
| 94 |
} |
| 95 |
|
| 96 |
// CompareAndSwapWithTTL atomically compares the value at key to the |
| 97 |
// old value. If it matches, it sets it to the new value and returns |
| 98 |
// true. Otherwise, it returns false. If the key does not exist in the |
| 99 |
// store, it returns false with no error. If the swap succeeds, the |
| 100 |
// ttl for the key is updated atomically. |
| 101 |
func (r *GoRedisStore) CompareAndSwapWithTTL(key string, old, new int64, ttl time.Duration) (bool, error) { |
| 102 |
key = r.prefix + key |
| 103 |
|
| 104 |
ttlSeconds := int(ttl.Seconds()) |
| 105 |
|
| 106 |
// An `EXPIRE 0` will delete the key immediately, so make sure that we set |
| 107 |
// expiry for a minimum of one second out so that our results stay in the |
| 108 |
// store. |
| 109 |
if ttlSeconds < 1 { |
| 110 |
ttlSeconds = 1 |
| 111 |
} |
| 112 |
|
| 113 |
// result will be 0 or 1 |
| 114 |
result, err := r.client.Eval(redisCASScript, []string{key}, old, new, ttlSeconds).Result() |
| 115 |
|
| 116 |
var swapped bool |
| 117 |
if s, ok := result.(int64); ok { |
| 118 |
swapped = s == 1 |
| 119 |
} // if not ok, zero value of swapped is false. |
| 120 |
|
| 121 |
if err != nil { |
| 122 |
if strings.Contains(err.Error(), redisCASMissingKey) { |
| 123 |
return false, nil |
| 124 |
} |
| 125 |
return false, err |
| 126 |
} |
| 127 |
|
| 128 |
return swapped, nil |
| 129 |
} |