I want to combine rate limiting and caching in reqwest
. I want the request to go like this:
- Initiate request
- Fetch from the cache if available and return response
- Rate limit requests to the remote server
- Fetch from the remote server
- Return remote response
I'm having trouble figuring out how to combine the various middlewares to achieve this. It seems like the caching middleware (I'm trying to use http-cache-reqwest
and possibly reqwest-ratelimit
with governor
) would need to contain the rate limiting inside of it.
It seems there are two ways I could determine if the cache request will succeed. One is to use CACacheManager::get()
, but that requires that I know the cache key and kind of defeats the purpose of the middleware. The other is to let the request proceed and examine the response to see if it has cache headers set. But while I want a cached request to be resolved immediately (skip rate limiting), I don't want the response to be delayed by the rate limiting in the case where it fetches the result from the remote server. So it seems like maybe I need to send two requests, the first with OnlyIfCached
and then Reload
after the rate limiting?
This all just feels more complicated than it should be, and I'm wondering if I'm overthinking this.