GithubHelp home page GithubHelp logo

Comments (5)

bcap avatar bcap commented on July 24, 2024

Right now to have something like this that doesnt allow bursts to pass by, there is this poor man's option (and likely also naive) which is something managed outside the lib:

  1. have a single rate limiter for normal ops
  2. when an increase/decrease is requested, create a second rate limiter with the new value. We enter "swapping mode"
  3. Take now takes from both rate limiters in either serial or parallel fashion (not sure about which has better correctness here yet)
  4. When the window of the first (old) rate limiter is expired, remove it (we need to track windows here)
  5. We are back at having a single rate limiter
  6. The swapping mode is done, allowing more calls to increase/decrease again. Any calls to increase/decrease that were done while we were in swapping mode either got rejected, waiting (blocked) or executed later (async)

from ratelimit.

rabbbit avatar rabbbit commented on July 24, 2024

Generally seems plausible, but as I commented in the other task, I don't expect to have bandwidth to work on this soon.

However, reading your use-case, I'm not sure if updating limits on the fly would make sense - it seems like you want some kind of burst++ mode, rather than external limits?

Note to self: #69 was asking for the same feature.

from ratelimit.

shmilyoo avatar shmilyoo commented on July 24, 2024

you can wrap it, and use a block channel to stop the rate limit

func (rl *RateLimit) Take() time.Time {
	<-rl.blockSignal
	return rl.throttle.Take()
}

from ratelimit.

bruno-nascimento avatar bruno-nascimento commented on July 24, 2024

Hi, I know I must be missing something here 😓
Why can't we have something like this to achieve the same goal ?

type Limiter struct {
	limiter ratelimit.Limiter
	mutex   sync.RWMutex
}

func NewLimiter(cfg config) Limiter {
	return Limiter{limiter: ratelimit.New(cfg.LimiterOperations, ratelimit.Per(cfg.LimiterDuration))}
}

func (l *Limiter) Update(ops int, duration time.Duration) {
	l.mutex.Lock()
	defer l.mutex.Unlock()
	l.limiter = ratelimit.New(ops, ratelimit.Per(duration))
}

func (l *Limiter) Take() time.Time {
	l.mutex.RLock()
	defer l.mutex.RUnlock()
	return l.limiter.Take()
}

As far as I could see, we could update the limiter while calling limiter.Take() from different go routines without any issue

time.AfterFunc(time.Second*5, func() {
        limiter.Update(3, time.Second)
})

time.AfterFunc(time.Second*10, func() {
        limiter.Update(20, time.Second)
})

time.AfterFunc(time.Second*15, func() {
        limiter.Update(100, time.Second)
})

from ratelimit.

rabbbit avatar rabbbit commented on July 24, 2024

Sorry, we don't foresee adding this to the library - contrary to your example, replacing the limit on the fly has non-obvious behavior wrt already existing limits and slack. Additionally, the extra lock defeats the purpose of the lock-less implementation we have.

Since, however, as your example shows it is simple enough to wrap this in your own code. We would encourage you to do just that if that fits your requirements.

from ratelimit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.