alitto / pond

🔘 Minimalistic and High-performance goroutine worker pool written in Go
MIT License
1.38k stars 60 forks source link

Return `chan struct{}` from pool.Stop() to give a better control over cleanup operations. #63

Closed CorentinClabaut closed 5 days ago

CorentinClabaut commented 1 month ago

Currently, when calling pool.Stop(), there's no immediate way to know when the pool has completely halted its operations.

By adding a chan struct{} return type to pool.Stop(), users can receive a signal indicating when the pool has stopped, enabling better management of resources and cleanup tasks.

This enhancement would help when multiple concurrent cleanup tasks need to be executed within a predefined timeframe.

    ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
    defer cancel()

        poolStopped := pool.Stop()
        otherStopped := other.Stop()

    select {
    case <-poolStopped:
    case <-ctx.Done():
        log.Error("Worker pool did not stop gracefully")
    }

        select {
    case <-otherStopped:
    case <-ctx.Done():
        log.Error("other did not stop gracefully")
    }

Let me know if you think that would be a good addition, and if so, I can create a pull request for it.

CorentinClabaut commented 1 week ago

Or we could return a context like in the cron lib (https://github.com/robfig/cron/blob/master/cron.go#L323)

I'm happy to make the change if you think that's a good idea @alitto

alitto commented 1 week ago

Hey @CorentinClabaut, Sorry for the delay, I saw this issue but completely lost track of it. I think this is a greate idea. Returning the a context looks like an elegant and composable solution, I like that option better 🙂 Please go ahead and create a PR if you can, I'll make sure to review it. This would modify the contract of the Stop method but there shouldn't be any issues with existing code that uses it since it currently has no return type.