Skip to content

Commit

Permalink
Rajeev - Add redis MULTI to attain atomicity in redis ops
Browse files Browse the repository at this point in the history
  • Loading branch information
rShetty committed Apr 29, 2017
1 parent bfcd4bd commit 067c05b
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions ratelimit/ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ func (rl *RateLimit) Reset(key string) error {
}

func initializeCounterForKey(key string, conn redis.Conn, ttl int) error {
if _, err := conn.Do("SET", key, 1); err != nil {
return err
}
conn.Send("MULTI")
conn.Send("SET", key, 1)
conn.Send("EXPIRE", key, ttl)

if _, err := conn.Do("EXPIRE", key, ttl); err != nil {
_, err := conn.Do("EXEC")
if err != nil {
return err
}

Expand All @@ -107,11 +108,12 @@ func incrementCounterForKey(key string, conn redis.Conn) error {
}

func initializeCooldownWindowForKey(key string, conn redis.Conn, ttl int) error {
if _, err := conn.Do("INCR", key); err != nil {
return err
}
conn.Send("MULTI")
conn.Send("INCR", key)
conn.Send("EXPIRE", key, ttl)

if _, err := conn.Do("EXPIRE", key, ttl); err != nil {
_, err := conn.Do("EXEC")
if err != nil {
return err
}

Expand Down

0 comments on commit 067c05b

Please sign in to comment.