github.com/koko1123/flow-go-1@v0.29.6/module/mempool/epochs/transactions.go (about) 1 package epochs 2 3 import ( 4 "sync" 5 6 "github.com/koko1123/flow-go-1/module/mempool" 7 ) 8 9 // TransactionPools is a set of epoch-scoped transaction pools. Each pool is a 10 // singleton that is instantiated the first time a transaction pool for that 11 // epoch is requested. 12 // 13 // This enables decoupled components to share access to the same transaction 14 // pools across epochs, while maintaining the property that one transaction 15 // pool is only valid for a single epoch. 16 type TransactionPools struct { 17 mu sync.RWMutex 18 pools map[uint64]mempool.Transactions 19 create func(uint64) mempool.Transactions 20 } 21 22 // NewTransactionPools returns a new set of epoch-scoped transaction pools. 23 func NewTransactionPools(create func(uint64) mempool.Transactions) *TransactionPools { 24 25 pools := &TransactionPools{ 26 pools: make(map[uint64]mempool.Transactions), 27 create: create, 28 } 29 return pools 30 } 31 32 // ForEpoch returns the transaction pool for the given pool. All calls for 33 // the same epoch will return the same underlying transaction pool. 34 func (t *TransactionPools) ForEpoch(epoch uint64) mempool.Transactions { 35 36 t.mu.RLock() 37 pool, exists := t.pools[epoch] 38 t.mu.RUnlock() 39 if exists { 40 return pool 41 } 42 43 t.mu.Lock() 44 defer t.mu.Unlock() 45 46 pool = t.create(epoch) 47 t.pools[epoch] = pool 48 return pool 49 } 50 51 // CombinedSize returns the sum of the sizes of all transaction pools. 52 func (t *TransactionPools) CombinedSize() uint { 53 54 t.mu.RLock() 55 defer t.mu.RUnlock() 56 57 size := uint(0) 58 for _, pool := range t.pools { 59 size += pool.Size() 60 } 61 62 return size 63 }