Skip to content

Commit

Permalink
Add extra method to Client and improve performance of multiple reques…
Browse files Browse the repository at this point in the history
…ts when we have only one
  • Loading branch information
donhardman committed Dec 25, 2024
1 parent 6a45de4 commit beafa89
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/ManticoreSearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,23 +157,20 @@ public function sendRequest(

/**
* Send multiple requests with async and get all responses in single run
* @param array<string,array{request:string,path?:string,disableAgentHeader?:bool}> $requests
* @param array<array{url:string,request:string,path?:string,disableAgentHeader?:bool}> $requests
* @return array<Response>
*/
public function sendMultiRequest(array $requests): array {
if (sizeof($requests) === 0) {
$request = array_pop($requests);
return $this->sendRequestToUrl(...$request);
}
$requestCount = sizeof($requests);
$channel = new Channel($requestCount);
foreach ($requests as $url => $request) {
foreach ($requests as $request) {
Coroutine::create(
function () use ($channel, $request, $url) {
if ($url) {
$origServerUrl = $this->getServerUrl();
$this->setServerUrl($url);
}
$response = $this->sendRequest(...$request);
if ($url) {
$this->setServerUrl($origServerUrl);
}
function () use ($channel, $request) {
$response = $this->sendRequestToUrl(...$request);
$channel->push($response);
}
);
Expand All @@ -188,6 +185,31 @@ function () use ($channel, $request, $url) {
return $responses;
}

/**
* Helper function that let us to send request to the specified url and setit back to original
* @param string $url
* @param string $request
* @param ?string $path
* @param bool $disableAgentHeader
* @return Response
*/
public function sendRequestToUrl(
string $url,
string $request,
?string $path = null,
bool $disableAgentHeader = false
): Response {
if ($url) {
$origServerUrl = $this->getServerUrl();
$this->setServerUrl($url);
}
$response = $this->sendRequest($request, $path, $disableAgentHeader);
if ($url) {
$this->setServerUrl($origServerUrl);
}
return $response;
}

/**
* Force to use sync client instead of async detection
* @param bool $value
Expand Down

0 comments on commit beafa89

Please sign in to comment.