Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def start(self):
base, _leaf = self.base_path.rsplit("/", 1)
if base:
await self.client.ensure_path(base)
try:
await self.client.create(self.base_path, str(self._default))
self.value = self._default
self._version = 0
except exc.NodeExists:
await self._fetch()
async def obtain(self, duration):
lessees = await self.client.get_children(self.base_path)
if len(lessees) >= self.limit:
return False
time_limit = time.time() + duration.total_seconds()
try:
await self.create_unique_znode("lease", data=str(time_limit))
except exc.NodeExists:
log.warning("Lease for %s already obtained.", self.base_path)
callback = partial(asyncio.ensure_future, self.release(), loop=self.client.loop)
self.client.loop.call_later(duration.total_seconds(), callback)
return True
async def send(self, request):
response = None
while not response:
await self.retry_policy.enforce(request)
await self.ensure_safe_state(writing=request.writes_data)
try:
self.xid += 1
zxid, response = await self.conn.send(request, xid=self.xid)
self.last_zxid = zxid
self.set_heartbeat()
self.retry_policy.clear(request)
except (exc.NodeExists, exc.NoNode, exc.NotEmpty, exc.BadVersion):
self.retry_policy.clear(request)
raise
except asyncio.CancelledError:
self.retry_policy.clear(request)
raise
except exc.ConnectError:
if self.state != States.SUSPENDED:
self.state.transition_to(States.SUSPENDED)
except Exception as e:
log.exception('Send exception: {}'.format(e))
self.retry_policy.clear(request)
raise e
return response
async def create_znode(self, path):
try:
await self.client.create(path)
except exc.NodeExists:
pass
except exc.NoNode:
try:
await self.ensure_path()
except exc.NodeExists:
pass
paths_to_make.append("/".join([paths_to_make[-1], segment]))
while paths_to_make:
path = paths_to_make[0]
if self.features.create_with_stat:
request = protocol.Create2Request(path=path, acl=acl)
else:
request = protocol.CreateRequest(path=path, acl=acl)
request.set_flags(
ephemeral=False, sequential=False, container=self.features.containers
)
try:
await self.send(request)
except exc.NodeExists:
pass
paths_to_make.pop(0)