Hi there,
I've never posted here, so apologies if I'm breaching any etiquette, but 
the contribution guide 
<https://github.com/golang/go/blob/3959d54c0bd5c92fe0a5e33fedb0595723efc23b/CONTRIBUTING.md>
 
suggests posting here before creating an issue so I thought I'd try.

I was debugging an issue recently and saw some strange behaviour in the 
x/time/rate package.

The behaviour is exhibited in the following test

func TestRateIssue(t *testing.T) {
l := NewLimiter(0, 1)
fmt.Println(l.Allow()) // should be true
fmt.Println(l.Allow()) // should be false

l.SetLimit(10)

time.Sleep(1 * time.Second)
fmt.Println(l.Allow()) // should be true but is false.
}

This code 
<https://github.com/monzo/wearedev/blob/6d94d20d4bf4afbb912a2e0f90bc81d36e303be4/vendor/golang.org/x/time/rate/rate.go#L348-L358>
 seems 
very strange to me. If the burst is 1, decrementing it here means that the 
limiter becomes unusable even if the limit is subsequently increased.

This code appeared here 
<https://go-review.googlesource.com/c/time/+/323429/2..11/rate/rate.go#b325> 
but 
the conversation doesn't really reflect the code: the comment says:

> The opposite in fact needs to happen: lim.tokens must be reduced by the n 
> consumed 
tokens?

Which makes sense to me, but I'm not sure how we ended up decrementing the 
burst. To me the solution to the reported problem (limiters not actually 
being full) would be to set lim.tokens on the constructor, but I haven't 
thought about that too deeply.

I'm happy to propose a change/create an issue but hopefully you folks can 
help me understand whether I'm missing something obvious!

Thanks

James

-- 


--

*This email is confidential and protected by copyright, and might 
contain privileged information. The same goes for any attachments.*
*
*
*If 
we’ve sent it to you by mistake (sorry), please don’t copy it or show it to 
anyone. You also shouldn’t use it to make a decision, and you shouldn’t 
rely on the contents.  Let the sender know as soon as you can, and then 
delete the email. Thank you!*



Monzo Bank Limited is a company registered 
in England and Wales (No. 09446231) registered at Broadwalk House, 5 Appold 
St, London, EC2A 2AG. Monzo Bank Ltd is authorised by the Prudential 
Regulation Authority (PRA) and regulated by the Financial Conduct Authority 
and the PRA. Our Financial Services Register number is 730427.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/51d35812-8903-4801-b4d5-0beb3129542en%40googlegroups.com.

Reply via email to