From 3c2338a008df86e2dad230a70c3a1004456cb4db Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 7 Jan 2025 07:52:49 -0800 Subject: [PATCH 1/2] cmd: start pin loop --- ..._with_price_pinning_not_updating_prices.md | 5 +++++ cmd/hostd/run.go | 1 + host/settings/pin/pin.go | 21 +++++++++++++++++-- host/settings/pin/pin_test.go | 14 +------------ 4 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 .changeset/fixed_an_issue_with_price_pinning_not_updating_prices.md diff --git a/.changeset/fixed_an_issue_with_price_pinning_not_updating_prices.md b/.changeset/fixed_an_issue_with_price_pinning_not_updating_prices.md new file mode 100644 index 00000000..f49cbb24 --- /dev/null +++ b/.changeset/fixed_an_issue_with_price_pinning_not_updating_prices.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +# Fixed an issue with price pinning not updating prices diff --git a/cmd/hostd/run.go b/cmd/hostd/run.go index d14df28c..6d854acd 100644 --- a/cmd/hostd/run.go +++ b/cmd/hostd/run.go @@ -421,6 +421,7 @@ func runRootCmd(ctx context.Context, cfg config.Config, walletKey types.PrivateK if err != nil { return fmt.Errorf("failed to create pin manager: %w", err) } + defer pm.Close() apiOpts = append(apiOpts, api.WithPinnedSettings(pm), api.WithExplorer(ex)) } diff --git a/host/settings/pin/pin.go b/host/settings/pin/pin.go index b67b7144..debf49be 100644 --- a/host/settings/pin/pin.go +++ b/host/settings/pin/pin.go @@ -11,6 +11,7 @@ import ( "go.sia.tech/core/types" "go.sia.tech/hostd/alerts" "go.sia.tech/hostd/host/settings" + "go.sia.tech/hostd/internal/threadgroup" "go.uber.org/zap" "lukechampine.com/frand" ) @@ -80,6 +81,7 @@ type ( alerts Alerts forex Forex sm SettingsManager + tg *threadgroup.ThreadGroup frequency time.Duration rateWindow time.Duration @@ -252,8 +254,20 @@ func (m *Manager) Update(ctx context.Context, p PinnedSettings) error { return nil } -// Run starts the PinManager's update loop. -func (m *Manager) Run(ctx context.Context) error { +// Close closes the PinManager. +func (m *Manager) Close() error { + m.tg.Stop() + return nil +} + +// run starts the PinManager's update loop. +func (m *Manager) run() error { + ctx, cancel, err := m.tg.AddContext(context.Background()) + if err != nil { + return err + } + defer cancel() + t := time.NewTicker(m.frequency) // update prices immediately @@ -300,6 +314,7 @@ func NewManager(store Store, settings SettingsManager, f Forex, opts ...Option) sm: settings, forex: f, + tg: threadgroup.New(), alerts: alerts.NewNop(), log: zap.NewNop(), @@ -325,5 +340,7 @@ func NewManager(store Store, settings SettingsManager, f Forex, opts ...Option) return nil, fmt.Errorf("failed to get pinned settings: %w", err) } m.settings = pinned + + go m.run() // run the update loop in the background return m, nil } diff --git a/host/settings/pin/pin_test.go b/host/settings/pin/pin_test.go index 4c0ea580..be8d415d 100644 --- a/host/settings/pin/pin_test.go +++ b/host/settings/pin/pin_test.go @@ -230,19 +230,7 @@ func TestAutomaticUpdate(t *testing.T) { if err != nil { t.Fatal(err) } - if err != nil { - t.Fatal(err) - } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - go func() { - if err := pm.Run(ctx); err != nil { - if errors.Is(err, context.Canceled) { - return - } - panic(err) - } - }() + defer pm.Close() time.Sleep(time.Second) From 577fa658b60369f803966c5f2388f246bf1d1120 Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 7 Jan 2025 08:36:19 -0800 Subject: [PATCH 2/2] pinner: fix missing Close --- host/settings/pin/pin_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/host/settings/pin/pin_test.go b/host/settings/pin/pin_test.go index be8d415d..7122c4fd 100644 --- a/host/settings/pin/pin_test.go +++ b/host/settings/pin/pin_test.go @@ -131,6 +131,7 @@ func TestPinnedFields(t *testing.T) { if err != nil { t.Fatal(err) } + defer pm.Close() initialSettings := sm.Settings() pin := pin.PinnedSettings{