Skip to content

Commit cf27d1f

Browse files
dsotirho-ucschannes-ucsc
authored andcommitted
Refactor server-side sleep into parent class
1 parent bfecd7a commit cf27d1f

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

src/azul/chalice.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import mimetypes
1616
import os
1717
import pathlib
18+
import time
1819
from typing import (
1920
Any,
2021
Iterator,
@@ -617,3 +618,27 @@ def lambda_context(self) -> LambdaContext:
617618
@property
618619
def current_request(self) -> AzulRequest:
619620
return self.app.current_request
621+
622+
def server_side_sleep(self, max_seconds: float) -> float:
623+
"""
624+
Sleep in the current Lambda function.
625+
626+
:param max_seconds: The requested amount of time to sleep in seconds.
627+
The actual time slept will be less if the requested
628+
amount would cause the Lambda function to exceed its
629+
execution timeout.
630+
631+
:return: The actual amount of time slept in seconds
632+
"""
633+
assert isinstance(max_seconds, float), max_seconds
634+
assert 0 <= max_seconds <= config.api_gateway_lambda_timeout, max_seconds
635+
remaining_time = self.lambda_context.get_remaining_time_in_millis() / 1000
636+
# A small buffer is subtracted from the Lambda's remaining time to
637+
# ensure that it wakes up before it runs out of execution time (and
638+
# before API Gateway times out) so it gets a chance to return a response
639+
# to the client.
640+
actual_seconds = min(max_seconds,
641+
remaining_time - config.api_gateway_timeout_padding - 3)
642+
log.debug('Sleeping for %.3f seconds', actual_seconds)
643+
time.sleep(actual_seconds)
644+
return actual_seconds

src/azul/service/drs_controller.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from datetime import (
1313
datetime,
1414
)
15-
import time
1615
from typing import (
1716
Optional,
1817
)
@@ -184,10 +183,7 @@ def _dos_gs_url(self, file_uuid, version) -> mutable_furl:
184183
return url
185184
elif dss_response.status_code == 301:
186185
url = dss_response.next.url
187-
remaining_lambda_seconds = self.lambda_context.get_remaining_time_in_millis() / 1000
188-
server_side_sleep = min(1,
189-
max(remaining_lambda_seconds - config.api_gateway_timeout_padding - 3, 0))
190-
time.sleep(server_side_sleep)
186+
self.server_side_sleep(1.0)
191187
else:
192188
raise ChaliceViewError({
193189
'msg': f'Received {dss_response.status_code} from DSS. Could not get file'

src/azul/service/repository_controller.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
)
55
import json
66
import logging
7-
import time
87
from typing import (
98
Optional,
109
TYPE_CHECKING,
@@ -21,7 +20,6 @@
2120
CatalogName,
2221
cache,
2322
cached_property,
24-
config,
2523
reject,
2624
)
2725
from azul.auth import (
@@ -264,14 +262,8 @@ def download_file(self,
264262
if wait == '0':
265263
pass
266264
elif wait == '1':
267-
# Sleep in the lambda but ensure that we wake up before it
268-
# runs out of execution time (and before API Gateway times
269-
# out) so we get a chance to return a response to the client
270-
remaining_time = self.lambda_context.get_remaining_time_in_millis() / 1000
271-
server_side_sleep = min(float(retry_after),
272-
remaining_time - config.api_gateway_timeout_padding - 3)
273-
time.sleep(server_side_sleep)
274-
retry_after = round(retry_after - server_side_sleep)
265+
time_slept = self.server_side_sleep(float(retry_after))
266+
retry_after = round(retry_after - time_slept)
275267
else:
276268
assert False, wait
277269
query_params['wait'] = wait

0 commit comments

Comments
 (0)