• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 13543202610

26 Feb 2025 11:50AM UTC coverage: 58.834% (+0.009%) from 58.825%
13543202610

Pull #9544

github

web-flow
Merge pull request #9550 from ellemouton/graph14

graph: extract cache from CRUD [3]
Pull Request #9544: graph: move graph cache out of CRUD layer

2548 of 3279 new or added lines in 5 files covered. (77.71%)

301 existing lines in 21 files now uncovered.

136426 of 231881 relevant lines covered (58.83%)

19328.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.88
/graph/db/options.go
1
package graphdb
2

3
import "time"
4

5
const (
6
        // DefaultRejectCacheSize is the default number of rejectCacheEntries to
7
        // cache for use in the rejection cache of incoming gossip traffic. This
8
        // produces a cache size of around 1MB.
9
        DefaultRejectCacheSize = 50000
10

11
        // DefaultChannelCacheSize is the default number of ChannelEdges cached
12
        // in order to reply to gossip queries. This produces a cache size of
13
        // around 40MB.
14
        DefaultChannelCacheSize = 20000
15

16
        // DefaultPreAllocCacheNumNodes is the default number of channels we
17
        // assume for mainnet for pre-allocating the graph cache. As of
18
        // September 2021, there currently are 14k nodes in a strictly pruned
19
        // graph, so we choose a number that is slightly higher.
20
        DefaultPreAllocCacheNumNodes = 15000
21
)
22

23
// chanGraphOptions holds parameters for tuning and customizing the
24
// ChannelGraph.
25
type chanGraphOptions struct {
26
        // useGraphCache denotes whether the in-memory graph cache should be
27
        // used or a fallback version that uses the underlying database for
28
        // path finding.
29
        useGraphCache bool
30

31
        // preAllocCacheNumNodes is the number of nodes we expect to be in the
32
        // graph cache, so we can pre-allocate the map accordingly.
33
        preAllocCacheNumNodes int
34
}
35

36
// defaultChanGraphOptions returns a new chanGraphOptions instance populated
37
// with default values.
38
func defaultChanGraphOptions() *chanGraphOptions {
176✔
39
        return &chanGraphOptions{
176✔
40
                useGraphCache:         true,
176✔
41
                preAllocCacheNumNodes: DefaultPreAllocCacheNumNodes,
176✔
42
        }
176✔
43
}
176✔
44

45
// ChanGraphOption describes the signature of a functional option that can be
46
// used to customize a ChannelGraph instance.
47
type ChanGraphOption func(*chanGraphOptions)
48

49
// WithUseGraphCache sets whether the in-memory graph cache should be used.
50
func WithUseGraphCache(use bool) ChanGraphOption {
105✔
51
        return func(o *chanGraphOptions) {
210✔
52
                o.useGraphCache = use
105✔
53
        }
105✔
54
}
55

56
// WithPreAllocCacheNumNodes sets the number of nodes we expect to be in the
57
// graph cache, so we can pre-allocate the map accordingly.
NEW
58
func WithPreAllocCacheNumNodes(n int) ChanGraphOption {
×
NEW
59
        return func(o *chanGraphOptions) {
×
NEW
60
                o.preAllocCacheNumNodes = n
×
NEW
61
        }
×
62
}
63

64
// KVStoreOptions holds parameters for tuning and customizing a graph.DB.
65
type KVStoreOptions struct {
66
        // RejectCacheSize is the maximum number of rejectCacheEntries to hold
67
        // in the rejection cache.
68
        RejectCacheSize int
69

70
        // ChannelCacheSize is the maximum number of ChannelEdges to hold in the
71
        // channel cache.
72
        ChannelCacheSize int
73

74
        // BatchCommitInterval is the maximum duration the batch schedulers will
75
        // wait before attempting to commit a pending set of updates.
76
        BatchCommitInterval time.Duration
77

78
        // NoMigration specifies that underlying backend was opened in read-only
79
        // mode and migrations shouldn't be performed. This can be useful for
80
        // applications that use the channeldb package as a library.
81
        NoMigration bool
82
}
83

84
// DefaultOptions returns a KVStoreOptions populated with default values.
85
func DefaultOptions() *KVStoreOptions {
216✔
86
        return &KVStoreOptions{
216✔
87
                RejectCacheSize:  DefaultRejectCacheSize,
216✔
88
                ChannelCacheSize: DefaultChannelCacheSize,
216✔
89
                NoMigration:      false,
216✔
90
        }
216✔
91
}
216✔
92

93
// KVStoreOptionModifier is a function signature for modifying the default
94
// KVStoreOptions.
95
type KVStoreOptionModifier func(*KVStoreOptions)
96

97
// WithRejectCacheSize sets the RejectCacheSize to n.
98
func WithRejectCacheSize(n int) KVStoreOptionModifier {
3✔
99
        return func(o *KVStoreOptions) {
6✔
100
                o.RejectCacheSize = n
3✔
101
        }
3✔
102
}
103

104
// WithChannelCacheSize sets the ChannelCacheSize to n.
105
func WithChannelCacheSize(n int) KVStoreOptionModifier {
3✔
106
        return func(o *KVStoreOptions) {
6✔
107
                o.ChannelCacheSize = n
3✔
108
        }
3✔
109
}
110

111
// WithBatchCommitInterval sets the batch commit interval for the interval batch
112
// schedulers.
113
func WithBatchCommitInterval(interval time.Duration) KVStoreOptionModifier {
3✔
114
        return func(o *KVStoreOptions) {
6✔
115
                o.BatchCommitInterval = interval
3✔
116
        }
3✔
117
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc