mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-11-20 19:16:47 +01:00
graph: add contexts to the ReadOnlyGraph interface
Since the GraphSource interface may be satisfied by an RPC connection, it is best practice to pass a context through to any call in the interface. The ChanGraphSource implementation, which uses a kvdb backend, does not make use of the ctx. Any call-sites are for now given a `context.TODO()` which will all be addressed in follow up commits.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package session
|
package session
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
graphdb "github.com/lightningnetwork/lnd/graph/db"
|
graphdb "github.com/lightningnetwork/lnd/graph/db"
|
||||||
@@ -30,7 +31,7 @@ func NewGraphSessionFactory(graph ReadOnlyGraph) routing.GraphSessionFactory {
|
|||||||
//
|
//
|
||||||
// NOTE: This is part of the routing.GraphSessionFactory interface.
|
// NOTE: This is part of the routing.GraphSessionFactory interface.
|
||||||
func (g *Factory) NewGraphSession() (routing.Graph, func() error, error) {
|
func (g *Factory) NewGraphSession() (routing.Graph, func() error, error) {
|
||||||
tx, err := g.graph.NewPathFindTx()
|
tx, err := g.graph.NewPathFindTx(context.TODO())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -85,7 +86,9 @@ func (g *session) close() error {
|
|||||||
func (g *session) ForEachNodeChannel(nodePub route.Vertex,
|
func (g *session) ForEachNodeChannel(nodePub route.Vertex,
|
||||||
cb func(channel *graphdb.DirectedChannel) error) error {
|
cb func(channel *graphdb.DirectedChannel) error) error {
|
||||||
|
|
||||||
return g.graph.ForEachNodeDirectedChannel(g.tx, nodePub, cb)
|
return g.graph.ForEachNodeDirectedChannel(
|
||||||
|
context.TODO(), g.tx, nodePub, cb,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FetchNodeFeatures returns the features of the given node. If the node is
|
// FetchNodeFeatures returns the features of the given node. If the node is
|
||||||
@@ -95,7 +98,7 @@ func (g *session) ForEachNodeChannel(nodePub route.Vertex,
|
|||||||
func (g *session) FetchNodeFeatures(nodePub route.Vertex) (
|
func (g *session) FetchNodeFeatures(nodePub route.Vertex) (
|
||||||
*lnwire.FeatureVector, error) {
|
*lnwire.FeatureVector, error) {
|
||||||
|
|
||||||
return g.graph.FetchNodeFeatures(g.tx, nodePub)
|
return g.graph.FetchNodeFeatures(context.TODO(), g.tx, nodePub)
|
||||||
}
|
}
|
||||||
|
|
||||||
// A compile-time check to ensure that *session implements the
|
// A compile-time check to ensure that *session implements the
|
||||||
@@ -108,7 +111,7 @@ type ReadOnlyGraph interface {
|
|||||||
// NewPathFindTx returns a new read transaction that can be used for a
|
// NewPathFindTx returns a new read transaction that can be used for a
|
||||||
// single path finding session. Will return nil if the graph cache is
|
// single path finding session. Will return nil if the graph cache is
|
||||||
// enabled.
|
// enabled.
|
||||||
NewPathFindTx() (RTx, error)
|
NewPathFindTx(ctx context.Context) (RTx, error)
|
||||||
|
|
||||||
graph
|
graph
|
||||||
}
|
}
|
||||||
@@ -127,7 +130,8 @@ type graph interface {
|
|||||||
//
|
//
|
||||||
// NOTE: if a nil tx is provided, then it is expected that the
|
// NOTE: if a nil tx is provided, then it is expected that the
|
||||||
// implementation create a read only tx.
|
// implementation create a read only tx.
|
||||||
ForEachNodeDirectedChannel(tx RTx, node route.Vertex,
|
ForEachNodeDirectedChannel(ctx context.Context, tx RTx,
|
||||||
|
node route.Vertex,
|
||||||
cb func(channel *graphdb.DirectedChannel) error) error
|
cb func(channel *graphdb.DirectedChannel) error) error
|
||||||
|
|
||||||
// FetchNodeFeatures returns the features of a given node. If no
|
// FetchNodeFeatures returns the features of a given node. If no
|
||||||
@@ -135,6 +139,6 @@ type graph interface {
|
|||||||
//
|
//
|
||||||
// NOTE: if a nil tx is provided, then it is expected that the
|
// NOTE: if a nil tx is provided, then it is expected that the
|
||||||
// implementation create a read only tx.
|
// implementation create a read only tx.
|
||||||
FetchNodeFeatures(tx RTx, node route.Vertex) (*lnwire.FeatureVector,
|
FetchNodeFeatures(ctx context.Context, tx RTx, node route.Vertex) (
|
||||||
error)
|
*lnwire.FeatureVector, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package sources
|
package sources
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
graphdb "github.com/lightningnetwork/lnd/graph/db"
|
graphdb "github.com/lightningnetwork/lnd/graph/db"
|
||||||
@@ -32,8 +33,8 @@ func NewDBGSource(db *graphdb.ChannelGraph) *DBSource {
|
|||||||
// path finding session. Will return nil if the graph cache is enabled for the
|
// path finding session. Will return nil if the graph cache is enabled for the
|
||||||
// underlying graphdb.ChannelGraph.
|
// underlying graphdb.ChannelGraph.
|
||||||
//
|
//
|
||||||
// NOTE: this is part of the graphsession.ReadOnlyGraph interface.
|
// NOTE: this is part of the session.ReadOnlyGraph interface.
|
||||||
func (s *DBSource) NewPathFindTx() (session.RTx, error) {
|
func (s *DBSource) NewPathFindTx(_ context.Context) (session.RTx, error) {
|
||||||
tx, err := s.db.NewPathFindTx()
|
tx, err := s.db.NewPathFindTx()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -51,8 +52,8 @@ func (s *DBSource) NewPathFindTx() (session.RTx, error) {
|
|||||||
//
|
//
|
||||||
// Unknown policies are passed into the callback as nil values.
|
// Unknown policies are passed into the callback as nil values.
|
||||||
//
|
//
|
||||||
// NOTE: this is part of the graphsession.ReadOnlyGraph interface.
|
// NOTE: this is part of the session.ReadOnlyGraph interface.
|
||||||
func (s *DBSource) ForEachNodeDirectedChannel(tx session.RTx,
|
func (s *DBSource) ForEachNodeDirectedChannel(_ context.Context, tx session.RTx,
|
||||||
node route.Vertex,
|
node route.Vertex,
|
||||||
cb func(channel *graphdb.DirectedChannel) error) error {
|
cb func(channel *graphdb.DirectedChannel) error) error {
|
||||||
|
|
||||||
@@ -70,7 +71,7 @@ func (s *DBSource) ForEachNodeDirectedChannel(tx session.RTx,
|
|||||||
// and passed into the callback.
|
// and passed into the callback.
|
||||||
//
|
//
|
||||||
// NOTE: this is part of the graphsession.ReadOnlyGraph interface.
|
// NOTE: this is part of the graphsession.ReadOnlyGraph interface.
|
||||||
func (s *DBSource) FetchNodeFeatures(tx session.RTx,
|
func (s *DBSource) FetchNodeFeatures(_ context.Context, tx session.RTx,
|
||||||
node route.Vertex) (*lnwire.FeatureVector, error) {
|
node route.Vertex) (*lnwire.FeatureVector, error) {
|
||||||
|
|
||||||
kvdbTx, err := extractKVDBRTx(tx)
|
kvdbTx, err := extractKVDBRTx(tx)
|
||||||
|
|||||||
Reference in New Issue
Block a user