Skip to content

Commit

Permalink
Improve performance by using MULTI and MGET commands (#13)
Browse files Browse the repository at this point in the history
* Run ICNRBY + EXPIRE in a single atomic transaction

* Use MGET to fetch both counters at once
  • Loading branch information
VojtechVitek authored Jul 25, 2024
1 parent 8e2fdd6 commit 71c932d
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions httprateredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"time"

"github.com/go-chi/httprate"
Expand Down Expand Up @@ -102,20 +103,20 @@ func (c *redisCounter) IncrementBy(key string, currentWindow time.Time, amount i

hkey := c.limitCounterKey(key, currentWindow)

cmd := conn.Do(ctx, "INCRBY", hkey, amount)
if cmd == nil {
return fmt.Errorf("httprateredis: redis incr failed")
}
if err := cmd.Err(); err != nil {
return err
pipe := conn.TxPipeline()
incrCmd := pipe.IncrBy(ctx, hkey, int64(amount))
expireCmd := pipe.Expire(ctx, hkey, c.windowLength*3)
_, err := pipe.Exec(ctx)
if err != nil {
return fmt.Errorf("httprateredis: redis transaction failed: %w", err)
}

cmd = conn.Do(ctx, "EXPIRE", hkey, c.windowLength.Seconds()*3)
if cmd == nil {
return fmt.Errorf("httprateredis: redis expire failed")
if err := incrCmd.Err(); err != nil {
return fmt.Errorf("httprateredis: redis incr failed: %w", err)
}
if err := cmd.Err(); err != nil {
return err

if err := expireCmd.Err(); err != nil {
return fmt.Errorf("httprateredis: redis expire failed: %w", err)
}

return nil
Expand All @@ -125,32 +126,27 @@ func (c *redisCounter) Get(key string, currentWindow, previousWindow time.Time)
ctx := context.Background()
conn := c.client

cmd := conn.Do(ctx, "GET", c.limitCounterKey(key, currentWindow))
if cmd == nil {
return 0, 0, fmt.Errorf("httprateredis: redis get curr failed")
}
if err := cmd.Err(); err != nil && err != redis.Nil {
return 0, 0, fmt.Errorf("httprateredis: redis get curr failed: %w", err)
}
currKey := c.limitCounterKey(key, currentWindow)
prevKey := c.limitCounterKey(key, previousWindow)

curr, err := cmd.Int()
if err != nil && err != redis.Nil {
return 0, 0, fmt.Errorf("httprateredis: redis int curr value: %w", err)
values, err := conn.MGet(ctx, currKey, prevKey).Result()
if err != nil {
return 0, 0, fmt.Errorf("httprateredis: redis mget failed: %w", err)
} else if len(values) != 2 {
return 0, 0, fmt.Errorf("httprateredis: redis mget returned wrong number of keys: %v, expected 2", len(values))
}

cmd = conn.Do(ctx, "GET", c.limitCounterKey(key, previousWindow))
if cmd == nil {
return 0, 0, fmt.Errorf("httprateredis: redis get prev failed")
}
var curr, prev int

if err := cmd.Err(); err != nil && err != redis.Nil {
return 0, 0, fmt.Errorf("httprateredis: redis get prev failed: %w", err)
// MGET always returns slice with nil or "string" values, even if the values
// were created with the INCR command. Ignore error if we can't parse the number.
if values[0] != nil {
v, _ := values[0].(string)
curr, _ = strconv.Atoi(v)
}

var prev int
prev, err = cmd.Int()
if err != nil && err != redis.Nil {
return 0, 0, fmt.Errorf("httprateredis: redis int prev value: %w", err)
if values[1] != nil {
v, _ := values[1].(string)
prev, _ = strconv.Atoi(v)
}

return curr, prev, nil
Expand Down

0 comments on commit 71c932d

Please sign in to comment.