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

lightningnetwork / lnd / 25455608158

06 May 2026 07:10PM UTC coverage: 62.23% (+0.002%) from 62.228%
25455608158

Pull #10686

github

web-flow
Merge c5cd7ffca into 6fd5b7bb2
Pull Request #10686: chainreg: use getnetworkinfo to count outbound peers (bitcoind backend)

7 of 37 new or added lines in 1 file covered. (18.92%)

31329 existing lines in 480 files now uncovered.

143794 of 231070 relevant lines covered (62.23%)

19066.44 hits per line

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

92.16
/graph/db/graph_cache_state.go
1
package graphdb
2

3
import (
4
        "sync"
5
        "sync/atomic"
6
)
7

8
// pendingUpdatesWarnThreshold is the number of buffered cache mutations at
9
// which a warning is logged. A large buffer indicates that cache population is
10
// taking a long time relative to the incoming gossip rate.
11
const pendingUpdatesWarnThreshold = 10_000
12

13
// graphCacheState tracks the in-memory graph cache together with its
14
// population state. The underlying GraphCache is independently thread-safe, so
15
// once reads are allowed to use it, they do not need to hold updateMtx.
16
type graphCacheState struct {
17
        graphCache *GraphCache
18
        loaded     atomic.Bool
19
        failed     atomic.Bool
20

21
        updateMtx sync.Mutex
22
        loading   bool
23

24
        pendingUpdates []func(*GraphCache)
25
}
26

27
// newGraphCacheState constructs a graph cache state with a new cache instance.
28
func newGraphCacheState(preAllocNumNodes int) *graphCacheState {
172✔
29
        return &graphCacheState{
172✔
30
                graphCache: NewGraphCache(preAllocNumNodes),
172✔
31
        }
172✔
32
}
172✔
33

34
// isLoaded reports whether the cache has finished its initial population and
35
// is safe to serve reads from.
36
func (s *graphCacheState) isLoaded() bool {
1,328✔
37
        return s.loaded.Load()
1,328✔
38
}
1,328✔
39

40
// isFailed reports whether the cache population attempt has failed.
UNCOV
41
func (s *graphCacheState) isFailed() bool {
4✔
UNCOV
42
        return s.failed.Load()
4✔
UNCOV
43
}
4✔
44

45
// stats returns the cache stats if the cache has finished its initial
46
// population.
47
func (s *graphCacheState) stats() (string, bool) {
239✔
48
        if !s.isLoaded() {
242✔
49
                return "", false
3✔
50
        }
3✔
51

52
        return s.graphCache.Stats(), true
239✔
53
}
54

55
// beginPopulation marks the cache as loading and starts buffering concurrent
56
// cache mutations until the population pass completes.
57
func (s *graphCacheState) beginPopulation() {
172✔
58
        s.updateMtx.Lock()
172✔
59
        defer s.updateMtx.Unlock()
172✔
60

172✔
61
        s.loading = true
172✔
62
        s.pendingUpdates = nil
172✔
63
}
172✔
64

65
// finishPopulation replays any buffered mutations and marks the cache as ready
66
// when the initial population completed successfully. If population failed,
67
// buffered mutations are discarded since the cache won't be used for reads.
68
func (s *graphCacheState) finishPopulation(loaded bool) {
172✔
69
        s.updateMtx.Lock()
172✔
70
        defer s.updateMtx.Unlock()
172✔
71

172✔
72
        if loaded {
341✔
73
                for _, update := range s.pendingUpdates {
173✔
74
                        update(s.graphCache)
4✔
75
                }
4✔
76

77
                s.loaded.Store(true)
169✔
UNCOV
78
        } else {
3✔
UNCOV
79
                s.failed.Store(true)
3✔
UNCOV
80
        }
3✔
81

82
        s.pendingUpdates = nil
172✔
83
        s.loading = false
172✔
84
}
85

86
// applyUpdate applies a cache mutation immediately or buffers it when the
87
// cache is still being populated.
88
func (s *graphCacheState) applyUpdate(update func(cache *GraphCache)) {
6,064✔
89
        s.updateMtx.Lock()
6,064✔
90
        defer s.updateMtx.Unlock()
6,064✔
91

6,064✔
92
        if s.loading {
6,068✔
93
                s.pendingUpdates = append(s.pendingUpdates, update)
4✔
94

4✔
95
                if len(s.pendingUpdates)%pendingUpdatesWarnThreshold == 0 {
4✔
96
                        log.Warnf("Graph cache has %d pending updates "+
×
97
                                "buffered during population",
×
98
                                len(s.pendingUpdates))
×
99
                }
×
100

101
                return
4✔
102
        }
103

104
        update(s.graphCache)
6,063✔
105
}
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

© 2026 Coveralls, Inc