|
5 | 5 |
|
6 | 6 | namespace Torchlight; |
7 | 7 |
|
8 | | -use Illuminate\Http\Client\Pool; |
| 8 | +use GuzzleHttp\Promise\Promise; |
| 9 | +use Illuminate\Http\Client\PendingRequest; |
9 | 10 | use Illuminate\Support\Arr; |
10 | 11 | use Illuminate\Support\Collection; |
11 | 12 | use Illuminate\Support\Facades\Http; |
@@ -43,6 +44,7 @@ public function highlight($blocks) |
43 | 44 | protected function request(Collection $blocks) |
44 | 45 | { |
45 | 46 | $error = false; |
| 47 | + $response = []; |
46 | 48 |
|
47 | 49 | try { |
48 | 50 | $response = $this->collectionOfBlocks($blocks) |
@@ -100,19 +102,26 @@ protected function request(Collection $blocks) |
100 | 102 |
|
101 | 103 | protected function requestChunks($chunks) |
102 | 104 | { |
103 | | - return Http::pool(function (Pool $pool) use ($chunks) { |
104 | | - $host = Torchlight::config('host', 'https://api.torchlight.dev'); |
105 | | - $timeout = Torchlight::config('request_timeout', 5); |
106 | | - |
107 | | - $chunks->each(function ($blocks) use ($pool, $host, $timeout) { |
108 | | - $pool->timeout($timeout) |
| 105 | + $host = Torchlight::config('host', 'https://api.torchlight.dev'); |
| 106 | + $timeout = Torchlight::config('request_timeout', 5); |
| 107 | + |
| 108 | + // Can't use Http::pool here because it's not |
| 109 | + // available in Laravel 7 and early 8. |
| 110 | + return $chunks |
| 111 | + // This first map fires all the requests. |
| 112 | + ->map(function ($blocks) use ($host, $timeout) { |
| 113 | + return Http::async() |
109 | 114 | ->baseUrl($host) |
| 115 | + ->timeout($timeout) |
110 | 116 | ->withToken($this->getToken()) |
111 | 117 | ->post('highlight', [ |
112 | 118 | 'blocks' => $this->blocksAsRequestParam($blocks)->values()->toArray(), |
113 | 119 | ]); |
| 120 | + }) |
| 121 | + // The second one waits for them all to finish. |
| 122 | + ->map(function (Promise $request) { |
| 123 | + return $request->wait(); |
114 | 124 | }); |
115 | | - }); |
116 | 125 | } |
117 | 126 |
|
118 | 127 | protected function collectionOfBlocks($blocks) |
|
0 commit comments