2 ms·
(author here) Virtual threads being "free" definitely makes things simpler to implement, I was actually quite surprised. Where I think things would get more co
by andersmurphy 2y ago
(author here) Virtual threads being "free" definitely makes things simpler to implement, I was actually quite surprised.
Where I think things would get more complicated is if you wanted to control how bursty the token bucket is. You'd probably need another semaphore to limit the bursts etc.
This implementation is naive and does create a virtual thread for each permit that is waiting, so you have 2x the number of threads as you would have with a more complex implementation (i.e 1 per running task and 1 per waiting permit). This would be a no go with regular threads, but seems to work fine with virtual threads. It's pretty cool that virtual threads make naive implementations work.