github.com/yandex/pandora@v0.5.32/core/provider/queue.go (about)

     1  package provider
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/yandex/pandora/core"
     7  )
     8  
     9  type AmmoQueueConfig struct {
    10  	// AmmoQueueSize is number maximum number of ready but not acquired ammo.
    11  	// On queue overflow, ammo decode is stopped.
    12  	AmmoQueueSize int `config:"ammo-queue-size" validate:"min=1"`
    13  }
    14  
    15  const (
    16  	shootsPerSecondUpperBound = 128 * 1024
    17  	DefaultAmmoQueueSize      = shootsPerSecondUpperBound / 16
    18  )
    19  
    20  func DefaultAmmoQueueConfig() AmmoQueueConfig {
    21  	return AmmoQueueConfig{
    22  		AmmoQueueSize: DefaultAmmoQueueSize,
    23  	}
    24  }
    25  
    26  func NewAmmoQueue(newAmmo func() core.Ammo, conf AmmoQueueConfig) *AmmoQueue {
    27  	return &AmmoQueue{
    28  		OutQueue: make(chan core.Ammo, conf.AmmoQueueSize),
    29  		InputPool: sync.Pool{New: func() interface{} {
    30  			return newAmmo()
    31  		}},
    32  	}
    33  }
    34  
    35  type AmmoQueue struct {
    36  	OutQueue  chan core.Ammo
    37  	InputPool sync.Pool
    38  }
    39  
    40  func (p *AmmoQueue) Acquire() (core.Ammo, bool) {
    41  	ammo, ok := <-p.OutQueue
    42  	return ammo, ok
    43  }
    44  
    45  func (p *AmmoQueue) Release(a core.Ammo) {
    46  	p.InputPool.Put(a)
    47  }