- DESCRIPTION
- CONFIGURATION
  - `request_timeout`
  - `ping_timeout`
  - `dead_timeout`
  - `max_dead_timeout`
  - `sniff_request_timeout`
  - `sniff_timeout`
  - `handle_args`
- METHODS
  - `host()`
  - `port()`
  - `uri()`
  - `is_dead()`
  - `is_live()`
  - `next_ping()`
  - `ping_failures()`
  - `mark_dead()`
  - `mark_live()`
  - `force_ping()`
  - `pings_ok()`
  - `sniff()`
  - `process_response()`

# DESCRIPTION

Elasticsearch::Role::Cxn provides common functionality to the Cxn implementations. Cxn instances are created by a Elasticsearch::Role::CxnPool implentation, using the Elasticsearch::Cxn::Factory class.

# CONFIGURATION

The configuration parameters are as follows:

## `request_timeout`

        $e = Elasticsearch->new(
            request_timeout => 10
        );

How long a normal request (ie not a ping or sniff request) should wait before throwing a `Timeout` error. Defaults to `10` seconds.

## `ping_timeout`

        $e = Elasticsearch->new(
            request_timeout => 0.3
        );

How long a ping request should wait before throwing a `Timeout` error. Defaults to `0.3` seconds. You may want to increase this if you are using the Elasticsearch::CxnPool::Static connection pool with a slow network.

## `dead_timeout`

How long a Cxn should be considered to be dead (not used to serve requests), before it is retried. The default is `60` seconds. This value is increased by powers of 2 for each time a request fails. In other words, the delay after each failure is as follows:

        Failure     Delay
        1           60 * 1  = 60 seconds
        2           60 * 2  = 120 seconds
        3           60 * 4  = 240 seconds
        4           60 * 8  = 480 seconds
        5           60 * 16 = 960 seconds

## `max_dead_timeout`

The maximum delay that should be applied to a failed node. If the "dead_timeout" calculation results in a delay greater than `max_dead_timeout` (default `3,600` seconds) then the `max_dead_timeout` is used instead. In other words, dead nodes will be retried at least once every hour by default.

## `sniff_request_timeout`

How long a sniff request should wait before throwing a `Timeout` error. Defaults to `0.3` seconds. You may want to increase this if you are using the Elasticsearch::CxnPool::Sniff connection pool with a slow network.

## `sniff_timeout`

How long the node being sniffed should wait for responses from other nodes before responding to the client. Note: this is distinct from the "sniff_request_timeout". For example, let's say you have a cluster with 5 nodes, 2 of which are unhealthy (taking a long time to respond):

- If you sniff an unhealthy node, the request will throw a `Timeout` error after `sniff_request_timeout` seconds.

- If you sniff a healthy node, it will gather responses from the other nodes, and give up after `sniff_timeout` seconds, returning just the information it has managed to gather from the healthy nodes.

NOTE: The `sniff_request_timeout` must be longer than the `sniff_timeout` to ensure that you get information about healthy nodes from the cluster.

For CxnPool's which implement sniffing (see Elasticsearch::CxnPool::Sniff), the `sniff_timeout` is the amount of time that the node being sniffed will wait to receive responses from other nodes, before returning a list of healthy nodes.

## `handle_args`

Any default arguments which should be passed when creating a new instance of the class which handles the network transport, eg HTTP::Tiny.

# METHODS

None of the methods listed below are useful to the user. They are documented for those who are writing alternative implementations only.

## `host()`

        $host = $cxn->host;

The value of the `host` parameter, eg `search.domain.com`.

## `port()`

        $port = $cxn->port;

The value of the `port` parameter, eg `9200`.

## `uri()`

        $uri = $cxn->uri;

A URI object representing the node, eg `https://search.domain.com:9200/path`.

## `is_dead()`

        $bool = $cxn->is_dead

Is the current node marked as dead.

## `is_live()`

        $bool = $cxn->is_live

Is the current node marked as live.

## `next_ping()`

        $time = $cxn->next_ping($time)

Get/set the time for the next scheduled ping. If zero, no ping is scheduled and the cxn is considered to be alive. If -1, a ping is scheduled before the next use.

## `ping_failures()`

        $num = $cxn->ping_failures($num)

The number of times that a cxn has been marked as dead.

## `mark_dead()`

        $cxn->mark_dead

Mark the cxn as dead, set "next_ping()" and increment "ping_failures()".

## `mark_live()`

Mark the cxn as live, set "next_ping()" and "ping_failures()" to zero.

## `force_ping()`

Set "next_ping()" to -1 (ie before next use) and "ping_failures()" to zero.

## `pings_ok()`

        $bool = $cxn->pings_ok

Try to ping the node and call "mark_live()" or "mark_dead()" depending on the success or failure of the ping.

## `sniff()`

        $response = $cxn->sniff;

Send a sniff request to the node and return the response.

## `process_response()`

        ($code,$result) = $cxn->process_response($params, $code, $msg, $body );

Processes the response received from an Elasticsearch node and either returns the HTTP status code and the response body (deserialized from JSON) or throws an error of the appropriate type.

The `$params` are the original params passed to "perform_request()" in Elasticsearch::Transport, the `$code` is the HTTP status code, the `$msg` is the error message returned by the backend library and the `$body` is the HTTP response body returned by Elasticsearch.