Skip to content

Commit

Permalink
soroban-rpc: Check DB on health endpoint (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio authored Feb 23, 2023
1 parent 6b47801 commit 6dc54bd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type HandlerParams struct {
// NewJSONRPCHandler constructs a Handler instance
func NewJSONRPCHandler(cfg *config.LocalConfig, params HandlerParams) (Handler, error) {
bridge := jhttp.NewBridge(handler.Map{
"getHealth": methods.NewHealthCheck(),
"getHealth": methods.NewHealthCheck(params.LedgerEntryReader),
"getEvents": methods.NewGetEventsHandler(params.EventStore, cfg.MaxEventsLimit, cfg.DefaultEventsLimit),
"getNetwork": methods.NewGetNetworkHandler(cfg.NetworkPassphrase, cfg.FriendbotURL, params.CoreClient),
"getLedgerEntry": methods.NewGetLedgerEntryHandler(params.Logger, params.LedgerEntryReader),
Expand Down
15 changes: 12 additions & 3 deletions cmd/soroban-rpc/internal/methods/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@ import (
"context"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/code"
"github.com/creachadair/jrpc2/handler"

"github.com/stellar/soroban-tools/cmd/soroban-rpc/internal/db"
)

type HealthCheckResult struct {
Status string `json:"status"`
}

// NewHealthCheck returns a health check json rpc handler
func NewHealthCheck() jrpc2.Handler {
return handler.New(func(context.Context) HealthCheckResult {
return HealthCheckResult{Status: "healthy"}
func NewHealthCheck(reader db.LedgerEntryReader) jrpc2.Handler {
return handler.New(func(ctx context.Context) (HealthCheckResult, error) {
if _, err := reader.GetLatestLedgerSequence(ctx); err != nil {
return HealthCheckResult{}, jrpc2.Error{
Code: code.InternalError,
Message: err.Error(),
}
}
return HealthCheckResult{Status: "healthy"}, nil
})
}

0 comments on commit 6dc54bd

Please sign in to comment.