From 9df330a9daf2349bc848eef9c6e6523461f99a7e Mon Sep 17 00:00:00 2001 From: Kien Dang Date: Mon, 27 Nov 2023 16:37:59 +0700 Subject: [PATCH] fix: stats lock all routines when it cannot connect to server (#92) --- stats/bridge_stats.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/stats/bridge_stats.go b/stats/bridge_stats.go index 972681b..9a70f36 100644 --- a/stats/bridge_stats.go +++ b/stats/bridge_stats.go @@ -6,6 +6,7 @@ import ( "net/http" "strings" "sync" + "sync/atomic" "time" bridgeCore "github.com/axieinfinity/bridge-core" @@ -15,7 +16,7 @@ import ( "gorm.io/gorm" ) -const bridgeVersion = "v2" +const bridgeVersion = "v0.2.7" type NodeInfo struct { Organization string `json:"organization,omitempty" mapstructure:"organization"` @@ -126,6 +127,7 @@ type Service struct { processedBlockCh chan processedBlockMessage quitCh chan struct{} store stores.TaskStore + isReady atomic.Bool } func NewService(node, chainId, operator, host, secret string, db *gorm.DB) { @@ -297,6 +299,8 @@ func (s *Service) report(conn *connWrapper) error { func (s *Service) readLoop(conn *connWrapper) { // If the read loop exits, close the connection defer conn.Close() + // set isReady to true + s.isReady.Store(true) log.Info("[Bridge stats] Start read loop") for { // Exit the function when receiving the quit signal @@ -348,9 +352,17 @@ func (s *Service) setProcessedBlock(listener string, block uint64) error { } func (s *Service) SendError(listener, err string) { + if !s.isReady.Load() { + log.Info("Stats is not ready to broadcast error") + return + } s.errCh <- errorMessage{Listener: listener, Err: err} } func (s *Service) SendProcessedBlock(listener string, block uint64) { + if !s.isReady.Load() { + log.Info("Stats is not ready to broadcast processed block") + return + } s.processedBlockCh <- processedBlockMessage{Listener: listener, ProcessedBlock: block} }