Skip to content

Commit

Permalink
Use concurrent requests for improved efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Sep 8, 2022
1 parent e46e612 commit ee2d8f1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
20 changes: 18 additions & 2 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Turbo124\Beacon;

use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise;

class Generator
{
Expand Down Expand Up @@ -94,13 +95,28 @@ public function batchFire($metric_array)
if(!is_array($metric_array) || count($metric_array) == 0)
return;

$data['metrics'] = $metric_array;
// $data['metrics'] = $metric_array;

$client = $this->httpClient();

try {

$response = $client->request('POST',$this->endPoint($metric_array[0]->type), ['form_params' => $data]);
//$response = $client->request('POST',$this->endPoint($metric_array[0]->type), ['form_params' => $data]);

$batch_of = 40;
$batch = array_chunk($metric_array, $batch_of);

/* Concurrency ++ */
foreach($batch as $key => $value) {

$data['metrics'] = $value;

$promises = [
$key => $client->requestAsync('POST',$this->endPoint($metric_array[0]->type), ['form_params' => $data])
];
}

$responses = Promise\Utils::unwrap($promises);

} catch (RequestException $e) {

Expand Down
10 changes: 1 addition & 9 deletions src/Jobs/BatchMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,7 @@ public function handle()
continue;

$generator = new Generator();

$batch_of = 40;
$batch = array_chunk($metrics, $batch_of);

foreach($batch as $b) {

$generator->batchFire($b);

}
$generator->batchFire($metrics);

Cache::put(config('beacon.cache_key') . '_' . $type, []);
}
Expand Down

0 comments on commit ee2d8f1

Please sign in to comment.