Skip to content

Commit

Permalink
Merge pull request #46 from mentionapp/configurable-timeout
Browse files Browse the repository at this point in the history
We want the http timeout to be longer and configurable
  • Loading branch information
nervetattoo committed Aug 6, 2014
2 parents 24dc35b + 0b535aa commit 6c63c4f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
48 changes: 48 additions & 0 deletions README.markdown
Expand Up @@ -121,3 +121,51 @@ $es->createBulk()

```

### Usage as a service in Symfony2

In order to use the Dependency Injection to inject the client as a service, you'll have to define it before.
So in your bundle's services.yml file you can put something like this :
```yml
your_bundle.elastic_transport:
class: ElasticSearch\Transport\HTTP
arguments:
- localhost
- 9200
- 60

your_bundle.elastic_client:
class: ElasticSearch\Client
arguments:
- @your_bundle.elastic_transport
```
To make Symfony2 recognize the `ElasticSearch` namespace, you'll have to register it. So in your `app/autoload.php` make sure your have :
```php
<?php
// ...

$loader->registerNamespaces(array(
// ...
'ElasticSearch' => __DIR__.'/path/to/your/vendor/nervetattoo/elasticsearch/src',
));
```
Then, you can get your client via the service container and use it like usual. For example, in your controller you can do this :
```php
<?php
class FooController extends Controller
{
// ...

public function barAction()
{
// ...
$es = $this->get('your_bundle.elastic_client');
$results = $es
->setIndex(array("one", "two"))
->setType(array("mytype", "other-type"))
->search('title:cool');
}
}
```



12 changes: 10 additions & 2 deletions src/ElasticSearch/Client.php
Expand Up @@ -23,7 +23,8 @@ class Client {
'protocol' => Client::DEFAULT_PROTOCOL,
'servers' => Client::DEFAULT_SERVER,
'index' => Client::DEFAULT_INDEX,
'type' => Client::DEFAULT_TYPE
'type' => Client::DEFAULT_TYPE,
'timeout' => null,
);

protected static $_protocols = array(
Expand Down Expand Up @@ -66,6 +67,7 @@ public static function connection($config = array()) {
if (is_string($config)) {
$config = self::parseDsn($config);
}

$config += self::$_defaults;

$protocol = $config['protocol'];
Expand All @@ -74,9 +76,15 @@ public static function connection($config = array()) {
}
$class = self::$_protocols[$protocol];

if (null !== $config['timeout'] && !is_numeric($config['timeout'])) {
throw new \Exception("HTTP timeout should have a numeric value when specified.");
}

$server = is_array($config['servers']) ? $config['servers'][0] : $config['servers'];
list($host, $port) = explode(':', $server);
$transport = new $class($host, $port);

$transport = new $class($host, $port, $config['timeout']);

$client = new self($transport, $config['index'], $config['type']);
$client->config($config);
return $client;
Expand Down
19 changes: 16 additions & 3 deletions src/ElasticSearch/Transport/HTTP.php
Expand Up @@ -20,7 +20,7 @@ class HTTP extends Base {
/**
* How long before timing out CURL call
*/
const TIMEOUT = 5;
private $timeout = 5;

/**
* curl handler which is needed for reusing existing http connection to the server
Expand All @@ -29,8 +29,11 @@ class HTTP extends Base {
protected $ch;


public function __construct($host='localhost', $port=9200) {
public function __construct($host='localhost', $port=9200, $timeout=null) {
parent::__construct($host, $port);
if(null !== $timeout) {
$this->setTimeout($timeout);
}
$this->ch = curl_init();
}

Expand Down Expand Up @@ -157,7 +160,7 @@ protected function call($url, $method="GET", $payload=null) {
$protocol = "http";
$requestURL = $protocol . "://" . $this->host . $url;
curl_setopt($conn, CURLOPT_URL, $requestURL);
curl_setopt($conn, CURLOPT_TIMEOUT, self::TIMEOUT);
curl_setopt($conn, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($conn, CURLOPT_PORT, $this->port);
curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1) ;
curl_setopt($conn, CURLOPT_CUSTOMREQUEST, strtoupper($method));
Expand Down Expand Up @@ -221,4 +224,14 @@ protected function call($url, $method="GET", $payload=null) {

return $data;
}

public function setTimeout($timeout)
{
$this->timeout = $timeout;
}

public function getTimeout()
{
return $this->timeout;
}
}
4 changes: 2 additions & 2 deletions src/ElasticSearch/Transport/Memcached.php
Expand Up @@ -15,10 +15,10 @@
*/

class Memcached extends Base {
public function __construct($host="127.0.0.1", $port=11311) {
public function __construct($host="127.0.0.1", $port=11311, $timeout=null) {
parent::__construct($host, $port);
$this->conn = new Memcache;
$this->conn->connect($host, $port);
$this->conn->connect($host, $port, $timeout);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/units/Client.php
Expand Up @@ -20,7 +20,8 @@ public function testDsnIsCorrectlyParsed() {
'protocol' => 'http',
'servers' => 'test.com:9100',
'index' => 'index',
'type' => 'type'
'type' => 'type',
'timeout' => null,
);
$this->assert->array($search->config())
->isEqualTo($config);
Expand Down

0 comments on commit 6c63c4f

Please sign in to comment.