github.com/decred/dcrd/blockchain@v1.2.1/prune.go (about)

     1  package blockchain
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // pruningIntervalInMinutes is the interval in which to prune the blockchain's
     8  // nodes and restore memory to the garbage collector.
     9  const pruningIntervalInMinutes = 5
    10  
    11  // chainPruner is used to occasionally prune the blockchain of old nodes that
    12  // can be freed to the garbage collector.
    13  type chainPruner struct {
    14  	chain              *BlockChain
    15  	lastNodeInsertTime time.Time
    16  }
    17  
    18  // newChainPruner returns a new chain pruner.
    19  func newChainPruner(chain *BlockChain) *chainPruner {
    20  	return &chainPruner{
    21  		chain:              chain,
    22  		lastNodeInsertTime: time.Now(),
    23  	}
    24  }
    25  
    26  // pruneChainIfNeeded checks the current time versus the time of the last pruning.
    27  // If the blockchain hasn't been pruned in this time, it initiates a new pruning.
    28  //
    29  // pruneChainIfNeeded must be called with the chainLock held for writes.
    30  func (c *chainPruner) pruneChainIfNeeded() {
    31  	now := time.Now()
    32  	duration := now.Sub(c.lastNodeInsertTime)
    33  	if duration < time.Minute*pruningIntervalInMinutes {
    34  		return
    35  	}
    36  
    37  	c.lastNodeInsertTime = now
    38  	c.chain.pruneStakeNodes()
    39  }