github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/mempool/stdmap/times.go (about) 1 package stdmap 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/onflow/flow-go/model/flow" 8 ) 9 10 // Times implements the times memory pool used to store time.Times for an idetifier to track transaction metrics in 11 // access nodes 12 type Times struct { 13 *Backend 14 } 15 16 // NewTimes creates a new memory pool for times 17 func NewTimes(limit uint) (*Times, error) { 18 t := &Times{ 19 Backend: NewBackend(WithLimit(limit)), 20 } 21 22 return t, nil 23 } 24 25 type Time struct { 26 id flow.Identifier 27 ti time.Time 28 } 29 30 func (t *Time) ID() flow.Identifier { 31 return t.id 32 } 33 34 func (t *Time) Checksum() flow.Identifier { 35 return t.id 36 } 37 38 // Add adds a time to the mempool. 39 func (t *Times) Add(id flow.Identifier, ti time.Time) bool { 40 return t.Backend.Add(&Time{id, ti}) 41 } 42 43 // ByID returns the time with the given ID from the mempool. 44 func (t *Times) ByID(id flow.Identifier) (time.Time, bool) { 45 entity, exists := t.Backend.ByID(id) 46 if !exists { 47 return time.Time{}, false 48 } 49 tt, ok := entity.(*Time) 50 if !ok { 51 panic(fmt.Sprintf("invalid entity in times pool (%T)", entity)) 52 } 53 return tt.ti, true 54 } 55 56 // Remove removes the time with the given ID. 57 func (t *Times) Remove(id flow.Identifier) bool { 58 return t.Backend.Remove(id) 59 }