Skip to content

Commit 68de47d

Browse files
committed
fix: allow keeping connections open
1 parent ad1f44f commit 68de47d

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

libsimba/schemas.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class ConnectionConfig(BaseModel):
6262
# a path to a cert file or false to turn off verification
6363
verify: Optional[Union[bool, str]] = None
6464

65+
close_connection_after_use: bool = True
66+
6567
httpx_class: Optional[Type[httpx.Client]] = httpx.Client
6668
httpx_extra_kwargs: Optional[Dict[str, Any]] = {}
6769
async_httpx_class: Optional[Type[httpx.AsyncClient]] = httpx.AsyncClient

libsimba/utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ def __init__(
124124
respect_retry_after_header: bool = True,
125125
retryable_methods: Iterable[str] = None,
126126
retry_status_codes: Iterable[int] = None,
127+
close_connection_after_use: bool = True,
127128
) -> None:
128129
self.wrapped_transport = wrapped_transport
130+
self.close_connection_after_use = close_connection_after_use
129131
if jitter_ratio < 0 or jitter_ratio > 0.5:
130132
raise ValueError(
131133
f"jitter ratio should be between 0 and 0.5, actual {jitter_ratio}"
@@ -197,7 +199,9 @@ def handle_request(self, request: httpx.Request) -> httpx.Response:
197199
while (
198200
remaining_attempts > 0 and response.status_code in self.retry_status_codes
199201
):
200-
response.close()
202+
if self.close_connection_after_use:
203+
response.close()
204+
201205
sleep_time = self._calculate_sleep(attempts_made, response.headers)
202206
sleep(sleep_time)
203207
response = self.wrapped_transport.handle_request(request)
@@ -257,6 +261,7 @@ def async_http_client(
257261
http2=config.http2
258262
),
259263
max_attempts=config.max_attempts,
264+
close_connection_after_use=config.close_connection_after_use,
260265
)
261266
return config.async_httpx_class(timeout=config.timeout,
262267
transport=transport,
@@ -282,9 +287,11 @@ def http_client(
282287
HTTPTransport(
283288
retries=config.connection_retries,
284289
verify=config.verify,
285-
http2=config.http2
290+
http2=config.http2,
291+
close_connection_after_use=config.close_connection_after_use,
286292
),
287293
max_attempts=config.max_attempts,
294+
close_connection_after_use=config.close_connection_after_use,
288295
)
289296
return config.httpx_class(timeout=config.timeout,
290297
transport=transport,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "libsimba"
3-
version = "1.7.4"
3+
version = "1.7.5"
44
description = "libsimba is a library simplifying the use of SIMBAChain Blocks APIs."
55
authors = [
66
{ name = "SIMBA Chain Inc." }

0 commit comments

Comments
 (0)