Skip to content

Commit 095c3dd

Browse files
committed
feat(client): use asyncio semaphore for streaming batch check
1 parent 5d67f5c commit 095c3dd

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

openfga_sdk/client/client.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -511,17 +511,20 @@ async def check(self, body: ClientCheckRequest, options: dict[str, str] = None):
511511
)
512512
return api_response
513513

514-
async def _single_batch_check(self, body: ClientCheckRequest, options: dict[str, str] = None): # noqa: E501
514+
async def _single_batch_check(self, body: ClientCheckRequest, semaphore: asyncio.Semaphore, options: dict[str, str] = None): # noqa: E501
515515
"""
516516
Run a single batch request and return body in a SingleBatchCheckResponse
517517
:param body - ClientCheckRequest defining check request
518518
:param authorization_model_id(options) - Overrides the authorization model id in the configuration
519519
"""
520+
await semaphore.acquire()
520521
try:
521522
api_response = await self.check(body, options)
522523
return BatchCheckResponse(allowed=api_response.allowed, request=body, response=api_response, error=None)
523524
except Exception as err:
524525
return BatchCheckResponse(allowed=False, request=body, response=None, error=err)
526+
finally:
527+
semaphore.release()
525528

526529
async def batch_check(self, body: List[ClientCheckRequest], options: dict[str, str] = None): # noqa: E501
527530
"""
@@ -543,13 +546,11 @@ async def batch_check(self, body: List[ClientCheckRequest], options: dict[str, s
543546
max_parallel_requests = 10
544547
if options is not None and "max_parallel_requests" in options:
545548
max_parallel_requests = options["max_parallel_requests"]
546-
# Break the batch into chunks
547-
request_batches = _chuck_array(body, max_parallel_requests)
548-
batch_check_response = []
549-
for request_batch in request_batches:
550-
request = [self._single_batch_check(i, options) for i in request_batch]
551-
response = await asyncio.gather(*request)
552-
batch_check_response.extend(response)
549+
550+
sem = asyncio.Semaphore(max_parallel_requests)
551+
batch_check_coros = [self._single_batch_check(request, sem, options) for request in body]
552+
batch_check_response = await asyncio.gather(*batch_check_coros)
553+
553554
return batch_check_response
554555

555556
async def expand(self, body: ClientExpandRequest, options: dict[str, str] = None): # noqa: E501

0 commit comments

Comments
 (0)