Search code examples
goworker-pool

Suspend worker pool


With a golang implementation of worker pool that look like close than this

I want to suspend my workers for few seconds, while doing a database synchronisation like a transaction process. I don't want my sync data being updated by another potential uncontrolled worker.

What is the best way to suspend work ?

  • Close all the worker ?
  • Use a chan in the worker to prevent getting a job if suspended ?
  • Use a global flag to setup a lock in each worker before handling job ?

Thanks


Solution

  • If the database synchronization is to be implemented or at least signalled to the binary, you could abuse an RWMutex for this purpose.

    Use the Read side of the mutex when in the worker doing ordinary work, and require the Write side to be held whenever the database synchronization actions are performed.

    Crucially, you must ensure Read is only held when work is actually taking place. If the worker is blocked awaiting more work, the read lock must not be held as this will starve pending writers.


    If you use this, I strongly recommend adding documentation to your code as to the expected behaviour of the mutex, as this would be somewhat nonstandard in terms of the Read and Write operations.

    You could also wrap it and export different methods from your derived interface with better names, if you would like a more general solution, as shown in the example:

    type WorkerGroupLocker struct {
        sync.RWMutex
    }
    
    func (lock *WorkerGroupLocker) LockWorker() {
        lock.RLock()
    }
    
    func (lock *WorkerGroupLocker) UnlockWorker() {
        lock.RUnlock()
    }
    
    func (lock *WorkerGroupLocker) LockBackgroundSync() {
        lock.Lock()
    }
    
    func (lock *WorkerGroupLocker) UnlockBackgroundSync() {
        lock.Unlock()
    }
    

    Any solution must solve these general problems:

    • Asking all workers to stop working.
    • Ensuring all workers have quiesced before the database work is allowed to start.
    • Signalling workers that it is safe to continue working again.

    Alternative methods for doing so include the following, but in my opinion all of the below will be much more complex (and, therefore, prone to bugs) than using the mutex:

    • Close all the worker ?

      Asking workers to stop working before the database work goes ahead would achieve the mutual exclusion required. You need a way of signalling them to cease and a mechanism for detecting when this has indeed completed.

      One could argue in any event that you need this anyway to ensure you cleanly shutdown on program termination. However, program termination is fatal, so you don't need to be able to cleanly start and stop the pool in the same way as this database sync work would require.

    • Use a chan in the worker to prevent getting a job if suspended ?

      This would be complex to implement, as you would need to signal all workers to stop working and be assured they have indeed stopped actively processing jobs before starting work. The reverse is also required: a signal to start again.

    The simplest tool for the job which involves the least code is the mutex. I recommend using that.