Skip to content

Commit

Permalink
[api] API delay before readiness (#4516)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc authored Jan 7, 2025
1 parent d6886f1 commit 360260f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package api

import (
"time"

"github.com/iotexproject/iotex-core/v2/gasstation"
"github.com/iotexproject/iotex-core/v2/pkg/tracer"
)
Expand All @@ -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
Expand All @@ -41,4 +45,5 @@ var DefaultConfig = Config{
BatchRequestLimit: _defaultBatchRequestLimit,
WebsocketRateLimit: 5,
ListenerLimit: 5000,
ReadyDuration: time.Second * 30,
}
8 changes: 8 additions & 0 deletions server/itx/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http/pprof"
"runtime"
"sync"
"time"

"github.com/pkg/errors"
"go.uber.org/zap"
Expand Down Expand Up @@ -231,9 +232,16 @@ func StartServer(ctx context.Context, svr *Server, probeSvr *probe.Server, cfg c
log.L().Panic("Failed to stop server.", zap.Error(err))
}
}()
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))
time.Sleep(cfg.API.ReadyDuration)
}
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)
Expand Down

0 comments on commit 360260f

Please sign in to comment.