您好,登錄后才能下訂單哦!
本篇文章為大家展示了tunny中workerWrapper的作用是什么,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
type workerWrapper struct { worker Worker interruptChan chan struct{} // reqChan is NOT owned by this type, it is used to send requests for work. reqChan chan<- workRequest // closeChan can be closed in order to cleanly shutdown this worker. closeChan chan struct{} // closedChan is closed by the run() goroutine when it exits. closedChan chan struct{} } func newWorkerWrapper( reqChan chan<- workRequest, worker Worker, ) *workerWrapper { w := workerWrapper{ worker: worker, interruptChan: make(chan struct{}), reqChan: reqChan, closeChan: make(chan struct{}), closedChan: make(chan struct{}), } go w.run() return &w }
workerWrapper包裝了worker,定義了interruptChan、reqChan、closeChan、closedChan屬性
func (w *workerWrapper) interrupt() { close(w.interruptChan) w.worker.Interrupt() }
interrupt方法關閉w.interruptChan,執行w.worker.Interrupt()
func (w *workerWrapper) run() { jobChan, retChan := make(chan interface{}), make(chan interface{}) defer func() { w.worker.Terminate() close(retChan) close(w.closedChan) }() for { // NOTE: Blocking here will prevent the worker from closing down. w.worker.BlockUntilReady() select { case w.reqChan <- workRequest{ jobChan: jobChan, retChan: retChan, interruptFunc: w.interrupt, }: select { case payload := <-jobChan: result := w.worker.Process(payload) select { case retChan <- result: case <-w.interruptChan: w.interruptChan = make(chan struct{}) } case _, _ = <-w.interruptChan: w.interruptChan = make(chan struct{}) } case <-w.closeChan: return } } }
run首先創建jobChan、retChan,然后for循環執行select讀取reqChan,之后讀取jobChan的payload,進行處理,然后寫入到retChan
func (w *workerWrapper) stop() { close(w.closeChan) }
stop方法關閉w.closeChan
func (w *workerWrapper) join() { <-w.closedChan }
join方法則等待w.closedChan
tunny的workerWrapper包裝了worker,定義了interruptChan、reqChan、closeChan、closedChan屬性,它提供了interrupt、run、stop、join方法。
上述內容就是tunny中workerWrapper的作用是什么,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。