ulule / limiter

Dead simple rate limit middleware for Go.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling Reset() on a non-existing key will throw a "retry limit exceeded" error in Redis

mikegleasonjr opened this issue · comments

// doResetValue will execute resetValue with a retry mecanism (optimistic locking) until store.MaxRetry is reached.
func (store *Store) doResetValue(rtx *libredis.Tx, key string) error {
	for i := 0; i < store.MaxRetry; i++ {
		err := resetValue(rtx, key)
		if err == nil {
			return nil
		}
	}
	return errors.New("retry limit exceeded")
}

// resetValue will try to reset the counter identified by given key.
func resetValue(rtx *libredis.Tx, key string) error {
	deletion := rtx.Del(key)

	count, err := deletion.Result()
	if err != nil {
		return err
	}
	if count != 1 {
		return errors.New("cannot delete key")   <------------
	}

	return nil

}

Because resetValue assumes a key will be present.

To test you can Get a key, flush redis, then try to delete the key.

I use a Redis LRU so sometimes a limiter will get evicted on its own.

Anyway Resetting an already blank limiter should not throw an error in my opinion.

Thanks

Hello @mikegleasonjr

I agree with you, resetting a non-existing key shouldn't return an error.
I'll try to fix that when I have some spare time. In the meantime, don't hesitate to submit a pull request 🙂

Thank you for this report 👍