Skip to content

Commit

Permalink
deprecate endpoint timeout (solariumphp#767) (solariumphp#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher authored Apr 2, 2020
1 parent b57cf52 commit 202867d
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Zend2HttpAdapter, use PSR-18 http adapter instead
- GuzzleAdapter, use PSR-18 http adapter instead
- Guzzle3Adapter, use PSR-18 http adapter instead
- AdapterHelper
- Endpoint::setTimeout and Endpoint::getTimeout, configure the timeout on the http adapter instead


## [5.1.6]
Expand Down
8 changes: 5 additions & 3 deletions src/Core/Client/Adapter/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*
* @author Intervals <[email protected]>
*/
class Curl extends Configurable implements AdapterInterface
class Curl extends Configurable implements AdapterInterface, TimeoutAwareInterface
{
use TimeoutAwareTrait;

/**
* Execute a Solr request using the cURL Http.
*
Expand Down Expand Up @@ -207,10 +209,10 @@ protected function init()
protected function createOptions($request, $endpoint)
{
$options = [
'timeout' => $endpoint->getTimeout(),
'timeout' => $this->timeout ?? $endpoint->getTimeout(),
];
foreach ($request->getHeaders() as $headerLine) {
list($header, $value) = explode(':', $headerLine);
[$header, $value] = explode(':', $headerLine);
if ($header = trim($header)) {
$options['headers'][$header] = trim($value);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Core/Client/Adapter/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
/**
* Basic HTTP adapter using a stream.
*/
class Http extends Configurable implements AdapterInterface
class Http extends Configurable implements AdapterInterface, TimeoutAwareInterface
{
use TimeoutAwareTrait;

/**
* Handle Solr communication.
*
Expand Down Expand Up @@ -68,7 +70,7 @@ public function createContext($request, $endpoint)
$context = stream_context_create(
['http' => [
'method' => $method,
'timeout' => $endpoint->getTimeout(),
'timeout' => $this->timeout ?? $endpoint->getTimeout(),
'protocol_version' => 1.0,
'user_agent' => 'Solarium Http Adapter',
],
Expand Down
18 changes: 18 additions & 0 deletions src/Core/Client/Adapter/TimeoutAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Solarium\Core\Client\Adapter;

/**
* Contract for Http Adapters that are aware of timeouts.
*/
interface TimeoutAwareInterface
{
/**
* default timeout that should be respected by adapters implementing this interface.
*/
public const DEFAULT_TIMEOUT = 5;

public function setTimeout(int $timeoutInSeconds): void;

public function getTimeout(): int;
}
24 changes: 24 additions & 0 deletions src/Core/Client/Adapter/TimeoutAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Solarium\Core\Client\Adapter;

/**
* @internal
*/
trait TimeoutAwareTrait
{
/**
* @var int|null
*/
private $timeout;

public function setTimeout(int $timeoutInSeconds): void
{
$this->timeout = $timeoutInSeconds;
}

public function getTimeout(): int
{
return $this->timeout ?? TimeoutAwareInterface::DEFAULT_TIMEOUT;
}
}
8 changes: 8 additions & 0 deletions src/Core/Client/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,13 @@ public function getCore(): ?string
* @param int $timeout
*
* @return self Provides fluent interface
*
* @deprecated Endpoint::setTimeout is deprecated since Solarium 5.2 and will be removed in Solarium 6. Configure the timeout on the HTTP Client used by the Adapter instead.
*/
public function setTimeout(int $timeout): self
{
@trigger_error('Endpoint::setTimeout is deprecated since Solarium 5.2 and will be removed in Solarium 6. Configure the timeout on the HTTP Client used by the Adapter instead.', E_USER_DEPRECATED);

$this->setOption('timeout', $timeout);
return $this;
}
Expand All @@ -205,9 +209,13 @@ public function setTimeout(int $timeout): self
* Get timeout option.
*
* @return int|null
*
* @deprecated Endpoint::getTimeout is deprecated since Solarium 5.2 and will be removed in Solarium 6. Configure the timeout on the HTTP Client used by the Adapter instead.
*/
public function getTimeout(): ?int
{
@trigger_error('Endpoint::getTimeout is deprecated since Solarium 5.2 and will be removed in Solarium 6. Configure the timeout on the HTTP Client used by the Adapter instead.', E_USER_DEPRECATED);

return $this->getOption('timeout');
}

Expand Down
10 changes: 5 additions & 5 deletions tests/Core/Client/Adapter/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testCreateContextGetRequest()
$request->setMethod($method);
$request->setIsServerRequest(true);
$endpoint = new Endpoint();
$endpoint->setTimeout($timeout);
$this->adapter->setTimeout($timeout);

$context = $this->adapter->createContext($request, $endpoint);

Expand Down Expand Up @@ -122,7 +122,7 @@ public function testCreateContextWithHeaders()
$request->addHeader($header2);
$request->setIsServerRequest(true);
$endpoint = new Endpoint();
$endpoint->setTimeout($timeout);
$this->adapter->setTimeout($timeout);

$context = $this->adapter->createContext($request, $endpoint);

Expand Down Expand Up @@ -151,7 +151,7 @@ public function testCreateContextPostRequest()
$request->setRawData($data);
$request->setIsServerRequest(true);
$endpoint = new Endpoint();
$endpoint->setTimeout($timeout);
$this->adapter->setTimeout($timeout);

$context = $this->adapter->createContext($request, $endpoint);

Expand Down Expand Up @@ -180,7 +180,7 @@ public function testCreateContextPostFileRequest()
$request->setFileUpload(__FILE__);
$request->setIsServerRequest(true);
$endpoint = new Endpoint();
$endpoint->setTimeout($timeout);
$this->adapter->setTimeout($timeout);

$context = $this->adapter->createContext($request, $endpoint);

Expand Down Expand Up @@ -213,7 +213,7 @@ public function testCreateContextWithAuthorization()
$request->setIsServerRequest(true);

$endpoint = new Endpoint();
$endpoint->setTimeout($timeout);
$this->adapter->setTimeout($timeout);

$context = $this->adapter->createContext($request, $endpoint);

Expand Down
3 changes: 3 additions & 0 deletions tests/Core/Client/EndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public function testSetAndGetCore()
$this->assertSame('core1', $this->endpoint->getCore());
}

/**
* @group legacy
*/
public function testSetAndGetTimeout()
{
$this->endpoint->setTimeout(7);
Expand Down
5 changes: 3 additions & 2 deletions tests/Integration/SolrCloud/CollectionsCurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public function setUp(): void
parent::setUp();
// The default timeout of solarium of 5s seems to be too aggressive on travis and causes random test failures.
// Set it to the PHP default of 13s.
$this->client->setAdapter(new Curl());
$this->client->getEndpoint()->setTimeout(CURLOPT_TIMEOUT);
$adapter = new Curl();
$adapter->setTimeout(CURLOPT_TIMEOUT);
$this->client->setAdapter($adapter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public function setUp(): void
parent::setUp();
// The default timeout of solarium of 5s seems to be too aggressive on travis and causes random test failures.
// Set it to the PHP default of 13s.
$this->client->setAdapter(new Curl());
$this->client->getEndpoint()->setTimeout(CURLOPT_TIMEOUT);
$adapter = new Curl();
$adapter->setTimeout(CURLOPT_TIMEOUT);
$this->client->setAdapter($adapter);
}
}

0 comments on commit 202867d

Please sign in to comment.