Skip to content

Commit 48206df

Browse files
authored
[Python SDK] change library for sync client: httpX -> requests (aptos-labs#9038)
### Description Observed two issues in httpX using the sync client, both related to latency: (1) It does not properly reuse connections on POST, incurring the full TLS handshake time ~100ms. (2) It seems to delay ~100ms sending of the POST body. Combined, this was showing ~200ms additional time when measuring latency. These problems don't show up with requests. Updated with `poetry lock` as well. ### Test Plan Run `poetry install` `poetry install --sync` `make test` and `make examples`. Also confirmed manually that latency is reduced when running an e2e measurement script.
1 parent 38cf14f commit 48206df

File tree

4 files changed

+195
-51
lines changed

4 files changed

+195
-51
lines changed

ecosystem/python/sdk/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to the Aptos Python SDK will be captured in this file. This
44

55
**Note:** Until we cut a 1.0.0 release, the Aptos Python SDK does not follow semantic versioning.
66

7+
## 0.6.3
8+
- Change sync client library from httpX to requests due to latency concerns.
9+
710
## 0.6.2
811
- Added custom header "x-aptos-client" to both sync/async RestClient
912

ecosystem/python/sdk/aptos_sdk/client.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import time
55
from typing import Any, Dict, List
66

7-
import httpx
7+
import requests
88

99
from . import ed25519
1010
from .account import Account
@@ -38,14 +38,16 @@ class RestClient:
3838
"""A wrapper around the Aptos-core Rest API"""
3939

4040
chain_id: int
41-
client: httpx.Client
41+
client: requests.Session
4242
client_config: ClientConfig
4343
base_url: str
4444

4545
def __init__(self, base_url: str, client_config: ClientConfig = ClientConfig()):
4646
self.base_url = base_url
47-
self.client = httpx.Client()
48-
self.client.headers[Metadata.APTOS_HEADER] = Metadata.get_aptos_header_val()
47+
self.client = requests.Session()
48+
self.client.headers.update(
49+
{Metadata.APTOS_HEADER: Metadata.get_aptos_header_val()}
50+
)
4951
self.client_config = client_config
5052
self.chain_id = int(self.info()["chain_id"])
5153

@@ -205,8 +207,8 @@ def simulate_transaction(
205207
headers = {"Content-Type": "application/x.aptos.signed_transaction+bcs"}
206208
response = self.client.post(
207209
f"{self.base_url}/transactions/simulate",
210+
data=signed_transaction.bytes(),
208211
headers=headers,
209-
content=signed_transaction.bytes(),
210212
)
211213
if response.status_code >= 400:
212214
raise ApiError(response.text, response.status_code)
@@ -217,8 +219,8 @@ def submit_bcs_transaction(self, signed_transaction: SignedTransaction) -> str:
217219
headers = {"Content-Type": "application/x.aptos.signed_transaction+bcs"}
218220
response = self.client.post(
219221
f"{self.base_url}/transactions",
222+
data=signed_transaction.bytes(),
220223
headers=headers,
221-
content=signed_transaction.bytes(),
222224
)
223225
if response.status_code >= 400:
224226
raise ApiError(response.text, response.status_code)

0 commit comments

Comments
 (0)