Skip to content

Commit 4ce442e

Browse files
committed
feat(node): per-node life-cycle management
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 688a2ea commit 4ce442e

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

internal/node/load.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package node
1616

1717
import (
18+
"context"
1819
"errors"
1920
"fmt"
2021
"log/slog"
@@ -95,7 +96,7 @@ func Load(cfg *config.Config, logger *slog.Logger, immutableDir string) error {
9596
if err != nil {
9697
return fmt.Errorf("failed to load state: %w", err)
9798
}
98-
if err := ls.Start(); err != nil {
99+
if err := ls.Start(context.Background()); err != nil {
99100
return fmt.Errorf("failed to load state: %w", err)
100101
}
101102
// Open immutable DB

ledger/state.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package ledger
1616

1717
import (
18+
"context"
1819
"encoding/hex"
1920
"errors"
2021
"fmt"
@@ -300,6 +301,7 @@ type LedgerState struct {
300301
currentTip ochainsync.Tip
301302
currentEpoch models.Epoch
302303
dbWorkerPool *DatabaseWorkerPool
304+
ctx context.Context
303305
sync.RWMutex
304306
chainsyncMutex sync.Mutex
305307
chainsyncBlockfetchMutex sync.Mutex
@@ -334,7 +336,8 @@ func NewLedgerState(cfg LedgerStateConfig) (*LedgerState, error) {
334336
return ls, nil
335337
}
336338

337-
func (ls *LedgerState) Start() error {
339+
func (ls *LedgerState) Start(ctx context.Context) error {
340+
ls.ctx = ctx
338341
// Init metrics
339342
ls.metrics.init(ls.config.PromRegistry)
340343

node.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type Node struct {
4646
ouroboros *ouroborosPkg.Ouroboros
4747
shutdownFuncs []func(context.Context) error
4848
config Config
49+
ctx context.Context
50+
cancel context.CancelFunc
4951
done chan struct{}
5052
shutdownOnce sync.Once
5153
}
@@ -73,6 +75,7 @@ func (n *Node) Run() error {
7375
return err
7476
}
7577
}
78+
n.ctx, n.cancel = context.WithCancel(context.Background())
7679
// Load database
7780
dbNeedsRecovery := false
7881
dbConfig := &database.Config{
@@ -149,7 +152,7 @@ func (n *Node) Run() error {
149152
}
150153
}
151154
// Start ledger
152-
if err := n.ledgerState.Start(); err != nil {
155+
if err := n.ledgerState.Start(n.ctx); err != nil {
153156
return fmt.Errorf("failed to start ledger: %w", err)
154157
}
155158
// Initialize mempool
@@ -229,7 +232,7 @@ func (n *Node) Run() error {
229232
}
230233

231234
// Wait for shutdown signal
232-
<-n.done
235+
<-n.ctx.Done()
233236
return nil
234237
}
235238

@@ -249,6 +252,9 @@ func (n *Node) shutdown() error {
249252
}
250253
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
251254
defer cancel()
255+
if n.cancel != nil {
256+
n.cancel()
257+
}
252258

253259
var err error
254260

0 commit comments

Comments
 (0)