Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'TON_API_TONLIB_PARALLEL_REQUESTS_PER_LITESERVER': '50',
'TON_API_TONLIB_CDLL_PATH': '',
'TON_API_TONLIB_REQUEST_TIMEOUT': '10',
'TON_API_TONLIB_WAIT_FOR_SEND_SUCCESS': '0',
'TON_API_GUNICORN_FLAGS': ''
}

Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ services:
- TON_API_TONLIB_PARALLEL_REQUESTS_PER_LITESERVER
- TON_API_TONLIB_CDLL_PATH
- TON_API_TONLIB_REQUEST_TIMEOUT
- TON_API_TONLIB_WAIT_FOR_SEND_SUCCESS
- TON_API_GET_METHODS_ENABLED
- TON_API_JSON_RPC_ENABLED
- TON_API_ROOT_PATH
Expand Down
4 changes: 4 additions & 0 deletions ton-http-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ The service supports the following environment variables:

Timeout for liteserver requests.

- `TON_API_TONLIB_WAIT_FOR_SEND_SUCCESS` *(default: 0)*

Send behavior toggle (sendBoc, sendBocReturnHash): when enabled (1), returns the first successful response, ignoring early errors until success or timeout. When disabled (0), it returns the first completed response (success or error).

#### Cache configuration
- `TON_API_CACHE_ENABLED` *(default: 0)*

Expand Down
2 changes: 2 additions & 0 deletions ton-http-api/pyTON/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def setup_environment(args):
os.environ['TON_API_TONLIB_PARALLEL_REQUESTS_PER_LITESERVER'] = str(args.parallel_requests_per_liteserver)
if args.cdll_path is not None:
os.environ['TON_API_TONLIB_CDLL_PATH'] = args.cdll_path
os.environ['TON_API_TONLIB_WAIT_FOR_SEND_SUCCESS'] = ('1' if args.wait_for_send_success else '0')
return


Expand All @@ -38,6 +39,7 @@ def main():
tonlib_args.add_argument('--tonlib-keystore', type=str, default='./ton_keystore/', help='Keystore path for tonlibjson')
tonlib_args.add_argument('--parallel-requests-per-liteserver', type=int, default=50, help='Maximum parallel requests per liteserver')
tonlib_args.add_argument('--cdll-path', type=str, default=None, help='Path to tonlibjson binary')
tonlib_args.add_argument('--wait-for-send-success', default=False, action='store_true', help='Wait for first successful send across liteservers')

cache_args = parser.add_argument_group('cache')
cache_args.add_argument('--cache', default=False, action='store_true', help='Enable cache')
Expand Down
19 changes: 17 additions & 2 deletions ton-http-api/pyTON/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,23 @@ async def _send_message(self, serialized_boc, method):
self.futures[task_id] = self.loop.create_future()
task_ids.append(task_id)

done, _ = await asyncio.wait([self.futures[task_id] for task_id in task_ids], return_when=asyncio.FIRST_COMPLETED)
result = list(done)[0].result()
if self.tonlib_settings.wait_for_send_success:
active_futures = [self.futures[task_id] for task_id in task_ids]
first_error = None
while active_futures:
completed_futures, pending_futures = await asyncio.wait(active_futures, return_when=asyncio.FIRST_COMPLETED)
for future in completed_futures:
try:
return future.result()
except Exception as err:
if first_error is None:
first_error = err
active_futures = list(pending_futures)
if first_error is not None:
raise first_error
else:
done, _ = await asyncio.wait([self.futures[task_id] for task_id in task_ids], return_when=asyncio.FIRST_COMPLETED)
result = list(done)[0].result()
finally:
for task_id in task_ids:
self.futures.pop(task_id)
Expand Down
4 changes: 3 additions & 1 deletion ton-http-api/pyTON/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TonlibSettings:
cdll_path: Optional[str]
request_timeout: int
verbosity_level: int
wait_for_send_success: bool

@property
def liteserver_config(self):
Expand All @@ -44,7 +45,8 @@ def from_environment(cls):
liteserver_config_path=os.environ.get('TON_API_TONLIB_LITESERVER_CONFIG', 'https://ton.org/global-config.json'),
cdll_path=os.environ.get('TON_API_TONLIB_CDLL_PATH', None),
request_timeout=int(os.environ.get('TON_API_TONLIB_REQUEST_TIMEOUT', '10')),
verbosity_level=verbosity_level)
verbosity_level=verbosity_level,
wait_for_send_success=strtobool(os.environ.get('TON_API_TONLIB_WAIT_FOR_SEND_SUCCESS', '0')))


@dataclass
Expand Down