gammazero / workerpool

Concurrency limiting goroutine pool
MIT License
1.33k stars 138 forks source link

Wait on Submit until queue has #71

Open janekolszak opened 1 year ago

janekolszak commented 1 year ago

Hi, I'm in a need of blocking Submit until the queue of tasks is small enough. I imagine something like SubmitBlock( ctx context.Context, minQueueDepth uint, f func()) that waits for the queue to be small enough, inserts the func and immediately returns.

Would you like to add something like that or accept a PR?

toqueteos commented 11 months ago

Here's the workaround I'm using until workerpool supports customizable queue size:

var stop bool
for !stop {
    if pool.WaitingQueueSize() > DESIRED_QUEUE_SIZE {
        time.Sleep(CUSTOM_DURATION) // exponential backoff would probably be better
        continue
    }
    pool.Submit(func() {
        // ...
    })
}