Skip to content

Commit 197e0e3

Browse files
committed
add timeout for rpc call
1 parent fe99aa1 commit 197e0e3

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

rpc.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ func (m *Monitor) rpcBlockByNumber(blockNumber uint64) (*types.Block, error) {
7070
block := &types.Block{}
7171

7272
rpcBlockMeter.Mark(1)
73-
err := m.cl.Call(block, "ctxc_getBlockByNumber", "0x"+strconv.FormatUint(blockNumber, 16), true)
73+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
74+
defer cancel()
75+
err := m.cl.CallContext(ctx, block, "ctxc_getBlockByNumber", "0x"+strconv.FormatUint(blockNumber, 16), true)
7476
if err == nil {
7577
return block, nil
7678
}
@@ -135,7 +137,9 @@ func (m *Monitor) getRemainingSize(address string) (uint64, error) {
135137
}
136138
var remainingSize hexutil.Uint64
137139
rpcUploadMeter.Mark(1)
138-
if err := m.cl.Call(&remainingSize, "ctxc_getUpload", address, "latest"); err != nil {
140+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
141+
defer cancel()
142+
if err := m.cl.CallContext(ctx, &remainingSize, "ctxc_getUpload", address, "latest"); err != nil {
139143
return 0, err
140144
}
141145
remain := uint64(remainingSize)
@@ -147,7 +151,9 @@ func (m *Monitor) getRemainingSize(address string) (uint64, error) {
147151

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

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

163169
rpcCurrentMeter.Mark(1)
164-
if err := m.cl.Call(&currentNumber, "ctxc_blockNumber"); err != nil {
170+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
171+
defer cancel()
172+
if err := m.cl.CallContext(ctx, &currentNumber, "ctxc_blockNumber"); err != nil {
165173
log.Error("Call ipc method ctxc_blockNumber failed", "error", err)
166174
return m.currentNumber.Load(), false, err
167175
}

0 commit comments

Comments
 (0)