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

Add stream throttle mechanism (rate limiting using token bucket algorithm) #17

Open
clue opened this issue Jun 1, 2020 · 1 comment
Assignees
Labels
help wanted Extra attention is needed new feature New feature or request

Comments

@clue
Copy link
Owner

clue commented Jun 1, 2020

This project should provide a Throttle class that can be used to limit the throughput to X elements in Y seconds. This can be used in a piping context with or without a Transformer like this:

$throttle = new Throttle(10, 3600, $loop);
$input->pipe($throttle)->pipe($transformer)->pipe($output);

I've started working on a prototype implementation and it seems to work quite well, but I'd like to flesh out a decent API and address some subtle edge cases before filing this here. For example, do we also need a ReadThrottle or WriteThrottle decorator for readable/writable streams respectively? Also, by throttling before piping to the Transformer, we may end up sending too many requests anyway, so this might be better suited as part of the Transformer instead?

See also clue/reactphp-mq#14 for similar implementation ideas.

IMHO this mechanism should be independent of a "bandwidth" mechanism, i.e. it should only limit full elements instead of accepting a string data stream that will be broken down into the appropriate number of bytes per time frame. There's definitely also room for this implementation, but I don't see a lot of cohesion with the rest of this package, so this might be better off in a separate package.

@clue clue added new feature New feature or request help wanted Extra attention is needed labels Jun 1, 2020
@clue clue self-assigned this Jun 1, 2020
@clue clue changed the title Add stream throttle mechanism (token bucket algorithm) Add stream throttle mechanism (rate limiting using token bucket algorithm) Jun 1, 2020
@clue
Copy link
Owner Author

clue commented Jun 1, 2020

For the reference: Instead of rate limiting using a fixed interval, it's also possible to use a retry mechanism as discussed in #2. This can be used to send at maximum rate until an error is returned in which case a exponential backoff algorithm can be employed to keep retrying at after increasing intervals until the rate is no longer exceeded. While this can cause some failed attempts, it does not require any configuration / prior knowledge of any rate limits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed new feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant