github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/queue/queueWorker.go (about)

     1  package queue
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/onflow/flow-go/network"
     7  )
     8  
     9  const DefaultNumWorkers = 50
    10  
    11  // worker de-queues an item from the queue if available and calls the callback function endlessly
    12  // if no item is available, it blocks
    13  func worker(ctx context.Context, queue network.MessageQueue, callback func(interface{})) {
    14  	for {
    15  		// blocking call
    16  		item := queue.Remove()
    17  		select {
    18  		case <-ctx.Done():
    19  			return
    20  		default:
    21  			callback(item)
    22  		}
    23  	}
    24  }
    25  
    26  // CreateQueueWorkers creates queue workers to read from the queue
    27  func CreateQueueWorkers(ctx context.Context, numWorks uint64, queue network.MessageQueue, callback func(interface{})) {
    28  	for i := uint64(0); i < numWorks; i++ {
    29  		go worker(ctx, queue, callback)
    30  	}
    31  }