Skip to content

Commit 9df330a

Browse files
authored
fix: stats lock all routines when it cannot connect to server (#92)
1 parent e52a55b commit 9df330a

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

stats/bridge_stats.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"strings"
88
"sync"
9+
"sync/atomic"
910
"time"
1011

1112
bridgeCore "github.com/axieinfinity/bridge-core"
@@ -15,7 +16,7 @@ import (
1516
"gorm.io/gorm"
1617
)
1718

18-
const bridgeVersion = "v2"
19+
const bridgeVersion = "v0.2.7"
1920

2021
type NodeInfo struct {
2122
Organization string `json:"organization,omitempty" mapstructure:"organization"`
@@ -126,6 +127,7 @@ type Service struct {
126127
processedBlockCh chan processedBlockMessage
127128
quitCh chan struct{}
128129
store stores.TaskStore
130+
isReady atomic.Bool
129131
}
130132

131133
func NewService(node, chainId, operator, host, secret string, db *gorm.DB) {
@@ -297,6 +299,8 @@ func (s *Service) report(conn *connWrapper) error {
297299
func (s *Service) readLoop(conn *connWrapper) {
298300
// If the read loop exits, close the connection
299301
defer conn.Close()
302+
// set isReady to true
303+
s.isReady.Store(true)
300304
log.Info("[Bridge stats] Start read loop")
301305
for {
302306
// Exit the function when receiving the quit signal
@@ -348,9 +352,17 @@ func (s *Service) setProcessedBlock(listener string, block uint64) error {
348352
}
349353

350354
func (s *Service) SendError(listener, err string) {
355+
if !s.isReady.Load() {
356+
log.Info("Stats is not ready to broadcast error")
357+
return
358+
}
351359
s.errCh <- errorMessage{Listener: listener, Err: err}
352360
}
353361

354362
func (s *Service) SendProcessedBlock(listener string, block uint64) {
363+
if !s.isReady.Load() {
364+
log.Info("Stats is not ready to broadcast processed block")
365+
return
366+
}
355367
s.processedBlockCh <- processedBlockMessage{Listener: listener, ProcessedBlock: block}
356368
}

0 commit comments

Comments
 (0)