feat: add time to check

This commit is contained in:
Anthony Laibe 2023-02-23 14:55:57 +01:00 committed by Anthony Laibe
parent c6f6ca99ae
commit f81758ec6e
4 changed files with 35 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"math/big"
"sync"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
@ -21,6 +22,7 @@ type Client struct {
ChainID uint64
rpcClient *rpc.Client
IsConnected bool
LastCheckedAt int64
IsConnectedLock sync.RWMutex
}
@ -37,7 +39,7 @@ func NewClient(rpc *rpc.Client, chainID uint64) (*Client, error) {
if err != nil {
return nil, err
}
client := &Client{eth: ethClient, ChainID: chainID, rpcClient: rpc, IsConnected: true}
client := &Client{eth: ethClient, ChainID: chainID, rpcClient: rpc, IsConnected: true, LastCheckedAt: time.Now().Unix()}
ChainClientInstances[chainID] = client
return client, nil
}
@ -60,6 +62,7 @@ func NewClients(rpc *rpc.Client, chainIDs []uint64) (res []*Client, err error) {
func (cc *Client) toggleIsConnected(err error) {
cc.IsConnectedLock.Lock()
defer cc.IsConnectedLock.Unlock()
cc.LastCheckedAt = time.Now().Unix()
if err != nil {
cc.IsConnected = false
} else {

View File

@ -116,6 +116,7 @@ type OpenseaClient struct {
url string
apiKey string
IsConnected bool
LastCheckedAt int64
IsConnectedLock sync.RWMutex
}
@ -131,7 +132,7 @@ func newOpenseaClient(chainID uint64, apiKey string) (*OpenseaClient, error) {
Timeout: time.Second * 5,
}
if url, ok := BaseURLs[chainID]; ok {
openseaClient := &OpenseaClient{client: client, url: url, apiKey: apiKey, IsConnected: true}
openseaClient := &OpenseaClient{client: client, url: url, apiKey: apiKey, IsConnected: true, LastCheckedAt: time.Now().Unix()}
OpenseaClientInstances[chainID] = openseaClient
return openseaClient, nil
}
@ -144,6 +145,7 @@ func (o *OpenseaClient) fetchAllCollectionsByOwner(owner common.Address) ([]Open
var collections []OpenseaCollection
o.IsConnectedLock.Lock()
defer o.IsConnectedLock.Unlock()
o.LastCheckedAt = time.Now().Unix()
for {
url := fmt.Sprintf("%s/collections?asset_owner=%s&offset=%d&limit=%d", o.url, owner, offset, CollectionLimit)
body, err := o.doOpenseaRequest(url)
@ -174,6 +176,7 @@ func (o *OpenseaClient) fetchAllAssetsByOwnerAndCollection(owner common.Address,
var assets []OpenseaAsset
o.IsConnectedLock.Lock()
defer o.IsConnectedLock.Unlock()
o.LastCheckedAt = time.Now().Unix()
for {
url := fmt.Sprintf("%s/assets?owner=%s&collection=%s&offset=%d&limit=%d", o.url, owner, collectionSlug, offset, AssetLimit)
body, err := o.doOpenseaRequest(url)

View File

@ -26,10 +26,15 @@ import (
"github.com/status-im/status-go/transactions"
)
type Connection struct {
Up bool `json:"up"`
LastCheckedAt int64 `json:"lastCheckedAt"`
}
type ConnectedResult struct {
Infura map[uint64]bool `json:"infura"`
CryptoCompare bool `json:"cryptoCompare"`
Opensea map[uint64]bool `json:"opensea"`
Infura map[uint64]Connection `json:"infura"`
CryptoCompare Connection `json:"cryptoCompare"`
Opensea map[uint64]Connection `json:"opensea"`
}
// NewService initializes service instance.
@ -162,18 +167,27 @@ func (s *Service) IsStarted() bool {
}
func (s *Service) CheckConnected(ctx context.Context) *ConnectedResult {
infura := make(map[uint64]bool)
infura := make(map[uint64]Connection)
for chainID, client := range chain.ChainClientInstances {
infura[chainID] = client.IsConnected
infura[chainID] = Connection{
Up: client.IsConnected,
LastCheckedAt: client.LastCheckedAt,
}
}
opensea := make(map[uint64]bool)
opensea := make(map[uint64]Connection)
for chainID, client := range OpenseaClientInstances {
opensea[chainID] = client.IsConnected
opensea[chainID] = Connection{
Up: client.IsConnected,
LastCheckedAt: client.LastCheckedAt,
}
}
return &ConnectedResult{
Infura: infura,
Opensea: opensea,
CryptoCompare: s.cryptoCompare.IsConnected,
Infura: infura,
Opensea: opensea,
CryptoCompare: Connection{
Up: s.cryptoCompare.IsConnected,
LastCheckedAt: s.cryptoCompare.LastCheckedAt,
},
}
}

View File

@ -68,11 +68,12 @@ type MarketValuesContainer struct {
type CryptoCompare struct {
client *http.Client
IsConnected bool
LastCheckedAt int64
IsConnectedLock sync.RWMutex
}
func NewCryptoCompare() *CryptoCompare {
return &CryptoCompare{client: &http.Client{Timeout: time.Minute}, IsConnected: true}
return &CryptoCompare{client: &http.Client{Timeout: time.Minute}, IsConnected: true, LastCheckedAt: time.Now().Unix()}
}
func renameSymbols(symbols []string) (renames []string) {
@ -111,6 +112,7 @@ func (c *CryptoCompare) DoQuery(url string) (*http.Response, error) {
c.IsConnectedLock.Lock()
defer c.IsConnectedLock.Unlock()
c.LastCheckedAt = time.Now().Unix()
if err != nil {
c.IsConnected = false
return nil, err