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" "context"
"fmt" "fmt"
"math/big" "math/big"
<<<<<<< HEAD
"strings" "strings"
=======
>>>>>>> 6fcba6e30 (feat: add buffer for status event)
"sync" "sync"
"time" "time"
@ -34,9 +37,10 @@ type ClientWithFallback struct {
WalletNotifier func(chainId uint64, message string) WalletNotifier func(chainId uint64, message string)
IsConnected bool IsConnected bool
IsConnectedLock sync.RWMutex consecutiveFailureCount int
LastCheckedAt int64 IsConnectedLock sync.RWMutex
LastCheckedAt int64
} }
var vmErrors = []error{ var vmErrors = []error{
@ -70,13 +74,14 @@ func NewSimpleClient(main *rpc.Client, chainID uint64) *ClientWithFallback {
}) })
return &ClientWithFallback{ return &ClientWithFallback{
ChainID: chainID, ChainID: chainID,
main: ethclient.NewClient(main), main: ethclient.NewClient(main),
fallback: nil, fallback: nil,
mainRPC: main, mainRPC: main,
fallbackRPC: nil, fallbackRPC: nil,
IsConnected: true, IsConnected: true,
LastCheckedAt: time.Now().Unix(), consecutiveFailureCount: 0,
LastCheckedAt: time.Now().Unix(),
} }
} }
@ -126,16 +131,21 @@ func (c *ClientWithFallback) setIsConnected(value bool) {
c.IsConnectedLock.Lock() c.IsConnectedLock.Lock()
defer c.IsConnectedLock.Unlock() defer c.IsConnectedLock.Unlock()
c.LastCheckedAt = time.Now().Unix() c.LastCheckedAt = time.Now().Unix()
if value != c.IsConnected { if !value {
message := "down" c.consecutiveFailureCount += 1
if value { if c.consecutiveFailureCount > 3 && c.IsConnected {
message = "up" if c.WalletNotifier != nil {
c.WalletNotifier(c.ChainID, "down")
}
c.IsConnected = false
} }
if c.WalletNotifier != nil {
c.WalletNotifier(c.ChainID, message) } else {
c.consecutiveFailureCount = 0
if !c.IsConnected {
c.IsConnected = true
} }
} }
c.IsConnected = value
} }
func (c *ClientWithFallback) makeCallNoReturn(main func() error, fallback func() error) error { func (c *ClientWithFallback) makeCallNoReturn(main func() error, fallback func() error) error {