Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!(nodbuilder): making lifecycle timeouts configurable #2347

Merged
merged 5 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}