Skip to content

Commit c94f2cf

Browse files
authored
turn off server when block received is out of date (#4722)
1 parent 2d71b23 commit c94f2cf

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

pkg/probe/probe.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"context"
1010
"fmt"
1111
"net/http"
12+
"time"
1213

1314
"github.com/prometheus/client_golang/prometheus/promhttp"
1415
"go.uber.org/zap"
1516

17+
"github.com/iotexproject/iotex-core/v2/blockchain/block"
1618
"github.com/iotexproject/iotex-core/v2/pkg/lifecycle"
1719
"github.com/iotexproject/iotex-core/v2/pkg/log"
1820
"github.com/iotexproject/iotex-core/v2/pkg/util/httputil"
@@ -76,6 +78,20 @@ func (s *Server) Start(_ context.Context) error {
7678
// Stop shutdown the probe server.
7779
func (s *Server) Stop(ctx context.Context) error { return s.server.Shutdown(ctx) }
7880

81+
// ReceiveBlock receives the new block and update the readiness status
82+
func (s *Server) ReceiveBlock(blk *block.Block) error {
83+
if time.Now().After(blk.CommitTime().Add(10 * time.Second)) {
84+
if s.IsReady() {
85+
s.TurnOff()
86+
}
87+
} else {
88+
if !s.IsReady() {
89+
s.TurnOn()
90+
}
91+
}
92+
return nil
93+
}
94+
7995
func successHandleFunc(w http.ResponseWriter, _ *http.Request) {
8096
w.WriteHeader(http.StatusOK)
8197
if _, err := w.Write([]byte("OK")); err != nil {

pkg/probe/probe_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import (
1313

1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/require"
16+
"google.golang.org/protobuf/types/known/timestamppb"
1617

18+
"github.com/iotexproject/iotex-core/v2/blockchain/block"
1719
"github.com/iotexproject/iotex-core/v2/testutil"
20+
"github.com/iotexproject/iotex-proto/golang/iotextypes"
1821
)
1922

2023
type testCase struct {
@@ -73,6 +76,22 @@ func TestBasicProbe(t *testing.T) {
7376
require.NoError(t, s.TurnOff())
7477
testFunc(t, test1)
7578

79+
now := time.Now()
80+
createFooter := func(ts time.Time) block.Footer {
81+
footer := block.Footer{}
82+
require.NoError(t, footer.ConvertFromBlockFooterPb(
83+
&iotextypes.BlockFooter{
84+
Timestamp: timestamppb.New(ts),
85+
},
86+
))
87+
return footer
88+
}
89+
90+
require.NoError(t, s.ReceiveBlock(&block.Block{Footer: createFooter(now.Add(-9 * time.Second))}))
91+
require.True(t, s.IsReady())
92+
require.NoError(t, s.ReceiveBlock(&block.Block{Footer: createFooter(now.Add(-11 * time.Second))}))
93+
require.False(t, s.IsReady())
94+
7695
require.NoError(t, s.Stop(ctx))
7796
_, err := http.Get("http://localhost:7788/liveness")
7897
require.Error(t, err)

server/itx/server.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ func StartServer(ctx context.Context, svr *Server, probeSvr *probe.Server, cfg c
277277
log.L().Info("Waiting for server to be ready.", zap.Duration("duration", cfg.API.ReadyDuration))
278278
time.Sleep(cfg.API.ReadyDuration)
279279
}
280+
if err := svr.rootChainService.Blockchain().AddSubscriber(probeSvr); err != nil {
281+
log.L().Panic("Failed to add probe server as subscriber.", zap.Error(err))
282+
}
280283
if err := probeSvr.TurnOn(); err != nil {
281284
log.L().Panic("Failed to turn on probe server.", zap.Error(err))
282285
}
@@ -330,7 +333,9 @@ func StartServer(ctx context.Context, svr *Server, probeSvr *probe.Server, cfg c
330333
}
331334

332335
<-ctx.Done()
333-
if err := probeSvr.TurnOff(); err != nil {
334-
log.L().Panic("Failed to turn off probe server.", zap.Error(err))
336+
if probeSvr.IsReady() {
337+
if err := probeSvr.TurnOff(); err != nil {
338+
log.L().Panic("Failed to turn off probe server.", zap.Error(err))
339+
}
335340
}
336341
}

0 commit comments

Comments
 (0)