Skip to content
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

Redis backend with race check bug #29

Closed
oskarwojciski opened this issue Nov 28, 2023 · 2 comments
Closed

Redis backend with race check bug #29

oskarwojciski opened this issue Nov 28, 2023 · 2 comments

Comments

@oskarwojciski
Copy link
Contributor

oskarwojciski commented Nov 28, 2023

After the fix from #22 I found a bug:
With a small amount of Takes from the bucket, where Backend is a Redis with raceCheck=true then sometimes the expected version is higher than the actual one.

To replicate the issue I created a simple test:

func (s *LimitersTestSuite) TestTokenBucketRealClockRaceCheckRedisProblem() {
	ttl := time.Second
	limit := int64(2)
	refillEvery := time.Duration(int64(ttl) / limit)
	locker := l.NewLockNoop()
	backend := l.NewTokenBucketRedis(s.redisClient, uuid.New().String(), ttl, true)

	clk := l.NewSystemClock()
	ctx := context.Background()

	bucket := l.NewTokenBucket(
		limit,
		refillEvery,
		locker,
		backend,
		clk,
		nil,
	)

	for i := 0; i < 4; i++ {
		wait, err := bucket.Limit(ctx)
		if err != nil {
			assert.ErrorIs(s.T(), err, l.ErrLimitExhausted)
		}
		if wait.Seconds() > 0 {
			clk.Sleep(time.Second) // after a second all keys expired and the initial state was returned, but lastVersion is not cleared
		}
	}
}

checkResponseFromRedis returns error:

got [1 1 OK OK] from redis, expected [3 1 OK OK]
@oskarwojciski
Copy link
Contributor Author

I have created a PR with proposed solution for that: #30

@oskarwojciski
Copy link
Contributor Author

Resolved with #30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant