sethvargo/go-limiter

Unexpected result when token limit is beyond 10000 * sqrt(10)

PaulLiang1 opened this issue · 1 comments

Snippet to reproduce

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/sethvargo/go-limiter/memorystore"
)

func main()  {
	s, _ := memorystore.New(&memorystore.Config{
		Tokens:        65535,
		Interval:      time.Second,
	})

	for i := 0; i < 20; i++{
		limit, remaining, _, _, _ := s.Take(context.Background(), "asd")
		fmt.Println("Run =", i, " limit = ", limit, " remaining = ", remaining)
		time.Sleep(100 * time.Millisecond)
	}

}

With this setting this will cause fillrate https://github.com/sethvargo/go-limiter/blob/main/memorystore/store.go#L278 to went below token limit, which will trigger the logic here https://github.com/sethvargo/go-limiter/blob/main/memorystore/store.go#L327-L330 causing remaining count to be incorrect.

Run = 0  limit =  65535  remaining =  65534
Run = 1  limit =  65535  remaining =  65533
Run = 2  limit =  65535  remaining =  65532
Run = 3  limit =  65535  remaining =  65531
Run = 4  limit =  65535  remaining =  65530
Run = 5  limit =  65535  remaining =  65529
Run = 6  limit =  65535  remaining =  65528
Run = 7  limit =  65535  remaining =  65527
Run = 8  limit =  65535  remaining =  65526
Run = 9  limit =  65535  remaining =  65525
Run = 10  limit =  65535  remaining =  15258  <----
Run = 11  limit =  65535  remaining =  15257
Run = 12  limit =  65535  remaining =  15256
Run = 13  limit =  65535  remaining =  15255
Run = 14  limit =  65535  remaining =  15254
Run = 15  limit =  65535  remaining =  15253
Run = 16  limit =  65535  remaining =  15252
Run = 17  limit =  65535  remaining =  15251
Run = 18  limit =  65535  remaining =  15250
Run = 19  limit =  65535  remaining =  15249

this behaviour can be triggered with 31622.776601 token/s settings
https://www.wolframalpha.com/input/?i=10000+sqrt%2810%29

This issue has been automatically locked since there has not been any
recent activity after it was closed. Please open a new issue for
related bugs.