kjetil.hartveit
kjetil.hartveit14mo ago

How to add a short delay between requests

Environment: nextjs 13, node 18, npm, trpc 10.9.0 I'm wondering if it's possible to add a short delay between requests when either using httpBatchLink or httpLink. Background: I'm in a bit of a volatile situation as a 3rd-party API of my app doesn't support any more than 10 requests per minute. On a particular page there are sent several http requests of which multiple calls the 3rd-party API. I have implemented Upstash/redis caching for the requests, however whenever the cache have expired all requests are sent at the same time, thus no calls hits the cache as no requests has gone through and filled the cache yet. I'm playing with an idea of detecting whether a request has started and then waiting on the request to finish to fill the cache. This must work across http requests so it's implemented using upstash/redis. However I may need a little delay between the requests to make it work somewhat. Curious if anyone knows about other solutions should a delay not be feasible.
3 Replies
Alex / KATT 🐱
Alex / KATT 🐱14mo ago
You could probably do a custom link But if you have multiple users it won't really matter
Alex / KATT 🐱
Alex / KATT 🐱14mo ago
Links Overview | tRPC
Links enable you to customize the flow of data between the tRPC Client and Server. A link should do only one thing, which can be either a self-contained modification to a tRPC operation (query, mutation, or subscription) or a side-effect based on the operation (such as logging).
Nick
Nick14mo ago
This does sound like more of a backend concern, some async worker process and a status in redis for whether the data is ready. Could be polled and awaited that way