Skip to content

Commit

Permalink
add timeout for rpc call
Browse files Browse the repository at this point in the history
  • Loading branch information
ucwong committed Jul 21, 2024
1 parent fe99aa1 commit 197e0e3
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ func (m *Monitor) rpcBlockByNumber(blockNumber uint64) (*types.Block, error) {
block := &types.Block{}

rpcBlockMeter.Mark(1)
err := m.cl.Call(block, "ctxc_getBlockByNumber", "0x"+strconv.FormatUint(blockNumber, 16), true)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := m.cl.CallContext(ctx, block, "ctxc_getBlockByNumber", "0x"+strconv.FormatUint(blockNumber, 16), true)
if err == nil {
return block, nil
}
Expand Down Expand Up @@ -135,7 +137,9 @@ func (m *Monitor) getRemainingSize(address string) (uint64, error) {
}
var remainingSize hexutil.Uint64
rpcUploadMeter.Mark(1)
if err := m.cl.Call(&remainingSize, "ctxc_getUpload", address, "latest"); err != nil {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := m.cl.CallContext(ctx, &remainingSize, "ctxc_getUpload", address, "latest"); err != nil {
return 0, err
}
remain := uint64(remainingSize)
Expand All @@ -147,7 +151,9 @@ func (m *Monitor) getRemainingSize(address string) (uint64, error) {

func (m *Monitor) getReceipt(tx string) (receipt types.Receipt, err error) {
rpcReceiptMeter.Mark(1)
if err = m.cl.Call(&receipt, "ctxc_getTransactionReceipt", tx); err != nil {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err = m.cl.CallContext(ctx, &receipt, "ctxc_getTransactionReceipt", tx); err != nil {
log.Warn("R is nil", "R", tx, "err", err)
}

Expand All @@ -161,7 +167,9 @@ func (m *Monitor) currentBlock() (uint64, bool, error) {
)

rpcCurrentMeter.Mark(1)
if err := m.cl.Call(&currentNumber, "ctxc_blockNumber"); err != nil {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := m.cl.CallContext(ctx, &currentNumber, "ctxc_blockNumber"); err != nil {
log.Error("Call ipc method ctxc_blockNumber failed", "error", err)
return m.currentNumber.Load(), false, err
}
Expand Down

0 comments on commit 197e0e3

Please sign in to comment.