A list of servers can be passed while creating an instance of the http client:
>>> http_client = HttpClient([crate_host])
Its also possible to pass a single server as a string:
>>> http_client = HttpClient(crate_host)
If no server` argument (or no argument at all) is passed, the default one ``127.0.0.1:9200 is used:
>>> http_client = HttpClient()
>>> http_client._active_servers
['127.0.0.1:9200']
When using a list of servers, the servers are selected by round-robin:
>>> invalid_host = "invalid_host:9999"
>>> even_more_invalid_host = "even_more_invalid_host:9999"
>>> http_client = HttpClient([crate_host, invalid_host, even_more_invalid_host])
>>> http_client._get_server()
'127.0.0.1:9295'
>>> http_client._get_server()
'invalid_host:9999'
>>> http_client._get_server()
'even_more_invalid_host:9999'
Servers with connection errors will be removed from the active server list:
>>> http_client = HttpClient([invalid_host, even_more_invalid_host, crate_host])
>>> result = http_client.sql('select name from locations')
>>> http_client._active_servers
['127.0.0.1:9295']
Inactive servers will be re-added after a given time interval. To validate this, set the interval very short and sleep for that interval:
>>> http_client.retry_interval = 1
>>> import time; time.sleep(1)
>>> result = http_client.sql('select name from locations')
>>> http_client._active_servers
['invalid_host:9999', 'even_more_invalid_host:9999', '127.0.0.1:9295']
If no active servers are available and the retry interval is not reached, just use the oldest inactive one:
>>> http_client = HttpClient([invalid_host, even_more_invalid_host, crate_host])
>>> result = http_client.sql('select name from locations')
>>> http_client._active_servers = []
>>> http_client._get_server()
'invalid_host:9999'
Issue a select statement against our with test data pre-filled crate instance:
>>> http_client = HttpClient(crate_host)
>>> result = http_client.sql('select name from locations')
>>> pprint(result)
{u'cols': [u'name'],
u'rows': [[u'Algol'],
[u'Folfanga'],
[u'Aldebaran'],
[u'Argabuthon'],
[u'Bartledan'],
[u'Galactic Sector QQ7 Active J Gamma'],
[u'Allosimanius Syneca'],
[u'Arkintoofle Minor'],
[u'Outer Eastern Rim'],
[u'Altair']]}
It’s possible to define a HTTP timeout in seconds on client instantiation, so a timeout exception is raised when timeout is reached:
>>> http_client = HttpClient(crate_host, timeout=0.001)
>>> http_client.sql('select name from locations')
Traceback (most recent call last):
...
ConnectionError: No more Servers available, exception from last server: HTTPConnectionPool(host='...', port=...): Request timed out. (timeout=0.001)