Skip to content

Improve performance by using MULTI and MGET commands #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading