Skip to content

Commit 3582554

Browse files
committed
Use context.WithTimeout in submitSingleTransaction method
1 parent 6ccf8db commit 3582554

1 file changed

Lines changed: 46 additions & 24 deletions

File tree

services/requester/batch_tx_pool.go

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -284,34 +284,56 @@ func (t *BatchTxPool) submitSingleTransaction(
284284
ctx context.Context,
285285
hexEncodedTx cadence.String,
286286
) error {
287-
coinbaseAddress, err := cadence.NewString(t.config.Coinbase.Hex())
288-
if err != nil {
289-
return err
290-
}
287+
done := make(chan struct{})
288+
var submitError error
291289

292-
script := replaceAddresses(runTxScript, t.config.FlowNetworkID)
293-
flowTx, err := t.buildTransaction(
294-
ctx,
295-
t.getReferenceBlock(),
296-
script,
297-
cadence.NewArray([]cadence.Value{hexEncodedTx}),
298-
coinbaseAddress,
299-
)
300-
if err != nil {
301-
// If there was any error during the transaction build
302-
// process, we record it as a dropped transaction.
303-
t.collector.TransactionsDropped(1)
304-
return err
305-
}
290+
// This method is called while holding the `t.txMux` lock,
291+
// don't let it run for a long time, to avoid lock-contention
292+
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
293+
defer cancel()
306294

307-
if err := t.client.SendTransaction(ctx, *flowTx); err != nil {
308-
// If there was any error while sending the transaction,
309-
// we record it as a dropped transaction.
310-
t.collector.TransactionsDropped(1)
311-
return err
295+
// build & submit transaction
296+
go func() {
297+
defer close(done)
298+
299+
coinbaseAddress, err := cadence.NewString(t.config.Coinbase.Hex())
300+
if err != nil {
301+
submitError = err
302+
return
303+
}
304+
305+
script := replaceAddresses(runTxScript, t.config.FlowNetworkID)
306+
flowTx, err := t.buildTransaction(
307+
ctx,
308+
t.getReferenceBlock(),
309+
script,
310+
cadence.NewArray([]cadence.Value{hexEncodedTx}),
311+
coinbaseAddress,
312+
)
313+
if err != nil {
314+
// If there was any error during the transaction build
315+
// process, we record it as a dropped transaction.
316+
t.collector.TransactionsDropped(1)
317+
submitError = err
318+
return
319+
}
320+
321+
if err := t.client.SendTransaction(ctx, *flowTx); err != nil {
322+
// If there was any error while sending the transaction,
323+
// we record it as a dropped transaction.
324+
t.collector.TransactionsDropped(1)
325+
submitError = err
326+
return
327+
}
328+
}()
329+
330+
select {
331+
case <-ctx.Done():
332+
return ctx.Err()
333+
case <-done:
312334
}
313335

314-
return nil
336+
return submitError
315337
}
316338

317339
func (t *BatchTxPool) updateEOAActivityMetadata(

0 commit comments

Comments
 (0)