Skip to content

Commit

Permalink
feat: retry transaction submissions (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdummett authored Jan 17, 2024
1 parent 2aed8c9 commit 4440abb
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions vega_sim/wallet/vega_wallet.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, Dict, List, Optional, Union

import os
import time
import inflection

import json
Expand Down Expand Up @@ -266,6 +267,7 @@ def submit_transaction(
transaction: Any,
transaction_type: str,
wallet_name: Optional[int] = None,
max_retries: int = 20,
):
wallet_name = (
wallet_name if wallet_name is not None else self.vega_default_wallet_name
Expand Down Expand Up @@ -296,18 +298,28 @@ def submit_transaction(
}
url = f"{self.wallet_url}/api/v2/requests"

# Attempt to submit transaction, if client error raise error immediately, if
# server error retry submission un till max retries exceeded then raise error.
response = None
if self._mutex is None:
response = requests.post(url, headers=headers, json=submission)
else:
with self._mutex:
response = requests.post(url, headers=headers, json=submission)

try:
response.raise_for_status()
except Exception as e:
logging.warning(f"Submission failed, response={response.json()}")
raise e
exception = None
for i in range(max_retries):
try:
if self._mutex is None:
response = requests.post(url, headers=headers, json=submission)
else:
with self._mutex:
response = requests.post(url, headers=headers, json=submission)
response.raise_for_status()
return
except requests.exceptions.HTTPError as e:
exception = e
if 400 <= exception.response.status_code < 500:
break
if 500 <= exception.response.status_code < 600:
time.sleep(1.01 * i)
continue
logging.warning(f"Submission failed, response={response.json()}")
raise exception

def public_key(self, name: str, wallet_name: Optional[str] = None) -> str:
"""Return a public key for the given wallet name and key name.
Expand Down

0 comments on commit 4440abb

Please sign in to comment.