Skip to content

Commit

Permalink
Always expect version to be in initial state in case key was removed (#…
Browse files Browse the repository at this point in the history
…42)

* Always expect version to be in initial state in case key was removed
  • Loading branch information
radandrei authored May 17, 2024
1 parent fdeb965 commit 6fc2031
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions leakybucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,14 @@ func (t *LeakyBucketRedis) State(ctx context.Context) (LeakyBucketState, error)
}, nil
}

func checkResponseFromRedis(response interface{}, expected interface{}) error {
func checkResponseFromRedis(response interface{}, expected interface{}, expectedIfPreviousVersionExpired interface{}) error {
if s, sok := response.(string); sok && s == "RACE_CONDITION" {
return ErrRaceCondition
}
if !reflect.DeepEqual(response, expected) {
return errors.Errorf("got %+v from redis, expected %+v", response, expected)
if !reflect.DeepEqual(response, expected) && !reflect.DeepEqual(response, expectedIfPreviousVersionExpired) {
return errors.Errorf("got %+v from redis, expected %+v or %+v", response, expected, expectedIfPreviousVersionExpired)
}

return nil
}

Expand Down Expand Up @@ -385,7 +386,7 @@ func (t *LeakyBucketRedis) SetState(ctx context.Context, state LeakyBucketState)
int64(t.ttl/time.Microsecond)).Result()

if err == nil {
err = checkResponseFromRedis(result, []interface{}{t.lastVersion + 1, int64(1), "OK"})
err = checkResponseFromRedis(result, []interface{}{t.lastVersion + 1, int64(1), "OK"}, []interface{}{int64(1), int64(1), "OK"})
}
}()

Expand Down
2 changes: 1 addition & 1 deletion tokenbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ func (t *TokenBucketRedis) SetState(ctx context.Context, state TokenBucketState)
int64(t.ttl/time.Millisecond)).Result()

if err == nil {
err = checkResponseFromRedis(result, []interface{}{t.lastVersion + 1, int64(1), "OK", "OK"})
err = checkResponseFromRedis(result, []interface{}{t.lastVersion + 1, int64(1), "OK", "OK"}, []interface{}{int64(1), int64(1), "OK", "OK"})
}
}()

Expand Down

0 comments on commit 6fc2031

Please sign in to comment.