Skip to content

Commit ca44110

Browse files
committed
[api] API delay before readiness (#4516)
1 parent d45bb0c commit ca44110

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

api/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package api
77

88
import (
9+
"time"
10+
911
"github.com/iotexproject/iotex-core/v2/gasstation"
1012
"github.com/iotexproject/iotex-core/v2/pkg/tracer"
1113
)
@@ -27,6 +29,8 @@ type Config struct {
2729
WebsocketRateLimit int `yaml:"websocketRateLimit"`
2830
// ListenerLimit is the maximum number of listeners.
2931
ListenerLimit int `yaml:"listenerLimit"`
32+
// ReadyDuration is the duration to wait for the server to be ready.
33+
ReadyDuration time.Duration `yaml:"readyDuration"`
3034
}
3135

3236
// DefaultConfig is the default config
@@ -41,4 +45,5 @@ var DefaultConfig = Config{
4145
BatchRequestLimit: _defaultBatchRequestLimit,
4246
WebsocketRateLimit: 5,
4347
ListenerLimit: 5000,
48+
ReadyDuration: time.Second * 30,
4449
}

server/itx/server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http/pprof"
1313
"runtime"
1414
"sync"
15+
"time"
1516

1617
"github.com/pkg/errors"
1718
"go.uber.org/zap"
@@ -231,9 +232,16 @@ func StartServer(ctx context.Context, svr *Server, probeSvr *probe.Server, cfg c
231232
log.L().Panic("Failed to stop server.", zap.Error(err))
232233
}
233234
}()
235+
if _, isGateway := cfg.Plugins[config.GatewayPlugin]; isGateway && cfg.API.ReadyDuration > 0 {
236+
// wait for a while to make sure the server is ready
237+
// 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.
238+
log.L().Info("Waiting for server to be ready.", zap.Duration("duration", cfg.API.ReadyDuration))
239+
time.Sleep(cfg.API.ReadyDuration)
240+
}
234241
if err := probeSvr.TurnOn(); err != nil {
235242
log.L().Panic("Failed to turn on probe server.", zap.Error(err))
236243
}
244+
log.L().Info("Server is ready.")
237245

238246
if cfg.System.HeartbeatInterval > 0 {
239247
task := routine.NewRecurringTask(NewHeartbeatHandler(svr, cfg.Network).Log, cfg.System.HeartbeatInterval)

0 commit comments

Comments
 (0)