From 8684abeb3eed84e17c576443d76c491231b1b3e7 Mon Sep 17 00:00:00 2001 From: pritsheth Date: Thu, 18 Apr 2024 17:24:09 +0530 Subject: [PATCH] Cache captive core version during bootup and add retry logic --- Makefile | 3 +-- cmd/soroban-rpc/internal/config/version.go | 3 +++ cmd/soroban-rpc/internal/daemon/daemon.go | 19 +++++++++++++++++++ .../internal/methods/get_version_info.go | 13 ++----------- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 1c401fdb..278f6713 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,6 @@ all: check build test export RUSTFLAGS=-Dwarnings -Dclippy::all -Dclippy::pedantic - REPOSITORY_COMMIT_HASH := "$(shell git rev-parse HEAD)" ifeq (${REPOSITORY_COMMIT_HASH},"") $(error failed to retrieve git head commit hash) @@ -18,7 +17,7 @@ BUILD_TIMESTAMP ?= $(shell date '+%Y-%m-%dT%H:%M:%S') GOLDFLAGS := -X 'github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config.Version=${REPOSITORY_VERSION}' \ -X 'github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config.CommitHash=${REPOSITORY_COMMIT_HASH}' \ -X 'github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config.BuildTimestamp=${BUILD_TIMESTAMP}' \ - -X 'github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config.Branch=${REPOSITORY_BRANCH}' \ + -X 'github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/config.Branch=${REPOSITORY_BRANCH}' # The following works around incompatibility between the rust and the go linkers - diff --git a/cmd/soroban-rpc/internal/config/version.go b/cmd/soroban-rpc/internal/config/version.go index 909fdaea..5c334b36 100644 --- a/cmd/soroban-rpc/internal/config/version.go +++ b/cmd/soroban-rpc/internal/config/version.go @@ -12,4 +12,7 @@ var ( // Branch is the git branch from which the soroban-rpc was built, injected during build time. Branch = "" + + // CaptiveCoreVersion is the Build value from /Info endpoint of core, injected during daemon.run() + CaptiveCoreVersion = "" ) diff --git a/cmd/soroban-rpc/internal/daemon/daemon.go b/cmd/soroban-rpc/internal/daemon/daemon.go index c2c224de..b3d7eb89 100644 --- a/cmd/soroban-rpc/internal/daemon/daemon.go +++ b/cmd/soroban-rpc/internal/daemon/daemon.go @@ -3,6 +3,7 @@ package daemon import ( "context" "errors" + "github.com/cenkalti/backoff/v4" "net/http" "net/http/pprof" //nolint:gosec "os" @@ -185,6 +186,24 @@ func MustNew(cfg *config.Config) *Daemon { }, metricsRegistry), } + fetchCaptiveCoreVersion := func() error { + coreClient := daemon.CoreClient() + info, err := coreClient.Info(context.Background()) + if err != nil { + return err + } + config.CaptiveCoreVersion = info.Info.Build + return nil + } + + backoffStrategy := backoff.NewExponentialBackOff() + backoffStrategy.MaxElapsedTime = 4 * time.Second + err = backoff.Retry(fetchCaptiveCoreVersion, backoffStrategy) + if err != nil { + logger.WithError(err). + Info("error occurred while calling Info endpoint of core") + } + eventStore := events.NewMemoryStore( daemon, cfg.NetworkPassphrase, diff --git a/cmd/soroban-rpc/internal/methods/get_version_info.go b/cmd/soroban-rpc/internal/methods/get_version_info.go index ab2a38a1..0aa42fb4 100644 --- a/cmd/soroban-rpc/internal/methods/get_version_info.go +++ b/cmd/soroban-rpc/internal/methods/get_version_info.go @@ -22,18 +22,9 @@ type GetVersionInfoResponse struct { } func NewGetVersionInfoHandler(logger *log.Entry, ledgerEntryReader db.LedgerEntryReader, ledgerReader db.LedgerReader, daemon interfaces.Daemon) jrpc2.Handler { - coreClient := daemon.CoreClient() + //coreClient := daemon.CoreClient() return handler.New(func(ctx context.Context, request GetVersionInfoRequest) (GetVersionInfoResponse, error) { - var captiveCoreVersion string - info, err := coreClient.Info(ctx) - if err != nil { - logger.WithError(err).WithField("request", request). - Info("error occurred while calling Info endpoint of core") - } else { - captiveCoreVersion = info.Info.Build - } - // Fetch Protocol version var protocolVersion uint32 readTx, err := ledgerEntryReader.NewCachedTx(ctx) @@ -61,7 +52,7 @@ func NewGetVersionInfoHandler(logger *log.Entry, ledgerEntryReader db.LedgerEntr Version: config.Version, CommitHash: config.CommitHash, BuildTimestamp: config.BuildTimestamp, - CaptiveCoreVersion: captiveCoreVersion, + CaptiveCoreVersion: config.CaptiveCoreVersion, ProtocolVersion: protocolVersion, }, nil })