diff --git a/channeldb/db.go b/channeldb/db.go index 17275c292..173aada2d 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -291,6 +291,7 @@ func CreateWithBackend(backend kvdb.Backend, modifiers ...OptionModifier) (*DB, chanDB.graph, err = NewChannelGraph( backend, opts.RejectCacheSize, opts.ChannelCacheSize, opts.BatchCommitInterval, opts.PreAllocCacheNumNodes, + opts.UseGraphCache, ) if err != nil { return nil, err diff --git a/channeldb/graph.go b/channeldb/graph.go index 3cda1d782..6f8fd55bc 100644 --- a/channeldb/graph.go +++ b/channeldb/graph.go @@ -188,8 +188,8 @@ type ChannelGraph struct { // NewChannelGraph allocates a new ChannelGraph backed by a DB instance. The // returned instance has its own unique reject cache and channel cache. func NewChannelGraph(db kvdb.Backend, rejectCacheSize, chanCacheSize int, - batchCommitInterval time.Duration, - preAllocCacheNumNodes int) (*ChannelGraph, error) { + batchCommitInterval time.Duration, preAllocCacheNumNodes int, + useGraphCache bool) (*ChannelGraph, error) { if err := initChannelGraph(db); err != nil { return nil, err diff --git a/channeldb/graph_test.go b/channeldb/graph_test.go index 27b979842..f66b57958 100644 --- a/channeldb/graph_test.go +++ b/channeldb/graph_test.go @@ -77,6 +77,7 @@ func MakeTestGraph(modifiers ...OptionModifier) (*ChannelGraph, func(), error) { graph, err := NewChannelGraph( backend, opts.RejectCacheSize, opts.ChannelCacheSize, opts.BatchCommitInterval, opts.PreAllocCacheNumNodes, + true, ) if err != nil { backendCleanup() diff --git a/channeldb/options.go b/channeldb/options.go index ad22fa8ed..bc53bb47e 100644 --- a/channeldb/options.go +++ b/channeldb/options.go @@ -45,6 +45,11 @@ type Options struct { // graph cache, so we can pre-allocate the map accordingly. PreAllocCacheNumNodes int + // UseGraphCache denotes whether the in-memory graph cache should be + // used or a fallback version that uses the underlying database for + // path finding. + UseGraphCache bool + // clock is the time source used by the database. clock clock.Clock @@ -65,6 +70,7 @@ func DefaultOptions() Options { RejectCacheSize: DefaultRejectCacheSize, ChannelCacheSize: DefaultChannelCacheSize, PreAllocCacheNumNodes: DefaultPreAllocCacheNumNodes, + UseGraphCache: true, clock: clock.NewDefaultClock(), } } @@ -93,6 +99,13 @@ func OptionSetPreAllocCacheNumNodes(n int) OptionModifier { } } +// OptionSetUseGraphCache sets the UseGraphCache option to the given value. +func OptionSetUseGraphCache(use bool) OptionModifier { + return func(o *Options) { + o.UseGraphCache = use + } +} + // OptionSetSyncFreelist allows the database to sync its freelist. func OptionSetSyncFreelist(b bool) OptionModifier { return func(o *Options) { diff --git a/config_builder.go b/config_builder.go index b4f8e2b4e..a2ae613eb 100644 --- a/config_builder.go +++ b/config_builder.go @@ -847,6 +847,7 @@ func (d *DefaultDatabaseBuilder) BuildDatabase( channeldb.OptionSetChannelCacheSize(cfg.Caches.ChannelCacheSize), channeldb.OptionSetBatchCommitInterval(cfg.DB.BatchCommitInterval), channeldb.OptionDryRunMigration(cfg.DryRunMigration), + channeldb.OptionSetUseGraphCache(!cfg.DB.NoGraphCache), } // We want to pre-allocate the channel graph cache according to what we diff --git a/routing/pathfind_test.go b/routing/pathfind_test.go index dcc3b0a19..768729f33 100644 --- a/routing/pathfind_test.go +++ b/routing/pathfind_test.go @@ -173,6 +173,7 @@ func makeTestGraph() (*channeldb.ChannelGraph, kvdb.Backend, func(), error) { graph, err := channeldb.NewChannelGraph( backend, opts.RejectCacheSize, opts.ChannelCacheSize, opts.BatchCommitInterval, opts.PreAllocCacheNumNodes, + true, ) if err != nil { cleanUp()