From 7d5dc5048657fbc4c2ead4314924335ae3843e12 Mon Sep 17 00:00:00 2001 From: envestcc Date: Fri, 6 Dec 2024 10:33:50 +0800 Subject: [PATCH 1/3] delay ready for api node --- api/config.go | 4 ++++ server/itx/server.go | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/api/config.go b/api/config.go index d6800498fb..e6059ebd2e 100644 --- a/api/config.go +++ b/api/config.go @@ -6,6 +6,8 @@ package api import ( + "time" + "github.com/iotexproject/iotex-core/v2/gasstation" "github.com/iotexproject/iotex-core/v2/pkg/tracer" ) @@ -27,6 +29,8 @@ type Config struct { WebsocketRateLimit int `yaml:"websocketRateLimit"` // ListenerLimit is the maximum number of listeners. ListenerLimit int `yaml:"listenerLimit"` + // ReadyDuration is the duration to wait for the server to be ready. + ReadyDuration time.Duration `yaml:"readyDuration"` } // DefaultConfig is the default config diff --git a/server/itx/server.go b/server/itx/server.go index 013fe1010c..a9171bf06d 100644 --- a/server/itx/server.go +++ b/server/itx/server.go @@ -12,6 +12,7 @@ import ( "net/http/pprof" "runtime" "sync" + "time" "github.com/pkg/errors" "go.uber.org/zap" @@ -231,9 +232,18 @@ func StartServer(ctx context.Context, svr *Server, probeSvr *probe.Server, cfg c log.L().Panic("Failed to stop server.", zap.Error(err)) } }() + if cfg.API.ReadyDuration > 0 { + // wait for a while to make sure the server is ready + // The original intention was to ensure that all transactions that were not received during the restart were included in block, thereby avoiding inconsistencies in the state of the API node. + log.L().Info("Waiting for server to be ready.", zap.Duration("duration", cfg.API.ReadyDuration)) + readyTimer := time.NewTimer(cfg.API.ReadyDuration) + <-readyTimer.C + readyTimer.Stop() + } if err := probeSvr.TurnOn(); err != nil { log.L().Panic("Failed to turn on probe server.", zap.Error(err)) } + log.L().Info("Server is ready.") if cfg.System.HeartbeatInterval > 0 { task := routine.NewRecurringTask(NewHeartbeatHandler(svr, cfg.Network).Log, cfg.System.HeartbeatInterval) From fdf670d46e42493f9be36327766e21731ee1aefd Mon Sep 17 00:00:00 2001 From: envestcc Date: Mon, 9 Dec 2024 11:53:58 +0800 Subject: [PATCH 2/3] only enable for gateway --- api/config.go | 1 + server/itx/server.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/api/config.go b/api/config.go index e6059ebd2e..c98dfb538e 100644 --- a/api/config.go +++ b/api/config.go @@ -45,4 +45,5 @@ var DefaultConfig = Config{ BatchRequestLimit: _defaultBatchRequestLimit, WebsocketRateLimit: 5, ListenerLimit: 5000, + ReadyDuration: time.Second * 30, } diff --git a/server/itx/server.go b/server/itx/server.go index a9171bf06d..166a254986 100644 --- a/server/itx/server.go +++ b/server/itx/server.go @@ -232,7 +232,7 @@ func StartServer(ctx context.Context, svr *Server, probeSvr *probe.Server, cfg c log.L().Panic("Failed to stop server.", zap.Error(err)) } }() - if cfg.API.ReadyDuration > 0 { + if _, isGateway := cfg.Plugins[config.GatewayPlugin]; isGateway && cfg.API.ReadyDuration > 0 { // wait for a while to make sure the server is ready // The original intention was to ensure that all transactions that were not received during the restart were included in block, thereby avoiding inconsistencies in the state of the API node. log.L().Info("Waiting for server to be ready.", zap.Duration("duration", cfg.API.ReadyDuration)) From 62ef777b0781cc2aeb28b512266995783534c3fc Mon Sep 17 00:00:00 2001 From: envestcc Date: Tue, 7 Jan 2025 12:28:45 +0800 Subject: [PATCH 3/3] address comment --- server/itx/server.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/server/itx/server.go b/server/itx/server.go index 166a254986..8796795036 100644 --- a/server/itx/server.go +++ b/server/itx/server.go @@ -236,9 +236,7 @@ func StartServer(ctx context.Context, svr *Server, probeSvr *probe.Server, cfg c // wait for a while to make sure the server is ready // The original intention was to ensure that all transactions that were not received during the restart were included in block, thereby avoiding inconsistencies in the state of the API node. log.L().Info("Waiting for server to be ready.", zap.Duration("duration", cfg.API.ReadyDuration)) - readyTimer := time.NewTimer(cfg.API.ReadyDuration) - <-readyTimer.C - readyTimer.Stop() + time.Sleep(cfg.API.ReadyDuration) } if err := probeSvr.TurnOn(); err != nil { log.L().Panic("Failed to turn on probe server.", zap.Error(err))