| 1 |
// Package redigostore offers Redis-based store implementation for throttled using redigo. |
| 2 |
package redigostore // import "github.com/throttled/throttled/store/redigostore" |
| 3 |
|
| 4 |
import ( |
| 5 |
"strings" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/gomodule/redigo/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 |
// RedigoStore implements a Redis-based store using redigo. |
| 27 |
type RedigoStore struct { |
| 28 |
pool *redis.Pool |
| 29 |
prefix string |
| 30 |
db int |
| 31 |
} |
| 32 |
|
| 33 |
// New creates a new Redis-based store, using the provided pool to get |
| 34 |
// its connections. The keys will have the specified keyPrefix, which |
| 35 |
// may be an empty string, and the database index specified by db will |
| 36 |
// be selected to store the keys. Any updating operations will reset |
| 37 |
// the key TTL to the provided value rounded down to the nearest |
| 38 |
// second. Depends on Redis 2.6+ for EVAL support. |
| 39 |
func New(pool *redis.Pool, keyPrefix string, db int) (*RedigoStore, error) { |
| 40 |
return &RedigoStore{ |
| 41 |
pool: pool, |
| 42 |
prefix: keyPrefix, |
| 43 |
db: db, |
| 44 |
}, nil |
| 45 |
} |
| 46 |
|
| 47 |
// GetWithTime returns the value of the key if it is in the store |
| 48 |
// or -1 if it does not exist. It also returns the current time at |
| 49 |
// the redis server to microsecond precision. |
| 50 |
func (r *RedigoStore) GetWithTime(key string) (int64, time.Time, error) { |
| 51 |
var now time.Time |
| 52 |
|
| 53 |
key = r.prefix + key |
| 54 |
|
| 55 |
conn, err := r.getConn() |
| 56 |
if err != nil { |
| 57 |
return 0, now, err |
| 58 |
} |
| 59 |
defer conn.Close() |
| 60 |
|
| 61 |
conn.Send("TIME") |
| 62 |
conn.Send("GET", key) |
| 63 |
conn.Flush() |
| 64 |
timeReply, err := redis.Values(conn.Receive()) |
| 65 |
if err != nil { |
| 66 |
return 0, now, err |
| 67 |
} |
| 68 |
|
| 69 |
var s, us int64 |
| 70 |
if _, err := redis.Scan(timeReply, &s, &us); err != nil { |
| 71 |
return 0, now, err |
| 72 |
} |
| 73 |
now = time.Unix(s, us*int64(time.Microsecond)) |
| 74 |
|
| 75 |
v, err := redis.Int64(conn.Receive()) |
| 76 |
if err == redis.ErrNil { |
| 77 |
return -1, now, nil |
| 78 |
} else if err != nil { |
| 79 |
return 0, now, err |
| 80 |
} |
| 81 |
|
| 82 |
return v, now, nil |
| 83 |
} |
| 84 |
|
| 85 |
// SetIfNotExistsWithTTL sets the value of key only if it is not |
| 86 |
// already set in the store it returns whether a new value was set. |
| 87 |
// If a new value was set, the ttl in the key is also set, though this |
| 88 |
// operation is not performed atomically. |
| 89 |
func (r *RedigoStore) SetIfNotExistsWithTTL(key string, value int64, ttl time.Duration) (bool, error) { |
| 90 |
key = r.prefix + key |
| 91 |
|
| 92 |
conn, err := r.getConn() |
| 93 |
if err != nil { |
| 94 |
return false, err |
| 95 |
} |
| 96 |
defer conn.Close() |
| 97 |
|
| 98 |
v, err := redis.Int64(conn.Do("SETNX", key, value)) |
| 99 |
if err != nil { |
| 100 |
return false, err |
| 101 |
} |
| 102 |
|
| 103 |
updated := v == 1 |
| 104 |
|
| 105 |
ttlSeconds := int(ttl.Seconds()) |
| 106 |
|
| 107 |
// An `EXPIRE 0` will delete the key immediately, so make sure that we set |
| 108 |
// expiry for a minimum of one second out so that our results stay in the |
| 109 |
// store. |
| 110 |
if ttlSeconds < 1 { |
| 111 |
ttlSeconds = 1 |
| 112 |
} |
| 113 |
|
| 114 |
if _, err := conn.Do("EXPIRE", key, ttlSeconds); err != nil { |
| 115 |
return updated, err |
| 116 |
} |
| 117 |
|
| 118 |
return updated, nil |
| 119 |
} |
| 120 |
|
| 121 |
// CompareAndSwapWithTTL atomically compares the value at key to the |
| 122 |
// old value. If it matches, it sets it to the new value and returns |
| 123 |
// true. Otherwise, it returns false. If the key does not exist in the |
| 124 |
// store, it returns false with no error. If the swap succeeds, the |
| 125 |
// ttl for the key is updated atomically. |
| 126 |
func (r *RedigoStore) CompareAndSwapWithTTL(key string, old, new int64, ttl time.Duration) (bool, error) { |
| 127 |
key = r.prefix + key |
| 128 |
conn, err := r.getConn() |
| 129 |
if err != nil { |
| 130 |
return false, err |
| 131 |
} |
| 132 |
defer conn.Close() |
| 133 |
|
| 134 |
ttlSeconds := int(ttl.Seconds()) |
| 135 |
|
| 136 |
// An `EXPIRE 0` will delete the key immediately, so make sure that we set |
| 137 |
// expiry for a minimum of one second out so that our results stay in the |
| 138 |
// store. |
| 139 |
if ttlSeconds < 1 { |
| 140 |
ttlSeconds = 1 |
| 141 |
} |
| 142 |
|
| 143 |
swapped, err := redis.Bool(conn.Do("EVAL", redisCASScript, 1, key, old, new, ttlSeconds)) |
| 144 |
if err != nil { |
| 145 |
if strings.Contains(err.Error(), redisCASMissingKey) { |
| 146 |
return false, nil |
| 147 |
} |
| 148 |
|
| 149 |
return false, err |
| 150 |
} |
| 151 |
|
| 152 |
return swapped, nil |
| 153 |
} |
| 154 |
|
| 155 |
// Select the specified database index. |
| 156 |
func (r *RedigoStore) getConn() (redis.Conn, error) { |
| 157 |
conn := r.pool.Get() |
| 158 |
|
| 159 |
// Select the specified database |
| 160 |
if r.db > 0 { |
| 161 |
if _, err := redis.String(conn.Do("SELECT", r.db)); err != nil { |
| 162 |
conn.Close() |
| 163 |
return nil, err |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return conn, nil |
| 168 |
} |