Controlling the number of requests per http route

Hey there. What might be a nice way to restrict (throttle) the number of http requests that can come in for a particular service? I have a service that I’d like to restrict to serving just one http request at a time, whereas other routes are cheap to service and can be done so more regularly.

I had thought of using a BroadcastHub and completing my route with a source produced from the hub, but there are some parameters from my http request that I need to feed into the service so this isn’t possible. Also, a BroadcastHub would can only limit producing sources to a minimum of two at a time (a buffer of 0 is not permitted).

Something like this might be nice:

  (get & paramDateRange)((startDate, endDate) =>
      async(1)( // <-- Ficticious directive that limits the request rate to 1 at a time
        SomeService.doSomethingWithDates(startDate, endDate)

Nothing built in as far as I know but here’s a custom rate-limit directive I wrote for someone a while ago: Sample for a custom rate limiting directive for Akka HTTP · GitHub

Should be possible to use that wherever int the routing tree, tricky part is that you need to create the surrounding limiter object outside of the tree for the atomic to be shared - in tree it would be created anew for each requests.

1 Like

Thanks for that. I’m looking for something that back-pressures instead of failing. I’ll think on it a bit more though. Good to know that I’m not missing anything though.

Do you have an idea how backpressure should work? Just keep backpressured requests unanswered until there’s more capacity? Or don’t accept new connections in the first place?

Some time ago I started collecting ideas in Backpressure on outstanding requests instead of connections for server side · Issue #2992 · akka/akka-http · GitHub.