Skip to content

Commit

Permalink
Allow timeout changes for grpc client
Browse files Browse the repository at this point in the history
  • Loading branch information
fbsobreira committed Aug 24, 2020
1 parent 228ee00 commit ce5a488
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 58 deletions.
12 changes: 6 additions & 6 deletions pkg/client/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (g *GrpcClient) GetAccount(addr string) (*core.Account, error) {
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

acc, err := g.Client.GetAccount(ctx, account)
Expand All @@ -47,7 +47,7 @@ func (g *GrpcClient) GetAccountNet(addr string) (*api.AccountNetMessage, error)
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

return g.Client.GetAccountNet(ctx, account)
Expand All @@ -64,7 +64,7 @@ func (g *GrpcClient) CreateAccount(from, addr string) (*api.TransactionExtention
if contract.AccountAddress, err = common.DecodeCheck(addr); err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.CreateAccount2(ctx, contract)
Expand All @@ -89,7 +89,7 @@ func (g *GrpcClient) UpdateAccount(from, accountName string) (*api.TransactionEx
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.UpdateAccount2(ctx, contract)
Expand Down Expand Up @@ -210,7 +210,7 @@ func (g *GrpcClient) WithdrawBalance(from string) (*api.TransactionExtention, er
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.WithdrawBalance2(ctx, contract)
Expand Down Expand Up @@ -351,7 +351,7 @@ func (g *GrpcClient) UpdateAccountPermission(from string, owner, witness map[str
contract.Witness = witnessPermission
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.AccountPermissionUpdate(ctx, contract)
Expand Down
18 changes: 9 additions & 9 deletions pkg/client/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func (g *GrpcClient) GetAssetIssueByAccount(address string) (*api.AssetIssueList
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

return g.Client.GetAssetIssueByAccount(ctx, account)
}

// GetAssetIssueByName list asset issued by name
func (g *GrpcClient) GetAssetIssueByName(name string) (*core.AssetIssueContract, error) {
ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

return g.Client.GetAssetIssueByName(ctx, GetMessageBytes([]byte(name)))
Expand All @@ -41,15 +41,15 @@ func (g *GrpcClient) GetAssetIssueByName(name string) (*core.AssetIssueContract,
func (g *GrpcClient) GetAssetIssueByID(tokenID string) (*core.AssetIssueContract, error) {
bn := new(big.Int).SetBytes([]byte(tokenID))

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

return g.Client.GetAssetIssueById(ctx, GetMessageBytes(bn.Bytes()))
}

// GetAssetIssueList list all TRC10
func (g *GrpcClient) GetAssetIssueList(page int64, limit ...int) (*api.AssetIssueList, error) {
ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

if page == -1 {
Expand Down Expand Up @@ -136,7 +136,7 @@ func (g *GrpcClient) AssetIssue(from, name, description, abbr, urlStr string,
FrozenSupply, assetIssueContractFrozenSupply)
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.CreateAssetIssue2(ctx, contract)
Expand Down Expand Up @@ -167,7 +167,7 @@ func (g *GrpcClient) UpdateAssetIssue(from, description, urlStr string,
contract.NewLimit = newLimit
contract.NewPublicLimit = newPublicLimit

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.UpdateAsset2(ctx, contract)
Expand Down Expand Up @@ -198,7 +198,7 @@ func (g *GrpcClient) TransferAsset(from, toAddress,
contract.AssetName = []byte(assetName)
contract.Amount = amount

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.TransferAsset2(ctx, contract)
Expand Down Expand Up @@ -229,7 +229,7 @@ func (g *GrpcClient) ParticipateAssetIssue(from, issuerAddress,
contract.AssetName = []byte(tokenID)
contract.Amount = amount

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.ParticipateAssetIssue2(ctx, contract)
Expand All @@ -253,7 +253,7 @@ func (g *GrpcClient) UnfreezeAsset(from string) (*api.TransactionExtention, erro
if contract.OwnerAddress, err = common.DecodeCheck(from); err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.UnfreezeAsset2(ctx, contract)
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (g *GrpcClient) FreezeBalance(from, delegateTo string,
}
contract.Resource = resource

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.FreezeBalance2(ctx, contract)
Expand Down Expand Up @@ -64,7 +64,7 @@ func (g *GrpcClient) UnfreezeBalance(from, delegateTo string, resource core.Reso
}
contract.Resource = resource

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.UnfreezeBalance2(ctx, contract)
Expand Down
12 changes: 6 additions & 6 deletions pkg/client/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// GetNowBlock return TIP block
func (g *GrpcClient) GetNowBlock() (*api.BlockExtention, error) {
ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

result, err := g.Client.GetNowBlock2(ctx, new(api.EmptyMessage))
Expand All @@ -28,7 +28,7 @@ func (g *GrpcClient) GetBlockByNum(num int64) (*api.BlockExtention, error) {
numMessage := new(api.NumberMessage)
numMessage.Num = num

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

result, err := g.Client.GetBlockByNum2(ctx, numMessage)
Expand All @@ -45,7 +45,7 @@ func (g *GrpcClient) GetBlockInfoByNum(num int64) (*api.TransactionInfoList, err
numMessage := new(api.NumberMessage)
numMessage.Num = num

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

result, err := g.Client.GetTransactionInfoByBlockNum(ctx, numMessage)
Expand All @@ -67,7 +67,7 @@ func (g *GrpcClient) GetBlockByID(id string) (*core.Block, error) {
return nil, fmt.Errorf("get block by id: %v", err)
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

return g.Client.GetBlockById(ctx, blockID)
Expand All @@ -79,7 +79,7 @@ func (g *GrpcClient) GetBlockByLimitNext(start, end int64) (*api.BlockListExtent
blockLimit.StartNum = start
blockLimit.EndNum = end

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

return g.Client.GetBlockByLimitNext2(ctx, blockLimit)
Expand All @@ -90,7 +90,7 @@ func (g *GrpcClient) GetBlockByLatestNum(num int64) (*api.BlockListExtention, er
numMessage := new(api.NumberMessage)
numMessage.Num = num

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

return g.Client.GetBlockByLatestNum2(ctx, numMessage)
Expand Down
29 changes: 22 additions & 7 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,37 @@ import (
"google.golang.org/grpc"
)

const grpcTimeout = 5 * time.Second

// GrpcClient controller structure
type GrpcClient struct {
Address string
Conn *grpc.ClientConn
Client api.WalletClient
Address string
Conn *grpc.ClientConn
Client api.WalletClient
grpcTimeout time.Duration
}

// NewGrpcClient create grpc controller
func NewGrpcClient(address string) *GrpcClient {
client := new(GrpcClient)
client.Address = address
client := &GrpcClient{
Address: address,
grpcTimeout: 5 * time.Second,
}
return client
}

// NewGrpcClientWithTimeout create grpc controller
func NewGrpcClientWithTimeout(address string, timeout time.Duration) *GrpcClient {
client := &GrpcClient{
Address: address,
grpcTimeout: timeout,
}
return client
}

// SetTimeout for Client connections
func (g *GrpcClient) SetTimeout(timeout time.Duration) {
g.grpcTimeout = timeout
}

// Start initiate grpc connection
func (g *GrpcClient) Start() error {
var err error
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (g *GrpcClient) TriggerConstantContract(from, contractAddress, method, json

// triggerConstantContract and return tx result
func (g *GrpcClient) triggerConstantContract(ct *core.TriggerSmartContract) (*api.TransactionExtention, error) {
ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

return g.Client.TriggerConstantContract(ctx, ct)
Expand Down Expand Up @@ -100,7 +100,7 @@ func (g *GrpcClient) TriggerContract(from, contractAddress, method, jsonString s

// triggerContract and return tx result
func (g *GrpcClient) triggerContract(ct *core.TriggerSmartContract, feeLimit int64) (*api.TransactionExtention, error) {
ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.TriggerContract(ctx, ct)
Expand Down Expand Up @@ -156,7 +156,7 @@ func (g *GrpcClient) DeployContract(from, contractName string,
},
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.DeployContract(ctx, ct)
Expand Down Expand Up @@ -193,7 +193,7 @@ func (g *GrpcClient) GetContractABI(contractAddress string) (*core.SmartContract
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

sm, err := g.Client.GetContract(ctx, GetMessageBytes(contractDesc))
Expand Down
12 changes: 6 additions & 6 deletions pkg/client/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// ExchangeList of bancor TRC10, use page -1 to list all
func (g *GrpcClient) ExchangeList(page int64, limit ...int) (*api.ExchangeList, error) {
ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

if page == -1 {
Expand All @@ -29,7 +29,7 @@ func (g *GrpcClient) ExchangeList(page int64, limit ...int) (*api.ExchangeList,

// ExchangeByID returns exchangeDetails
func (g *GrpcClient) ExchangeByID(id int64) (*core.Exchange, error) {
ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()
bID := make([]byte, 8)
binary.BigEndian.PutUint64(bID, uint64(id))
Expand Down Expand Up @@ -64,7 +64,7 @@ func (g *GrpcClient) ExchangeCreate(
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.ExchangeCreate(ctx, contract)
Expand Down Expand Up @@ -98,7 +98,7 @@ func (g *GrpcClient) ExchangeInject(
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.ExchangeInject(ctx, contract)
Expand Down Expand Up @@ -132,7 +132,7 @@ func (g *GrpcClient) ExchangeWithdraw(
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.ExchangeWithdraw(ctx, contract)
Expand Down Expand Up @@ -168,7 +168,7 @@ func (g *GrpcClient) ExchangeTrade(
return nil, err
}

ctx, cancel := context.WithTimeout(context.Background(), grpcTimeout)
ctx, cancel := context.WithTimeout(context.Background(), g.grpcTimeout)
defer cancel()

tx, err := g.Client.ExchangeTransaction(ctx, contract)
Expand Down
Loading

0 comments on commit ce5a488

Please sign in to comment.