feat: add buffer for status event

This commit is contained in:
Anthony Laibe 2023-03-23 15:08:55 +01:00 committed by Anthony Laibe
parent 62220cedee
commit f7690a5d89
1 changed files with 27 additions and 17 deletions

View File

@ -4,7 +4,10 @@ import (
"context"
"fmt"
"math/big"
<<<<<<< HEAD
"strings"
=======
>>>>>>> 6fcba6e30 (feat: add buffer for status event)
"sync"
"time"
@ -35,6 +38,7 @@ type ClientWithFallback struct {
WalletNotifier func(chainId uint64, message string)
IsConnected bool
consecutiveFailureCount int
IsConnectedLock sync.RWMutex
LastCheckedAt int64
}
@ -76,6 +80,7 @@ func NewSimpleClient(main *rpc.Client, chainID uint64) *ClientWithFallback {
mainRPC: main,
fallbackRPC: nil,
IsConnected: true,
consecutiveFailureCount: 0,
LastCheckedAt: time.Now().Unix(),
}
}
@ -126,16 +131,21 @@ func (c *ClientWithFallback) setIsConnected(value bool) {
c.IsConnectedLock.Lock()
defer c.IsConnectedLock.Unlock()
c.LastCheckedAt = time.Now().Unix()
if value != c.IsConnected {
message := "down"
if value {
message = "up"
}
if !value {
c.consecutiveFailureCount += 1
if c.consecutiveFailureCount > 3 && c.IsConnected {
if c.WalletNotifier != nil {
c.WalletNotifier(c.ChainID, message)
c.WalletNotifier(c.ChainID, "down")
}
c.IsConnected = false
}
} else {
c.consecutiveFailureCount = 0
if !c.IsConnected {
c.IsConnected = true
}
}
c.IsConnected = value
}
func (c *ClientWithFallback) makeCallNoReturn(main func() error, fallback func() error) error {