client
Restful json clients.
Use Resource for a single host.
Use Resources for multiple hosts with simple partitioning or redundancy.
Use Shards for horizontally partitioning hosts by different keys.
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.
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
-
__nonzero__()[source]
Return whether status is successful.
-
__call__()[source]
Return evaluated response body or raise exception.
Resource
-
class lupyne.client.Resource(host, port=None, strict=None, timeout=<object object at 0x10749d4d0>, source_address=None)[source]
Bases: httplib.HTTPConnection
Synchronous connection which handles json responses.
-
getresponse(buffering=False)
Get the response from the server.
-
call(method, path, body=None, params=(), redirect=False)[source]
Send request and return completed response.
-
delete(path, **params)[source]
-
get(path, **params)[source]
-
multicall(*requests)[source]
Pipeline requests (method, path[, body]) and return completed responses.
-
post(path, body=None, **kwargs)[source]
-
put(path, body=None, **kwargs)[source]
-
request(method, path, body=None)[source]
Send request after handling body and headers.
-
response_class
alias of Response
Resources
-
class lupyne.client.Resources(hosts, limit=0)[source]
Bases: dict
Thread-safe mapping of hosts to optionally persistent resources.
Parameters: |
- hosts – host[:port] strings
- limit – maximum number of cached connections per host
|
-
broadcast(method, path, body=None, hosts=())[source]
Send requests and return responses from all hosts, optionally from given subset.
-
choice(hosts)[source]
Return chosen host according to priority.
-
getresponse(host, resource)[source]
Return response and release resource if request completed.
Return None if it appears request may be repeated.
-
priority(host)[source]
Return priority for host. None may be used to eliminate from consideration.
-
class queue[source]
Bases: collections.deque
Queue of prioritized resources.
-
Resources.request(host, method, path, body=None)[source]
Send request to given host and return exclusive resource.
-
Resources.unicast(method, path, body=None, hosts=())[source]
Send request and return response from any host, optionally from given subset.
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
|
-
resources
Resources mapping.
-
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.
-
multicast(keys, method, path, body=None)[source]
Send requests and return responses from a minimal subset of hosts which cover all corresponding keys.
Response overlap is possible depending on partitioning.
-
priority(hosts)[source]
Return combined priority for hosts.
-
unicast(key, method, path, body=None)[source]
Send request and return response from any host for corresponding key.