Skip to content

Commit

Permalink
feat(rpc/node): readiness check (#3118)
Browse files Browse the repository at this point in the history
Implements a readiness check on the node module.

Question: I have set the permission level to public, but remember that
we removed all public methods in favor of `read`. Is this fine or should
this method also be `read`?
____________
Comes from a conversation with @joroshiba from Astria

---------

Co-authored-by: ramin <[email protected]>
  • Loading branch information
distractedm1nd and ramin authored Jan 29, 2024
1 parent 4850c90 commit 9484e15
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nodebuilder/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func ConstructModule(tp node.Type, network p2p.Network, cfg *Config, store Store
state.ConstructModule(tp, &cfg.State, &cfg.Core),
modhead.ConstructModule[*header.ExtendedHeader](tp, &cfg.Header),
share.ConstructModule(tp, &cfg.Share),
rpc.ConstructModule(tp, &cfg.RPC),
gateway.ConstructModule(tp, &cfg.Gateway),
core.ConstructModule(tp, &cfg.Core),
das.ConstructModule(tp, &cfg.DASer),
fraud.ConstructModule(tp),
blob.ConstructModule(),
node.ConstructModule(tp),
prune.ConstructModule(tp),
rpc.ConstructModule(tp, &cfg.RPC),
)

return fx.Module(
Expand Down
8 changes: 8 additions & 0 deletions nodebuilder/node/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func (m *module) Info(context.Context) (Info, error) {
}, nil
}

func (m *module) Ready(context.Context) (bool, error) {
// Because the node uses FX to provide the RPC last, all services' lifecycles have been started by
// the point this endpoint is available. It is not 100% guaranteed at this point that all services
// are fully ready, but it is very high probability and all endpoints are available at this point
// regardless.
return true, nil
}

func (m *module) LogLevelSet(_ context.Context, name, level string) error {
return logging.SetLogLevel(name, level)
}
Expand Down
8 changes: 8 additions & 0 deletions nodebuilder/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type Module interface {
// Info returns administrative information about the node.
Info(context.Context) (Info, error)

// Ready returns true once the node's RPC is ready to accept requests.
Ready(context.Context) (bool, error)

// LogLevelSet sets the given component log level to the given level.
LogLevelSet(ctx context.Context, name, level string) error

Expand All @@ -28,6 +31,7 @@ var _ Module = (*API)(nil)
type API struct {
Internal struct {
Info func(context.Context) (Info, error) `perm:"admin"`
Ready func(context.Context) (bool, error) `perm:"read"`
LogLevelSet func(ctx context.Context, name, level string) error `perm:"admin"`
AuthVerify func(ctx context.Context, token string) ([]auth.Permission, error) `perm:"admin"`
AuthNew func(ctx context.Context, perms []auth.Permission) (string, error) `perm:"admin"`
Expand All @@ -38,6 +42,10 @@ func (api *API) Info(ctx context.Context) (Info, error) {
return api.Internal.Info(ctx)
}

func (api *API) Ready(ctx context.Context) (bool, error) {
return api.Internal.Ready(ctx)
}

func (api *API) LogLevelSet(ctx context.Context, name, level string) error {
return api.Internal.LogLevelSet(ctx, name, level)
}
Expand Down
4 changes: 4 additions & 0 deletions nodebuilder/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func TestNodeModule(t *testing.T) {
require.NoError(t, err)
require.Equal(t, info.APIVersion, node.APIVersion)

ready, err := client.Node.Ready(ctx)
require.NoError(t, err)
require.True(t, ready)

perms, err := client.Node.AuthVerify(ctx, jwt)
require.NoError(t, err)
require.Equal(t, perms, adminPerms)
Expand Down

0 comments on commit 9484e15

Please sign in to comment.