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 wait_in_line(self, znode_label, timeout=None, blocked_by=None):
time_limit = None
if timeout is not None:
time_limit = time.time() + timeout
await self.create_unique_znode(znode_label)
while True:
if time_limit and time.time() >= time_limit:
await self.delete_unique_znode(znode_label)
raise exc.TimeoutError
owned_positions, contenders = await self.analyze_siblings()
if znode_label not in owned_positions:
raise exc.SessionLost
blockers = contenders[:owned_positions[znode_label]]
if blocked_by:
blockers = [
contender for contender in blockers
if self.determine_znode_label(contender) in blocked_by
]
if not blockers:
break
try:
await self.wait_on_sibling(blockers[-1], timeout)
except exc.TimeoutError:
await self.delete_unique_znode(znode_label)
connection_response = await self.conn.send_connect(
protocol.ConnectRequest(
protocol_version=0,
last_seen_zxid=self.last_zxid or 0,
timeout=int((self.timeout or 0) * 1000),
session_id=self.session_id or 0,
password=self.password,
read_only=self.allow_read_only,
)
)
if connection_response is None:
# handle issue with inconsistent zxid on reconnection
if self.state.current_state != States.LOST:
self.state.transition_to(States.LOST)
self.last_zxid = None
raise exc.SessionLost()
zxid, response = connection_response
self.last_zxid = zxid
if response.session_id == 0: # invalid session, probably expired
log.debug('Session lost')
if self.state.current_state != States.LOST:
self.state.transition_to(States.LOST)
raise exc.SessionLost()
log.info("Got session id %s", hex(response.session_id))
log.info("Negotiated timeout: %s seconds", response.timeout / 1000)
self.session_id = response.session_id
self.password = response.password
self.timeout = response.timeout / 1000
)
if connection_response is None:
# handle issue with inconsistent zxid on reconnection
if self.state.current_state != States.LOST:
self.state.transition_to(States.LOST)
self.last_zxid = None
raise exc.SessionLost()
zxid, response = connection_response
self.last_zxid = zxid
if response.session_id == 0: # invalid session, probably expired
log.debug('Session lost')
if self.state.current_state != States.LOST:
self.state.transition_to(States.LOST)
raise exc.SessionLost()
log.info("Got session id %s", hex(response.session_id))
log.info("Negotiated timeout: %s seconds", response.timeout / 1000)
self.session_id = response.session_id
self.password = response.password
self.timeout = response.timeout / 1000
self.last_zxid = zxid
async def acquire_write(self, timeout=None):
result = None
while not result:
try:
result = await self.wait_in_line("write", timeout)
except exc.SessionLost:
continue
return result
async def acquire(self, timeout=None):
result = None
while not result:
try:
result = await self.wait_in_line("lock", timeout)
except exc.SessionLost:
continue
return result