Skip to content

Commit dc284a3

Browse files
authored
Merge pull request #558 from SiaFoundation/nate/fix-price-pinning
Fix price pinning
2 parents b31f18c + 577fa65 commit dc284a3

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: patch
3+
---
4+
5+
# Fixed an issue with price pinning not updating prices

cmd/hostd/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ func runRootCmd(ctx context.Context, cfg config.Config, walletKey types.PrivateK
421421
if err != nil {
422422
return fmt.Errorf("failed to create pin manager: %w", err)
423423
}
424+
defer pm.Close()
424425

425426
apiOpts = append(apiOpts, api.WithPinnedSettings(pm), api.WithExplorer(ex))
426427
}

host/settings/pin/pin.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"go.sia.tech/core/types"
1212
"go.sia.tech/hostd/alerts"
1313
"go.sia.tech/hostd/host/settings"
14+
"go.sia.tech/hostd/internal/threadgroup"
1415
"go.uber.org/zap"
1516
"lukechampine.com/frand"
1617
)
@@ -80,6 +81,7 @@ type (
8081
alerts Alerts
8182
forex Forex
8283
sm SettingsManager
84+
tg *threadgroup.ThreadGroup
8385

8486
frequency time.Duration
8587
rateWindow time.Duration
@@ -252,8 +254,20 @@ func (m *Manager) Update(ctx context.Context, p PinnedSettings) error {
252254
return nil
253255
}
254256

255-
// Run starts the PinManager's update loop.
256-
func (m *Manager) Run(ctx context.Context) error {
257+
// Close closes the PinManager.
258+
func (m *Manager) Close() error {
259+
m.tg.Stop()
260+
return nil
261+
}
262+
263+
// run starts the PinManager's update loop.
264+
func (m *Manager) run() error {
265+
ctx, cancel, err := m.tg.AddContext(context.Background())
266+
if err != nil {
267+
return err
268+
}
269+
defer cancel()
270+
257271
t := time.NewTicker(m.frequency)
258272

259273
// update prices immediately
@@ -300,6 +314,7 @@ func NewManager(store Store, settings SettingsManager, f Forex, opts ...Option)
300314
sm: settings,
301315
forex: f,
302316

317+
tg: threadgroup.New(),
303318
alerts: alerts.NewNop(),
304319
log: zap.NewNop(),
305320

@@ -325,5 +340,7 @@ func NewManager(store Store, settings SettingsManager, f Forex, opts ...Option)
325340
return nil, fmt.Errorf("failed to get pinned settings: %w", err)
326341
}
327342
m.settings = pinned
343+
344+
go m.run() // run the update loop in the background
328345
return m, nil
329346
}

host/settings/pin/pin_test.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func TestPinnedFields(t *testing.T) {
131131
if err != nil {
132132
t.Fatal(err)
133133
}
134+
defer pm.Close()
134135

135136
initialSettings := sm.Settings()
136137
pin := pin.PinnedSettings{
@@ -230,19 +231,7 @@ func TestAutomaticUpdate(t *testing.T) {
230231
if err != nil {
231232
t.Fatal(err)
232233
}
233-
if err != nil {
234-
t.Fatal(err)
235-
}
236-
ctx, cancel := context.WithCancel(context.Background())
237-
defer cancel()
238-
go func() {
239-
if err := pm.Run(ctx); err != nil {
240-
if errors.Is(err, context.Canceled) {
241-
return
242-
}
243-
panic(err)
244-
}
245-
}()
234+
defer pm.Close()
246235

247236
time.Sleep(time.Second)
248237

0 commit comments

Comments
 (0)