Skip to content

Commit 81a2dac

Browse files
authored
fix: don't let the stats block the main logic (#94)
This commit makes the functions that main logic calls to send information to stats non-blocking. When the stats is not ready, the information is dropped.
1 parent 9df330a commit 81a2dac

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

stats/bridge_stats.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net/http"
77
"strings"
88
"sync"
9-
"sync/atomic"
109
"time"
1110

1211
bridgeCore "github.com/axieinfinity/bridge-core"
@@ -127,7 +126,6 @@ type Service struct {
127126
processedBlockCh chan processedBlockMessage
128127
quitCh chan struct{}
129128
store stores.TaskStore
130-
isReady atomic.Bool
131129
}
132130

133131
func NewService(node, chainId, operator, host, secret string, db *gorm.DB) {
@@ -299,8 +297,6 @@ func (s *Service) report(conn *connWrapper) error {
299297
func (s *Service) readLoop(conn *connWrapper) {
300298
// If the read loop exits, close the connection
301299
defer conn.Close()
302-
// set isReady to true
303-
s.isReady.Store(true)
304300
log.Info("[Bridge stats] Start read loop")
305301
for {
306302
// Exit the function when receiving the quit signal
@@ -352,17 +348,17 @@ func (s *Service) setProcessedBlock(listener string, block uint64) error {
352348
}
353349

354350
func (s *Service) SendError(listener, err string) {
355-
if !s.isReady.Load() {
356-
log.Info("Stats is not ready to broadcast error")
357-
return
351+
select {
352+
case s.errCh <- errorMessage{Listener: listener, Err: err}:
353+
default:
354+
log.Debug("Stats is not ready to broadcast error")
358355
}
359-
s.errCh <- errorMessage{Listener: listener, Err: err}
360356
}
361357

362358
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
359+
select {
360+
case s.processedBlockCh <- processedBlockMessage{Listener: listener, ProcessedBlock: block}:
361+
default:
362+
log.Debug("Stats is not ready to broadcast processed block")
366363
}
367-
s.processedBlockCh <- processedBlockMessage{Listener: listener, ProcessedBlock: block}
368364
}

0 commit comments

Comments
 (0)