github.com/onflow/flow-go@v0.33.17/module/mempool/queue/heroStore.go (about)

     1  package queue
     2  
     3  import (
     4  	"github.com/rs/zerolog"
     5  
     6  	"github.com/onflow/flow-go/engine"
     7  	"github.com/onflow/flow-go/module"
     8  	"github.com/onflow/flow-go/module/mempool/queue/internal"
     9  )
    10  
    11  type HeroStoreConfig struct {
    12  	SizeLimit uint32
    13  	Collector module.HeroCacheMetrics
    14  }
    15  
    16  type HeroStoreConfigOption func(builder *HeroStoreConfig)
    17  
    18  func WithHeroStoreSizeLimit(sizeLimit uint32) HeroStoreConfigOption {
    19  	return func(builder *HeroStoreConfig) {
    20  		builder.SizeLimit = sizeLimit
    21  	}
    22  }
    23  
    24  func WithHeroStoreCollector(collector module.HeroCacheMetrics) HeroStoreConfigOption {
    25  	return func(builder *HeroStoreConfig) {
    26  		builder.Collector = collector
    27  	}
    28  }
    29  
    30  // HeroStore is a FIFO (first-in-first-out) size-bound queue for maintaining engine.Message types.
    31  // It is based on HeroQueue.
    32  type HeroStore struct {
    33  	q *HeroQueue
    34  }
    35  
    36  func NewHeroStore(sizeLimit uint32, logger zerolog.Logger, collector module.HeroCacheMetrics,
    37  ) *HeroStore {
    38  	return &HeroStore{
    39  		q: NewHeroQueue(sizeLimit, logger, collector),
    40  	}
    41  }
    42  
    43  // Put enqueues the message into the message store.
    44  //
    45  // Boolean returned variable determines whether enqueuing was successful, i.e.,
    46  // put may be dropped if queue is full or already exists.
    47  func (c *HeroStore) Put(message *engine.Message) bool {
    48  	return c.q.Push(internal.NewMessageEntity(message))
    49  }
    50  
    51  // Get pops the queue, i.e., it returns the head of queue, and updates the head to the next element.
    52  // Boolean return value determines whether pop is successful, i.e., popping an empty queue returns false.
    53  func (c *HeroStore) Get() (*engine.Message, bool) {
    54  	head, ok := c.q.Pop()
    55  	if !ok {
    56  		return nil, false
    57  	}
    58  
    59  	msg := head.(internal.MessageEntity).Msg
    60  	return &msg, true
    61  }