Skip to content

Commit 20765dd

Browse files
committed
Roll back pooling, as it's done at the API level now.
1 parent 1fc9da2 commit 20765dd

File tree

2 files changed

+27
-87
lines changed

2 files changed

+27
-87
lines changed

src/Client.php

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Torchlight;
77

88
use GuzzleHttp\Promise\Promise;
9+
use Illuminate\Http\Client\ConnectionException;
910
use Illuminate\Support\Arr;
1011
use Illuminate\Support\Collection;
1112
use Illuminate\Support\Facades\Http;
@@ -42,35 +43,33 @@ public function highlight($blocks)
4243

4344
protected function request(Collection $blocks)
4445
{
45-
$error = false;
46-
$response = [];
47-
4846
try {
49-
$response = $this->collectionOfBlocks($blocks)
50-
->chunk(Torchlight::config('request_chunk_size', 15))
51-
->pipe(function ($chunks) {
52-
return collect($this->requestChunks($chunks));
53-
})
54-
->map(function ($response) use (&$error) {
55-
if ($response instanceof Throwable) {
56-
$error = $response;
57-
58-
return [];
59-
}
60-
61-
if ($response->failed()) {
62-
$error = $response->toException();
63-
64-
return [];
65-
}
66-
67-
return Arr::get($response->json(), 'blocks', []);
68-
})
69-
->flatten(1);
47+
$host = Torchlight::config('host', 'https://api.torchlight.dev');
48+
$timeout = Torchlight::config('request_timeout', 5);
49+
50+
$response = Http::baseUrl($host)
51+
->timeout($timeout)
52+
->withToken($this->getToken())
53+
->post('highlight', [
54+
'blocks' => $this->blocksAsRequestParam($blocks)->values()->toArray(),
55+
]);
56+
57+
if ($response->failed()) {
58+
$this->potentiallyThrowRequestException($response->toException());
59+
$response = [];
60+
} else {
61+
$response = $response->json();
62+
}
63+
7064
} catch (Throwable $e) {
71-
$this->throwUnlessProduction($e);
65+
$e instanceof ConnectionException
66+
? $this->potentiallyThrowRequestException($e)
67+
: $this->throwUnlessProduction($e);
68+
69+
$response = [];
7270
}
7371

72+
$response = Arr::get($response, 'blocks', []);
7473
$response = collect($response)->keyBy('id');
7574

7675
$blocks->each(function (Block $block) use ($response) {
@@ -94,8 +93,6 @@ protected function request(Collection $blocks)
9493
// Only store the ones we got back from the API.
9594
$this->setCacheFromBlocks($blocks, $response->keys());
9695

97-
$this->potentiallyThrowRequestException($error);
98-
9996
return $blocks;
10097
}
10198

@@ -148,7 +145,9 @@ protected function getToken()
148145
protected function potentiallyThrowRequestException($exception)
149146
{
150147
if ($exception) {
151-
$this->throwUnlessProduction(new RequestException($exception->getMessage()));
148+
$wrapped = new RequestException('A Torchlight request exception has occurred.', 0, $exception);
149+
150+
$this->throwUnlessProduction($wrapped);
152151
}
153152
}
154153

tests/ClientTest.php

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -194,63 +194,4 @@ public function a_500_error_returns_a_default_in_production()
194194
$this->assertEquals('<pre><code class=\'torchlight\'>echo &quot;hello world&quot;;</code></pre>', $block->wrapped);
195195
}
196196

197-
/** @test */
198-
public function multiple_requests_get_chunked()
199-
{
200-
config()->set('torchlight.request_chunk_size', 2);
201-
202-
$this->fakeSuccessfulResponse('1');
203-
$this->fakeSuccessfulResponse('2');
204-
$this->fakeSuccessfulResponse('3');
205-
$this->fakeSuccessfulResponse('4');
206-
207-
Torchlight::highlight([
208-
Block::make('1')->language('php')->code('echo "hello world 1";'),
209-
Block::make('2')->language('php')->code('echo "hello world 2";'),
210-
Block::make('3')->language('php')->code('echo "hello world 3";'),
211-
Block::make('4')->language('php')->code('echo "hello world 4";'),
212-
]);
213-
214-
// All these should be cached.
215-
Torchlight::highlight([
216-
Block::make('1')->language('php')->code('echo "hello world 1";'),
217-
Block::make('2')->language('php')->code('echo "hello world 2";'),
218-
Block::make('3')->language('php')->code('echo "hello world 3";'),
219-
Block::make('4')->language('php')->code('echo "hello world 4";'),
220-
]);
221-
222-
// 2 chunks sent, second set was cached.
223-
Http::assertSentCount(2);
224-
}
225-
226-
/** @test */
227-
public function blocks_still_get_cached_if_one_request_fails()
228-
{
229-
config()->set('torchlight.request_chunk_size', 1);
230-
231-
$this->fakeSuccessfulResponse('1');
232-
$this->fakeSuccessfulResponse('2');
233-
$this->fakeSuccessfulResponse('3');
234-
235-
$this->fakeTimeout('4');
236-
237-
try {
238-
Torchlight::highlight([
239-
$block1 = Block::make('1')->language('php')->code('echo "hello world 1";'),
240-
$block2 = Block::make('2')->language('php')->code('echo "hello world 2";'),
241-
$block3 = Block::make('3')->language('php')->code('echo "hello world 3";'),
242-
$block4 = Block::make('4')->language('php')->code('echo "hello world 4";'),
243-
]);
244-
} catch (RequestException $exception) {
245-
}
246-
247-
// Exception should have been thrown.
248-
$this->assertInstanceOf(RequestException::class, $exception);
249-
250-
$client = new Client;
251-
252-
$this->assertNotNull(Cache::get($client->cacheKey($block1)));
253-
$this->assertNotNull(Cache::get($client->cacheKey($block2)));
254-
$this->assertNotNull(Cache::get($client->cacheKey($block3)));
255-
}
256197
}

0 commit comments

Comments
 (0)