doushi4795 2018-09-05 21:11
浏览 8
已采纳

暂停工人池

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

  • 写回答

1条回答 默认 最新

  • duanrou5680 2018-09-05 21:47
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”