Skip to content

Commit

Permalink
fix: stats lock all routines when it cannot connect to server (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
DNK90 authored Nov 27, 2023
1 parent e52a55b commit 9df330a
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion stats/bridge_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strings"
"sync"
"sync/atomic"
"time"

bridgeCore "github.com/axieinfinity/bridge-core"
Expand All @@ -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"`
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
}

0 comments on commit 9df330a

Please sign in to comment.