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

lightningnetwork / lnd / 12583319996

02 Jan 2025 01:38PM UTC coverage: 57.522% (-1.1%) from 58.598%
12583319996

Pull #9361

github

starius
fn/ContextGuard: use context.AfterFunc to wait

Simplifies context cancellation handling by using context.AfterFunc instead of a
goroutine to wait for context cancellation. This approach avoids the overhead of
a goroutine during the waiting period.

For ctxQuitUnsafe, since g.quit is closed only in the Quit method (which also
cancels all associated contexts), waiting on context cancellation ensures the
same behavior without unnecessary dependency on g.quit.

Added a test to ensure that the Create method does not launch any goroutines.
Pull Request #9361: fn: optimize context guard

102587 of 178344 relevant lines covered (57.52%)

24734.33 hits per line

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

0.0
/graph/graphsession/graph_session.go
1
package graphsession
2

3
import (
4
        "fmt"
5

6
        graphdb "github.com/lightningnetwork/lnd/graph/db"
7
        "github.com/lightningnetwork/lnd/kvdb"
8
        "github.com/lightningnetwork/lnd/lnwire"
9
        "github.com/lightningnetwork/lnd/routing"
10
        "github.com/lightningnetwork/lnd/routing/route"
11
)
12

13
// Factory implements the routing.GraphSessionFactory and can be used to start
14
// a session with a ReadOnlyGraph.
15
type Factory struct {
16
        graph ReadOnlyGraph
17
}
18

19
// NewGraphSessionFactory constructs a new Factory which can then be used to
20
// start a new session.
21
func NewGraphSessionFactory(graph ReadOnlyGraph) routing.GraphSessionFactory {
×
22
        return &Factory{
×
23
                graph: graph,
×
24
        }
×
25
}
×
26

27
// NewGraphSession will produce a new Graph to use for a path-finding session.
28
// It returns the Graph along with a call-back that must be called once Graph
29
// access is complete. This call-back will close any read-only transaction that
30
// was created at Graph construction time.
31
//
32
// NOTE: This is part of the routing.GraphSessionFactory interface.
33
func (g *Factory) NewGraphSession() (routing.Graph, func() error, error) {
×
34
        tx, err := g.graph.NewPathFindTx()
×
35
        if err != nil {
×
36
                return nil, nil, err
×
37
        }
×
38

39
        session := &session{
×
40
                graph: g.graph,
×
41
                tx:    tx,
×
42
        }
×
43

×
44
        return session, session.close, nil
×
45
}
46

47
// A compile-time check to ensure that Factory implements the
48
// routing.GraphSessionFactory interface.
49
var _ routing.GraphSessionFactory = (*Factory)(nil)
50

51
// session is an implementation of the routing.Graph interface where the same
52
// read-only transaction is held across calls to the graph and can be used to
53
// access the backing channel graph.
54
type session struct {
55
        graph graph
56
        tx    kvdb.RTx
57
}
58

59
// NewRoutingGraph constructs a session that which does not first start a
60
// read-only transaction and so each call on the routing.Graph will create a
61
// new transaction.
62
func NewRoutingGraph(graph ReadOnlyGraph) routing.Graph {
×
63
        return &session{
×
64
                graph: graph,
×
65
        }
×
66
}
×
67

68
// close closes the read-only transaction being used to access the backing
69
// graph. If no transaction was started then this is a no-op.
70
func (g *session) close() error {
×
71
        if g.tx == nil {
×
72
                return nil
×
73
        }
×
74

75
        err := g.tx.Rollback()
×
76
        if err != nil {
×
77
                return fmt.Errorf("error closing db tx: %w", err)
×
78
        }
×
79

80
        return nil
×
81
}
82

83
// ForEachNodeChannel calls the callback for every channel of the given node.
84
//
85
// NOTE: Part of the routing.Graph interface.
86
func (g *session) ForEachNodeChannel(nodePub route.Vertex,
87
        cb func(channel *graphdb.DirectedChannel) error) error {
×
88

×
89
        return g.graph.ForEachNodeDirectedChannel(g.tx, nodePub, cb)
×
90
}
×
91

92
// FetchNodeFeatures returns the features of the given node. If the node is
93
// unknown, assume no additional features are supported.
94
//
95
// NOTE: Part of the routing.Graph interface.
96
func (g *session) FetchNodeFeatures(nodePub route.Vertex) (
97
        *lnwire.FeatureVector, error) {
×
98

×
99
        return g.graph.FetchNodeFeatures(nodePub)
×
100
}
×
101

102
// A compile-time check to ensure that *session implements the
103
// routing.Graph interface.
104
var _ routing.Graph = (*session)(nil)
105

106
// ReadOnlyGraph is a graph extended with a call to create a new read-only
107
// transaction that can then be used to make further queries to the graph.
108
type ReadOnlyGraph interface {
109
        // NewPathFindTx returns a new read transaction that can be used for a
110
        // single path finding session. Will return nil if the graph cache is
111
        // enabled.
112
        NewPathFindTx() (kvdb.RTx, error)
113

114
        graph
115
}
116

117
// graph describes the API necessary for a graph source to have access to on a
118
// database implementation, like channeldb.ChannelGraph, in order to be used by
119
// the Router for pathfinding.
120
type graph interface {
121
        // ForEachNodeDirectedChannel iterates through all channels of a given
122
        // node, executing the passed callback on the directed edge representing
123
        // the channel and its incoming policy. If the callback returns an
124
        // error, then the iteration is halted with the error propagated back
125
        // up to the caller.
126
        //
127
        // Unknown policies are passed into the callback as nil values.
128
        //
129
        // NOTE: if a nil tx is provided, then it is expected that the
130
        // implementation create a read only tx.
131
        ForEachNodeDirectedChannel(tx kvdb.RTx, node route.Vertex,
132
                cb func(channel *graphdb.DirectedChannel) error) error
133

134
        // FetchNodeFeatures returns the features of a given node. If no
135
        // features are known for the node, an empty feature vector is returned.
136
        FetchNodeFeatures(node route.Vertex) (*lnwire.FeatureVector, error)
137
}
138

139
// A compile-time check to ensure that *channeldb.ChannelGraph implements the
140
// graph interface.
141
var _ graph = (*graphdb.ChannelGraph)(nil)
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