Skip to content

Commit

Permalink
implement cap blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
gatsbyz committed Nov 2, 2023
1 parent b6e7007 commit bb7fdfd
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions cmd/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,17 +419,45 @@ func updateAllBlocks(ms *monitorStatus) []rpctypes.PolyBlock {
// default
blocks := make([]rpctypes.PolyBlock, 0)

ms.BlocksLock.RLock()
ms.BlocksLock.Lock()
defer ms.BlocksLock.Unlock()

// ensure the cap is maintained each time you read out the blocks.
capBlocks(ms)
for _, b := range ms.Blocks {
blocks = append(blocks, b)
}
ms.BlocksLock.RUnlock()
// ms.BlocksLock.RUnlock()

allBlocks := metrics.SortableBlocks(blocks)

return allBlocks
}

// Define a maximum number of blocks to keep in memory
const maxBlocksInMemory = 1000

// Function to manage the capping of blocks
func capBlocks(ms *monitorStatus) {
ms.BlocksLock.Lock()
defer ms.BlocksLock.Unlock()

currentBlockCount := len(ms.Blocks)
if currentBlockCount > maxBlocksInMemory {
// Delete blocks that exceed the maxBlocksInMemory
// Assuming blocks are sorted by some identifier (e.g., block height)
// Here you'd need to figure out the correct logic to remove the oldest blocks
// depending on how your ms.Blocks is structured.
for key, _ := range ms.Blocks {

Check failure on line 451 in cmd/monitor/monitor.go

View workflow job for this annotation

GitHub Actions / Lint

S1005: unnecessary assignment to the blank identifier (gosimple)
if currentBlockCount <= maxBlocksInMemory {
break
}
delete(ms.Blocks, key)
currentBlockCount--
}
}
}

func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatus, rpc *ethrpc.Client) error {
if err := ui.Init(); err != nil {
return err
Expand Down

0 comments on commit bb7fdfd

Please sign in to comment.