Concurrent rpc balances queries

pull/1211/head
flicker-harmony 5 years ago
parent c63dc3251b
commit d12c5434f7
  1. 59
      cmd/client/wallet/main.go

@ -797,32 +797,43 @@ func FetchBalance(address common.Address) []*AccountState {
result[uint32(shardID)] = &AccountState{balance, 0}
LOOP:
for j := 0; j < len(walletProfile.RPCServer[shardID]); j++ {
for retry := 0; retry < rpcRetry; retry++ {
server := walletProfile.RPCServer[shardID][j]
client, err := clientService.NewClient(server.IP, server.Port)
if err != nil {
continue
var wgShard sync.WaitGroup
wgShard.Add(len(walletProfile.RPCServer[shardID]))
var mutexAccountState = &sync.Mutex{}
for rpcServerID := 0; rpcServerID < len(walletProfile.RPCServer[shardID]); rpcServerID++ {
go func(rpcServerID int) {
defer wgShard.Done()
log.Debug("Launched goroutiune");
for retry := 0; retry < rpcRetry; retry++ {
server := walletProfile.RPCServer[shardID][rpcServerID]
client, err := clientService.NewClient(server.IP, server.Port)
if err != nil {
continue
}
log.Debug("FetchBalance", "server", server)
response, err := client.GetBalance(address)
if err != nil {
log.Info("failed to get balance, retrying ...")
time.Sleep(200 * time.Millisecond)
continue
}
log.Debug("FetchBalance", "response", response)
respBalance := big.NewInt(0)
respBalance.SetBytes(response.Balance)
mutexAccountState.Lock()
if balance.Cmp(respBalance) < 0 {
balance.SetBytes(response.Balance)
nonce = response.Nonce
}
mutexAccountState.Unlock()
}
log.Debug("FetchBalance", "server", server)
response, err := client.GetBalance(address)
if err != nil {
log.Info("failed to get balance, retrying ...")
time.Sleep(200 * time.Millisecond)
continue
}
log.Debug("FetchBalance", "response", response)
respBalance := big.NewInt(0)
respBalance.SetBytes(response.Balance)
if balance.Cmp(respBalance) < 0 {
balance.SetBytes(response.Balance)
nonce = response.Nonce
}
break LOOP
}
}(rpcServerID)
}
wgShard.Wait()
result[shardID] = &AccountState{balance, nonce}
}(shardID)
}

Loading…
Cancel
Save