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

panic when setting rate to lower than the number of tokens used in the last interval #2

Open
Kimbsen opened this issue Apr 15, 2021 · 0 comments

Comments

@Kimbsen
Copy link

Kimbsen commented Apr 15, 2021

Lets say you have a bucket with rateopts: interval=1s rate=100
You read 50bytes.
Then within the same interval change the rate to say 10
the next call to bucket.insert will return a value of -40 which will cause a panic slice bounds of range in Reader.Read

package main

import (
	"bytes"
	"time"

	"github.com/ryanuber/iocap"
)

func main() {

	bucket := iocap.NewGroup(iocap.RateOpts{
		Interval: time.Second,
		Size:     100,
	})
	r := bucket.NewReader(bytes.NewReader(make([]byte, 1024)))
	p := make([]byte, 50)
	r.Read(p)
	bucket.SetRate(iocap.RateOpts{
		Interval: time.Second,
		Size:     10,
	})
	r.Read(p)
}

=> panic: runtime error: slice bounds out of range [:-40]

I have two solutions to this.

  • Either restart the interval on bucket.setRate.
  • Or in Reader.Read just return when v := b.insert() is less than 0.

I can pull request either. Which would you prefer?

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