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