From 6dc54bdadc42ce06a468c0a4fb2f4ba46386f9a1 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Thu, 23 Feb 2023 19:32:35 +0100 Subject: [PATCH] soroban-rpc: Check DB on health endpoint (#449) --- cmd/soroban-rpc/internal/jsonrpc.go | 2 +- cmd/soroban-rpc/internal/methods/health.go | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/cmd/soroban-rpc/internal/jsonrpc.go b/cmd/soroban-rpc/internal/jsonrpc.go index 3c05dd6f7..a4067729b 100644 --- a/cmd/soroban-rpc/internal/jsonrpc.go +++ b/cmd/soroban-rpc/internal/jsonrpc.go @@ -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), diff --git a/cmd/soroban-rpc/internal/methods/health.go b/cmd/soroban-rpc/internal/methods/health.go index dfabb6d2c..204c1e609 100644 --- a/cmd/soroban-rpc/internal/methods/health.go +++ b/cmd/soroban-rpc/internal/methods/health.go @@ -4,7 +4,10 @@ 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 { @@ -12,8 +15,14 @@ type HealthCheckResult struct { } // 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 }) }