autopilot: continue threading context

Remove one context.TODO and add one more.
This commit is contained in:
Elle Mouton
2025-04-09 06:31:05 +02:00
committed by ziggie
parent cd4a59071d
commit 46f09ba1ff
5 changed files with 17 additions and 7 deletions

View File

@@ -169,8 +169,8 @@ func betweennessCentrality(g *SimpleGraph, s int, centrality []float64) {
}
// Refresh recalculates and stores centrality values.
func (bc *BetweennessCentrality) Refresh(graph ChannelGraph) error {
ctx := context.TODO()
func (bc *BetweennessCentrality) Refresh(ctx context.Context,
graph ChannelGraph) error {
cache, err := NewSimpleGraph(ctx, graph)
if err != nil {

View File

@@ -1,6 +1,7 @@
package autopilot
import (
"context"
"fmt"
"testing"
@@ -30,6 +31,9 @@ func TestBetweennessCentralityMetricConstruction(t *testing.T) {
// Tests that empty graph results in empty centrality result.
func TestBetweennessCentralityEmptyGraph(t *testing.T) {
t.Parallel()
ctx := context.Background()
centralityMetric, err := NewBetweennessCentralityMetric(1)
require.NoError(
t, err,
@@ -42,7 +46,7 @@ func TestBetweennessCentralityEmptyGraph(t *testing.T) {
require.NoError(t, err, "unable to create graph")
success := t.Run(chanGraph.name, func(t1 *testing.T) {
err = centralityMetric.Refresh(graph)
err = centralityMetric.Refresh(ctx, graph)
require.NoError(t1, err)
centrality := centralityMetric.GetMetric(false)
@@ -59,6 +63,9 @@ func TestBetweennessCentralityEmptyGraph(t *testing.T) {
// Test betweenness centrality calculating using an example graph.
func TestBetweennessCentralityWithNonEmptyGraph(t *testing.T) {
t.Parallel()
ctx := context.Background()
workers := []int{1, 3, 9, 100}
tests := []struct {
@@ -100,7 +107,7 @@ func TestBetweennessCentralityWithNonEmptyGraph(t *testing.T) {
t1, graph, centralityTestGraph,
)
err = metric.Refresh(graph)
err = metric.Refresh(ctx, graph)
require.NoError(t1, err)
for _, expected := range tests {

View File

@@ -157,7 +157,7 @@ type NodeMetric interface {
Name() string
// Refresh refreshes the metric values based on the current graph.
Refresh(graph ChannelGraph) error
Refresh(ctx context.Context, graph ChannelGraph) error
// GetMetric returns the latest value of this metric. Values in the
// map are per node and can be in arbitrary domain. If normalize is

View File

@@ -1,6 +1,7 @@
package autopilot
import (
"context"
"runtime"
"github.com/btcsuite/btcd/btcutil"
@@ -54,8 +55,10 @@ func (g *TopCentrality) NodeScores(graph ChannelGraph, chans []LocalChannel,
chanSize btcutil.Amount, nodes map[NodeID]struct{}) (
map[NodeID]*NodeScore, error) {
ctx := context.TODO()
// Calculate betweenness centrality for the whole graph.
if err := g.centralityMetric.Refresh(graph); err != nil {
if err := g.centralityMetric.Refresh(ctx, graph); err != nil {
return nil, err
}