Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement code review comments.
  • Loading branch information
Pushkar Chawda
Pushkar Chawda committed Oct 27, 2023
commit ab3c30d15f6caa07937870b6dabec8d761236590
8 changes: 4 additions & 4 deletions awslambdaric/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,9 @@ def run(app_root, handler, lambda_runtime_api_addr):
)

with create_log_sink() as log_sink:
lambda_runtime_client = LambdaRuntimeClient(lambda_runtime_api_addr)
lambda_runtime_client = LambdaRuntimeClient(
lambda_runtime_api_addr, use_thread_for_polling_next
)

try:
_setup_logging(_AWS_LAMBDA_LOG_FORMAT, _AWS_LAMBDA_LOG_LEVEL, log_sink)
Expand All @@ -483,9 +485,7 @@ def run(app_root, handler, lambda_runtime_api_addr):
sys.exit(1)

while True:
event_request = lambda_runtime_client.wait_next_invocation(
use_thread_for_polling_next
)
event_request = lambda_runtime_client.wait_next_invocation()

_GLOBAL_AWS_REQUEST_ID = event_request.invoke_id

Expand Down
18 changes: 10 additions & 8 deletions awslambdaric/lambda_runtime_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ class LambdaRuntimeClient(object):
and response. It allows for function authors to override the the default implementation, LambdaMarshaller which
unmarshals and marshals JSON, to an instance of a class that implements the same interface."""

def __init__(self, lambda_runtime_address):
def __init__(self, lambda_runtime_address, use_thread_for_polling_next=False):
self.lambda_runtime_address = lambda_runtime_address
self.use_thread_for_polling_next = use_thread_for_polling_next
if self.use_thread_for_polling_next:
from concurrent.futures import ThreadPoolExecutor

def post_init_error(self, error_response_data):
# These imports are heavy-weight. They implicitly trigger `import ssl, hashlib`.
Expand All @@ -67,20 +70,19 @@ def post_init_error(self, error_response_data):
if response.code != http.HTTPStatus.ACCEPTED:
raise LambdaRuntimeClientError(endpoint, response.code, response_body)

def wait_next_invocation(self, use_thread_for_polling_next=False):
def wait_next_invocation(self):
# Calling runtime_client.next() from a separate thread unblocks the main thread,
# which can then process signals.
if use_thread_for_polling_next:
from concurrent.futures import ThreadPoolExecutor
from .lambda_runtime_exception import FaultException

if self.use_thread_for_polling_next:
try:
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(runtime_client.next)
response_body, headers = future.result()
except Exception as e:
raise FaultException(
FaultException.LAMBDA_RUNTIME_CLIENT_ERROR,
from .lambda_runtime_exception import FaultExceptions

raise FaultExceptions(
Comment thread
pushkarchawda marked this conversation as resolved.
Outdated
FaultExceptions.LAMBDA_RUNTIME_CLIENT_ERROR,
"LAMBDA_RUNTIME Failed to get next invocation: {}".format(str(e)),
None,
)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_lambda_runtime_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ def test_wait_next_invocation(self, mock_runtime_client):
mock_runtime_client.next.return_value = response_body, headears
runtime_client = LambdaRuntimeClient("localhost:1234")

use_thread_for_polling_next = True
event_request = runtime_client.wait_next_invocation(use_thread_for_polling_next)
event_request = runtime_client.wait_next_invocation()

self.assertIsNotNone(event_request)
self.assertEqual(event_request.invoke_id, "RID1234")
Expand All @@ -86,8 +85,9 @@ def test_wait_next_invocation(self, mock_runtime_client):
self.assertEqual(event_request.event_body, response_body)

# Using ThreadPoolExecutor to polling next()
use_thread_for_polling_next = False
event_request = runtime_client.wait_next_invocation(use_thread_for_polling_next)
runtime_client = LambdaRuntimeClient("localhost:1234", True)

event_request = runtime_client.wait_next_invocation()

self.assertIsNotNone(event_request)
self.assertEqual(event_request.invoke_id, "RID1234")
Expand Down