client¶
Restful json clients.
Use Resource for a connection to a single host. Use Pool for persistent thread-safe connections to a single host. Use Resources for multiple hosts with simple partitioning or replication. Use Shards for horizontally partitioning hosts by different keys. Use Replicas in coordination with automatic host synchronization.
Resources optionally reuse connections, handling request timeouts. Broadcasting to multiple resources is parallelized with asynchronous requests and responses.
The load balancing strategy is randomized, biased by the number of cached connections available. This inherently provides limited failover support, but applications must still handle exceptions as desired. Replicas will automatically retry if host is unreachable.
Note
Caching more client connections than the backend server supports may cause blocking under load. CherryPy’s default thread pool is 10.
Response¶
-
class
lupyne.client.
Response
(sock, debuglevel=0, strict=0, method=None, buffering=False)[source]¶ Bases:
httplib.HTTPResponse
A completed response which handles json and caches its body.
-
status
¶ HTTP status code
-
reason
¶ HTTP status message
-
body
¶ string of entire response body
-
time
¶ server response time
-
Resource¶
-
class
lupyne.client.
Resource
(host, port=None, strict=None, timeout=<object object>, source_address=None)[source]¶ Bases:
httplib.HTTPConnection
Synchronous connection which handles json responses.
-
call
(method, path, body=None, params=(), redirect=False)[source]¶ Send request and return completed response.
-
getresponse
(filename='')[source]¶ Return completed response, optionally write response body to a file.
-
Pool¶
New in version 1.2.
Resources¶
-
class
lupyne.client.
Resources
(hosts, limit=0)[source]¶ Bases:
dict
Thread-safe mapping of hosts to resource pools.
Parameters: - hosts – host[:port] strings
- limit – maximum number of cached resources per host
Shards¶
-
class
lupyne.client.
Shards
(items=(), limit=0, **multimap)[source]¶ Bases:
dict
Mapping of keys to host clusters, with associated resources.
Parameters: - items – host, key pairs
- limit – maximum number of cached connections per host
- multimap – mapping of hosts to multiple keys
-
broadcast
(key, method, path, body=None)[source]¶ Send requests and return responses from all hosts for corresponding key.
-
choice
(hosts)¶ Return chosen host according to priority.
Replicas¶
New in version 1.2.
-
class
lupyne.client.
Replicas
(hosts, limit=0)[source]¶ Bases:
lupyne.client.Resources
Resources which failover assuming the hosts are being automatically synchronized. Writes are dispatched to the first host and sequentially failover. Reads are balanced among all remaining hosts.
-
call
(method, path, body=None, params=(), retry=False)[source]¶ Send request and return completed response, even if hosts are unreachable.
Parameters: retry – optionally retry request on http errors as well, such as waiting for indexer promotion
-
delete
(path, **params)¶ Return response body from DELETE request.
-
get
(path, **params)¶ Return response body from GET request.
-
post
(path, body=None, **kwargs)¶ Return response body from POST request.
-
put
(path, body=None, **kwargs)¶ Return response body from PUT request.
-