Skip to content

Commit

Permalink
added ability to enable / disable deflate filter on write queries
Browse files Browse the repository at this point in the history
  • Loading branch information
facedsid committed Jul 14, 2020
1 parent 0ba7783 commit c762dbe
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/Transport/HttpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class HttpTransport implements TransportInterface
protected $httpClient;

/**
* Array with two keys (read and write) with guzzle options for corresponding requests.
* Array with three keys (read, write and deflate) with guzzle options for corresponding requests.
*
* [
* 'read' => [
Expand All @@ -41,6 +41,7 @@ class HttpTransport implements TransportInterface
* 'debug' => true,
* 'timeout' => 100,
* ],
* 'deflate' => true
* ]
*
* @var array
Expand All @@ -60,17 +61,32 @@ public function __construct(Client $client = null, array $options = [])
$this->options = $options;
}

/**
* Returns flag to enable / disable queries and data compression
*
* @return bool
*/
protected function isDeflateEnabled(): bool
{
return $this->options['deflate'] ?? true;
}

/**
* Returns default headers for requests.
*
* @return array
*/
protected function getHeaders()
{
return [
$headers = [
'Accept-Encoding' => 'gzip',
'Content-Encoding' => 'gzip',
];

if ($this->isDeflateEnabled()) {
$headers['Content-Encoding'] = 'gzip';
}

return $headers;
}

/**
Expand Down Expand Up @@ -121,7 +137,7 @@ public function write(array $queries, int $concurrency = 5) : array
'query' => $query->getQuery(),
], $query->getSettings());

$stream = $file->open();
$stream = $file->open($this->isDeflateEnabled());
$openedStreams[] = $stream;

$request = new Request('POST', $uri, $headers, $stream);
Expand All @@ -133,7 +149,8 @@ public function write(array $queries, int $concurrency = 5) : array

$uri = $this->buildRequestUri($query->getServer(), [], $query->getSettings());

$request = new Request('POST', $uri, $headers, gzencode($query->getQuery()));
$sql = $this->isDeflateEnabled() ? gzencode($query->getQuery()) : $query->getQuery();
$request = new Request('POST', $uri, $headers, $sql);

yield $request;
}
Expand Down

0 comments on commit c762dbe

Please sign in to comment.