I have a very specific kind of requests that are o...
# coroutines
c
I have a very specific kind of requests that are only used for latency-reduction purposes (pre-loading data that may be useful later). If the server is overloaded in any way, I don't want to perform these requests, as they are not crucial in any way. Is there a way to represent this easily with coroutines? Maybe something like "launch this coroutine with low priority, and if it hasn't been scheduled after 1 second just give up to let other more important stuff run"
r
Two options spring to mind - replace all your Default dispatchers with https://github.com/khushpanchal/PriorityDispatcher Alternatively create an ExecutorService backed by a new thread pool where you set the thread priority and turning that into a coroutine dispatcher https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Thread.html#setPriority(int) For both, the "submit to execute" time check you would probably need to implement on top
k
c
Hm, both of these are JVM-only, no?
Thanks for the issue link
r
this isn’t directly answering your question about coroutine prioritization, but does your server and protocol honor request prioritization and/or flow control? If so could you use this to ensure that the pre-fetch requests are low priority?
k
Are you trying to do the coroutine prioritization on the server or on the client? Based on this
If the server is overloaded in any way, I don’t want to perform these requests, as they are not crucial in any way.
I’m lead to believe that the prioritization is being implemented on the server
c
Are you trying to do the coroutine prioritization on the server or on the client?
Both. Our server is able to guess which resources clients will want to access soon, sometimes a few seconds before they do. We'd like to take advantage of that and preload relevant information from the database. The client can attempt to pre-load stuff too, but that's easier to handle server-side (when you receive the request, check how many ongoing requests are in-flight, and just refuse to do it based on some rate-limit).
k
Interesting. Is your server running on the JVM?
My first attempt at this would be to not attempt coroutine prioritization on the client side. It’s unlikely that clients will have too many coroutines running to actually overwhelm them, right? Instead, I’d do some sort of prioritization on the server side and use that prioritization to determine if a request is rate limited. The client should respond to rate limits regularly. I have a feeling you’ve already thought of this, though, and there’s additional reasons why you want prioritization on the client side. If not, you might be able to fudge prioritization on the server with the JVM facilities mentioned above?
nod 1