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

feat!: gateway: fix rate limiting, better stateful handling #12315

Closed
wants to merge 2 commits into from

Conversation

rvagg
Copy link
Member

@rvagg rvagg commented Jul 27, 2024

Minor API changes:

  • gateway.NewRateLimiterHandler and gateway.NewConnectionRateLimiterHandler have been replaced with gateway.NewRateLimitHandler.
  • The handlers returned by both gateway.NewRateLimitHandler and the primary gateway.Handler return an http.Handler augmented with a Shutdown(ctx) method to be used for graceful cleanup of resources.

Fix:

  • --per-conn-rate-limit was previously applied as a global rate limiter,
    effectively making it have the same impact as --rate-limit. This change fixes
    the behaviour such that --per-conn-rate-limit is applied as a API call
    limiter within a single connection (i.e. a WebSocket connection). The rate
    is specified as tokens-per-second, where tokens are relative to the expense
    of the API call being made.

I wanted to get to #11589 but in the meantime I hit some hurdles that I decided to fix: the first is that it's not clear what these rate limiting properties are supposed to do, and the second is that they don't all seem to do what they should.

Most of this PR is cleanup, some refactoring, some doc extension, tests added. Big changes are listed above, the main fix is the --per-conn-rate-limit thing and you can see the problem in the diff where RateLimiterHandler has a global rate limiter that it just reuses for each connection, it ends up in Node#limit() where it's applied along with the global --rate-limit limiter, so it's basically the same thing.

Another major change is to replace the --conn-per-minute limit mechanism to a standard rate.Limiter instead of the custom rate limit implementation. There is a slight change of behaviour with this in that I allow a burst, which the original doesn't. It also has a different cleanup mechanism with a separate goroutine dedicated to this task instead of a goroutine per request that decrements and may cleanup if zero.

@rvagg rvagg requested review from masih and aarshkshah1992 July 27, 2024 10:07
@rvagg rvagg force-pushed the rvagg/gateway-cleanup branch 2 times, most recently from 4612641 to 537352e Compare July 27, 2024 10:24
Minor API changes:

* gateway.NewRateLimiterHandler and gateway.NewConnectionRateLimiterHandler have
  been replaced with gateway.NewRateLimitHandler.
* The handlers returned by both gateway.NewRateLimitHandler and the primary
  gateway.Handler return an http.Handler augmented with a Shutdown(ctx) method
  to be used for graceful cleanup of resources.

Fix:

* --per-conn-rate-limit was previously applied as a global rate limiter,
  effectively making it have the same impact as --rate-limit. This change fixes
  the behaviour such that --per-conn-rate-limit is applied as a API call
  limiter within a single connection (i.e. a WebSocket connection). The rate
  is specified as tokens-per-second, where tokens are relative to the expense
  of the API call being made.
@rvagg rvagg force-pushed the rvagg/gateway-cleanup branch from 537352e to 0c7e9af Compare July 27, 2024 10:25
@rvagg rvagg changed the title fix!: gateway: fix rate limiting, general cleanup feat!: gateway: fix rate limiting, better stateful handling Aug 1, 2024
@rvagg
Copy link
Member Author

rvagg commented Aug 1, 2024

Going to close this in favour of #12327

@rvagg rvagg closed this Aug 1, 2024
@rvagg rvagg deleted the rvagg/gateway-cleanup branch August 1, 2024 11:22
@rjan90 rjan90 mentioned this pull request Aug 31, 2024
8 tasks
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

Successfully merging this pull request may close these issues.

1 participant