pylxd
pylxd copied to clipboard
_APINode is very inefficient and creates a new Requests object for every part of the path
In the pylxd/client.py file there is the following code in the _APINode class:
class _APINode(object):
"""An api node object."""
def __init__(self, api_endpoint, cert=None, verify=True, timeout=None):
self._api_endpoint = api_endpoint
self._timeout = timeout
if self._api_endpoint.startswith('http+unix://'):
self.session = requests_unixsocket.Session()
else:
self.session = requests.Session()
self.session.cert = cert
self.session.verify = verify
This looks okay on the face of it, except when you realise that _APINode is created for each part (method) of a call like:
client.api.containers[name].post()
In this call, 3 requests.Session() or requests_unixsocket.Session() objects are created, and only the final one is used in the post(). This is using up resources that simply don't need to be made AND makes testing harder as new _APINodes keep popping up dynamically as calls are constructed.
Going forward, the code should delay the creation of the requests or requests_unixsocket objects until the end method is called.