Skip to content

Commit

Permalink
cmd: start pin loop
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Jan 7, 2025
1 parent b31f18c commit 3c2338a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

# Fixed an issue with price pinning not updating prices
1 change: 1 addition & 0 deletions cmd/hostd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
21 changes: 19 additions & 2 deletions host/settings/pin/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -80,6 +81,7 @@ type (
alerts Alerts
forex Forex
sm SettingsManager
tg *threadgroup.ThreadGroup

frequency time.Duration
rateWindow time.Duration
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),

Expand All @@ -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
}
14 changes: 1 addition & 13 deletions host/settings/pin/pin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 3c2338a

Please sign in to comment.