Skip to content

Commit

Permalink
Merge pull request #32 from designmynight/add-timeout-option
Browse files Browse the repository at this point in the history
feat: add option for request timeout
  • Loading branch information
Will Taylor-Jackson authored Feb 5, 2018
2 parents 5a09bfe + c417923 commit dc0dabf
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions src/DesignMyNight/Elasticsearch/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Connection extends BaseConnection

protected $indexSuffix = '';

protected $requestTimeout;

/**
* Create a new Elasticsearch connection instance.
*
Expand Down Expand Up @@ -158,7 +160,7 @@ public function selectOne($query, $bindings = [])
*/
public function select($params, $bindings = [])
{
return $this->connection->search($params);
return $this->connection->search($this->addClientParams($params));
}

/**
Expand Down Expand Up @@ -232,13 +234,13 @@ public function scroll($scrollId, $scrollTimeout, $limit){
/**
* Run an insert statement against the database.
*
* @param array $query
* @param array $bindings
* @param array $params
* @param array $bindings
* @return bool
*/
public function insert($params, $bindings = [])
{
return $this->connection->bulk($params);
return $this->connection->bulk($this->addClientParams($params));
}

/**
Expand Down Expand Up @@ -376,6 +378,41 @@ public function pretend(Closure $callback)

}

/**
* Get the timeout for the entire Elasticsearch request
* @return float
*/
public function getRequestTimeout(): float
{
return $this->requestTimeout;
}

/**
* Get the timeout for the entire Elasticsearch request
* @param float $requestTimeout seconds
* @return self
*/
public function setRequestTimeout(float $requestTimeout): self
{
$this->requestTimeout = $requestTimeout;

return $this;
}

/**
* Add client-specific parameters to the request params
* @param array $params
* @return array
*/
protected function addClientParams(array $params): array
{
if ($this->requestTimeout){
$params['client']['timeout'] = $this->requestTimeout;
}

return $params;
}

/**
* Dynamically pass methods to the connection.
*
Expand Down

0 comments on commit dc0dabf

Please sign in to comment.