Skip to content

Commit

Permalink
feat!(nodbuilder): making lifecycle timeouts configurable (#2347)
Browse files Browse the repository at this point in the history
Closes #2315 

Doing this proactively even though it is a good-first issue in case
other dagstore startup issues are not fixed in time. Breaks the config.
  • Loading branch information
distractedm1nd authored Jun 14, 2023
1 parent 4c87c69 commit 400517e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
2 changes: 2 additions & 0 deletions nodebuilder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ConfigLoader func() (*Config, error)
// Config is main configuration structure for a Node.
// It combines configuration units for all Node subsystems.
type Config struct {
Node node.Config
Core core.Config
State state.Config
P2P p2p.Config
Expand All @@ -39,6 +40,7 @@ type Config struct {
// NOTE: Currently, configs are identical, but this will change.
func DefaultConfig(tp node.Type) *Config {
commonConfig := &Config{
Node: node.DefaultConfig(tp),
Core: core.DefaultConfig(),
State: state.DefaultConfig(),
P2P: p2p.DefaultConfig(tp),
Expand Down
16 changes: 2 additions & 14 deletions nodebuilder/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"strings"
"time"

"github.com/cristalhq/jwt"
"github.com/ipfs/go-blockservice"
Expand Down Expand Up @@ -95,7 +94,7 @@ func NewWithConfig(tp node.Type, network p2p.Network, store Store, cfg *Config,

// Start launches the Node and all its components and services.
func (n *Node) Start(ctx context.Context) error {
to := timeout(n.Type)
to := n.Config.Node.StartupTimeout
ctx, cancel := context.WithTimeout(ctx, to)
defer cancel()

Expand Down Expand Up @@ -141,7 +140,7 @@ func (n *Node) Run(ctx context.Context) error {
// Canceling the given context earlier 'ctx' unblocks the Stop and aborts graceful shutdown forcing
// remaining Modules/Services to close immediately.
func (n *Node) Stop(ctx context.Context) error {
to := timeout(n.Type)
to := n.Config.Node.ShutdownTimeout
ctx, cancel := context.WithTimeout(ctx, to)
defer cancel()

Expand Down Expand Up @@ -183,14 +182,3 @@ func newNode(opts ...fx.Option) (*Node, error) {

// lifecycleFunc defines a type for common lifecycle funcs.
type lifecycleFunc func(context.Context) error

var DefaultLifecycleTimeout = time.Minute * 2

func timeout(tp node.Type) time.Duration {
switch tp {
case node.Light:
return time.Second * 20
default:
return DefaultLifecycleTimeout
}
}
38 changes: 38 additions & 0 deletions nodebuilder/node/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package node

import (
"fmt"
"time"
)

var defaultLifecycleTimeout = time.Minute * 2

type Config struct {
StartupTimeout time.Duration
ShutdownTimeout time.Duration
}

// DefaultConfig returns the default node configuration for a given node type.
func DefaultConfig(tp Type) Config {
var timeout time.Duration
switch tp {
case Light:
timeout = time.Second * 20
default:
timeout = defaultLifecycleTimeout
}
return Config{
StartupTimeout: timeout,
ShutdownTimeout: timeout,
}
}

func (c *Config) Validate() error {
if c.StartupTimeout == 0 {
return fmt.Errorf("invalid startup timeout: %v", c.StartupTimeout)
}
if c.ShutdownTimeout == 0 {
return fmt.Errorf("invalid shutdown timeout: %v", c.ShutdownTimeout)
}
return nil
}

0 comments on commit 400517e

Please sign in to comment.