Changing batch size

edit

The default batch size is 100, meaning 100 requests will queue up before the client forces futures to begin resolving (e.g. initiate a curl_multi call). The batch size can be changed depending on your preferences. The batch size is controllable via the max_handles setting when configuring the handler:

$handlerParams = [
    'max_handles' => 500
];

$defaultHandler = ClientBuilder::defaultHandler($handlerParams);

$client = ClientBuilder::create()
            ->setHandler($defaultHandler)
            ->build();

This will change the behavior to wait on 500 queued requests before sending the batch. Note, however, that forcing a future to resolve will cause the underlying curl batch to execute, regardless of if the batch is "full" or not. In this example, only 499 requests are added to the queue…​but the final future resolution will force the batch to flush anyway:

$handlerParams = [
    'max_handles' => 500
];

$defaultHandler = ClientBuilder::defaultHandler($handlerParams);

$client = ClientBuilder::create()
            ->setHandler($defaultHandler)
            ->build();

$futures = [];

for ($i = 0; $i < 499; $i++) {
    $params = [
        'index' => 'test',
        'type' => 'test',
        'id' => $i,
        'client' => [
            'future' => 'lazy'
        ]
    ];

    $futures[] = $client->get($params);     //queue up the request
}

// resolve the future, and therefore the underlying batch
$body = $future[499]['body'];