Skip to content

Commit

Permalink
Limit the baton lifetime to 8 seconds, to avoid error: The stream has…
Browse files Browse the repository at this point in the history
… expired due to inactivity
  • Loading branch information
richan-fongdasen committed Nov 6, 2024
1 parent c706735 commit 1e089c9
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/TursoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace RichanFongdasen\Turso;

use Illuminate\Cache\ArrayStore;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand All @@ -17,10 +18,10 @@ class TursoClient
{
protected string $baseUrl;

protected ?string $baton = null;

protected Collection $config;

protected ArrayStore $connectionStore;

protected bool $loggingQueries;

protected Collection $queryLog;
Expand All @@ -33,6 +34,9 @@ public function __construct(array $config = [])
$config,
config('turso-laravel', [])
));
$this->baseUrl = (string) $this->config->get('db_url', '');

$this->connectionStore = new ArrayStore();

$this->queryLog = new Collection();

Expand All @@ -47,15 +51,15 @@ public function __destruct()

public function close(): void
{
if ((string) $this->baton === '') {
if ((string) $this->getBaton() === '') {
return;
}

$body = RequestBody::create($this->baton)
$body = RequestBody::create($this->getBaton())
->withCloseRequest();

$this->httpRequest()
->baseUrl($this->baseUrl)
->baseUrl($this->getBaseUrl())
->post('/v3/pipeline', $body->toArray());

$this->resetHttpClientState();
Expand Down Expand Up @@ -97,14 +101,14 @@ public function freshHttpRequest(): PendingRequest
return $this->httpRequest;
}

public function getBaseUrl(): ?string
public function getBaseUrl(): string
{
return $this->baseUrl;
return $this->connectionStore->get('baseUrl') ?? $this->baseUrl;
}

public function getBaton(): ?string
{
return $this->baton;
return $this->connectionStore->get('baton');
}

public function getQueryLog(): Collection
Expand All @@ -121,12 +125,12 @@ public function query(string $statement, array $bindingValues = []): QueryRespon
{
$query = new ExecuteQuery($statement, $bindingValues);

$requestBody = RequestBody::create($this->baton)
$requestBody = RequestBody::create($this->getBaton())
->withForeignKeyConstraints((bool) $this->config->get('foreign_key_constraints'))
->push($query);

$httpResponse = $this->httpRequest()
->baseUrl($this->baseUrl)
->baseUrl($this->getBaseUrl())
->post('/v3/pipeline', $requestBody->toArray());

if ($httpResponse->failed()) {
Expand All @@ -143,12 +147,8 @@ public function query(string $statement, array $bindingValues = []): QueryRespon
]);
}

$this->baton = $responseBody->getBaton();
$baseUrl = (string) $responseBody->getBaseUrl();

if ($baseUrl !== '') {
$this->baseUrl = $baseUrl;
}
$this->connectionStore->put('baton', $responseBody->getBaton(), 9);
$this->connectionStore->put('baseUrl', $responseBody->getBaseUrl(), 9);

return $responseBody->getQueryResponse($query->getIndex());
}
Expand All @@ -162,7 +162,7 @@ public function httpRequest(): PendingRequest

public function resetHttpClientState(): void
{
$this->baton = null;
$this->baseUrl = (string) $this->config->get('db_url', '');
$this->connectionStore->forget('baton');
$this->connectionStore->forget('baseUrl');
}
}

0 comments on commit 1e089c9

Please sign in to comment.