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

View File

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

View File

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

View File

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