Skip to content

Commit 708e9e5

Browse files
authored
Merge pull request #40 from cloudblue/improve_connection_backoff
hf - improve connection backoff
2 parents e7db788 + 6f12b3a commit 708e9e5

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

connect/eaas/worker.py

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -168,40 +168,49 @@ def get_capabilities(self):
168168
),
169169
)
170170

171-
@backoff.on_exception(
172-
backoff.expo,
173-
CommunicationError,
174-
max_time=_get_max_retry_time_generic,
175-
max_value=_get_max_retry_delay_time,
176-
on_backoff=_on_communication_backoff,
177-
)
178-
@backoff.on_exception(
179-
backoff.expo,
180-
MaintenanceError,
181-
max_time=_get_max_retry_time_maintenance,
182-
max_value=_get_max_retry_delay_time,
183-
on_backoff=_on_communication_backoff,
184-
)
185171
async def communicate(self):
186-
try:
187-
await self.ensure_connection()
188-
await self.send(self.get_capabilities())
189-
while self.run_event.is_set():
172+
@backoff.on_exception(
173+
backoff.expo,
174+
CommunicationError,
175+
factor=10,
176+
max_time=_get_max_retry_time_generic,
177+
max_value=_get_max_retry_delay_time,
178+
jitter=backoff.random_jitter,
179+
on_backoff=_on_communication_backoff,
180+
giveup=lambda _: not self.run_event.is_set(),
181+
)
182+
@backoff.on_exception(
183+
backoff.expo,
184+
MaintenanceError,
185+
factor=10,
186+
max_time=_get_max_retry_time_maintenance,
187+
max_value=_get_max_retry_delay_time,
188+
jitter=backoff.random_jitter,
189+
on_backoff=_on_communication_backoff,
190+
giveup=lambda _: not self.run_event.is_set(),
191+
)
192+
async def _do_communicate():
193+
try:
190194
await self.ensure_connection()
191-
message = await self.receive()
192-
if not message:
193-
continue
194-
await self.process_message(message)
195-
except ConnectionClosedError as e:
196-
logger.warning(f'Connection closed with code {e.rcvd} from: {self.get_url()}')
197-
raise CommunicationError()
198-
except InvalidStatusCode as ic:
199-
if ic.status_code == 502:
200-
logger.warning('InvalidStatusCode 502 raised. Maintenance in progress.')
201-
raise MaintenanceError()
202-
else:
203-
logger.warning(f'InvalidStatusCode {ic.status_code} raised.')
195+
await self.send(self.get_capabilities())
196+
while self.run_event.is_set():
197+
await self.ensure_connection()
198+
message = await self.receive()
199+
if not message:
200+
continue
201+
await self.process_message(message)
202+
except ConnectionClosedError as e:
203+
logger.warning(f'Connection closed with code {e.rcvd} from: {self.get_url()}')
204204
raise CommunicationError()
205+
except InvalidStatusCode as ic:
206+
if ic.status_code == 502:
207+
logger.warning('InvalidStatusCode 502 raised. Maintenance in progress.')
208+
raise MaintenanceError()
209+
else:
210+
logger.warning(f'InvalidStatusCode {ic.status_code} raised.')
211+
raise CommunicationError()
212+
213+
await _do_communicate()
205214

206215
async def run(self): # noqa: CCR001
207216
"""

0 commit comments

Comments
 (0)