Skip to content

Add config option to control maximum gateway worker thread count #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: base-sha/17fa5ee48109ff6c245bd30d18e9910d3e81e3b1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions localstack/aws/serving/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ def __init__(
self,
gateway: Gateway,
event_loop: Optional[AbstractEventLoop] = None,
threads: int = 1000,
threads: int = None,
lifespan_listener: Optional[ASGILifespanListener] = None,
websocket_listener=None,
) -> None:
self.gateway = gateway

self.event_loop = event_loop or asyncio.get_event_loop()
self.executor = _ThreadPool(threads, thread_name_prefix="asgi_gw")
self.executor = _ThreadPool(threads or 1000, thread_name_prefix="asgi_gw")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery logo suggestion (code_refinement): Use of or for default parameter handling is clear but consider centralizing default values.

While the use of or for providing a default value is straightforward, centralizing default values (e.g., as constants) can make future adjustments and readability better.

Suggested change
self.executor = _ThreadPool(threads or 1000, thread_name_prefix="asgi_gw")
DEFAULT_THREAD_COUNT = 1000 # Centralized default value for easier adjustments and readability
self.executor = _ThreadPool(threads or DEFAULT_THREAD_COUNT, thread_name_prefix="asgi_gw")

self.adapter = ASGIAdapter(
WsgiGateway(gateway),
event_loop=event_loop,
Expand Down
3 changes: 2 additions & 1 deletion localstack/aws/serving/edge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from typing import List

from localstack import config
from localstack.config import HostAndPort
from localstack.http.hypercorn import GatewayServer
from localstack.runtime.shutdown import ON_AFTER_SERVICE_SHUTDOWN_HANDLERS
Expand All @@ -21,7 +22,7 @@ def serve_gateway(
gateway = LocalstackAwsGateway(SERVICE_PLUGINS)

# start serving gateway
server = GatewayServer(gateway, listen, use_ssl)
server = GatewayServer(gateway, listen, use_ssl, config.GATEWAY_WORKER_COUNT)
server.start()

# with the current way the infrastructure is started, this is the easiest way to shut down the server correctly
Expand Down
3 changes: 3 additions & 0 deletions localstack/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,8 @@ def populate_edge_configuration(
GATEWAY_LISTEN,
) = populate_edge_configuration(os.environ)

GATEWAY_WORKER_COUNT = int(os.environ.get("GATEWAY_WORKER_COUNT") or 1000)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery logo suggestion (code_refinement): Ensure environment variable fallback values are consistent across the codebase.

The fallback of 1000 for GATEWAY_WORKER_COUNT is used directly in multiple places. Consider defining this as a constant to ensure consistency and ease of maintenance.

Suggested change
GATEWAY_WORKER_COUNT = int(os.environ.get("GATEWAY_WORKER_COUNT") or 1000)
# Define a constant for the default gateway worker count
DEFAULT_GATEWAY_WORKER_COUNT = 1000
GATEWAY_WORKER_COUNT = int(os.environ.get("GATEWAY_WORKER_COUNT", DEFAULT_GATEWAY_WORKER_COUNT))


# IP of the docker bridge used to enable access between containers
DOCKER_BRIDGE_IP = os.environ.get("DOCKER_BRIDGE_IP", "").strip()

Expand Down Expand Up @@ -1114,6 +1116,7 @@ def use_custom_dns():
"EXTRA_CORS_ALLOWED_ORIGINS",
"EXTRA_CORS_EXPOSE_HEADERS",
"GATEWAY_LISTEN",
"GATEWAY_WORKER_THREAD_COUNT",
"HOSTNAME",
"HOSTNAME_FROM_LAMBDA",
"KINESIS_ERROR_PROBABILITY",
Expand Down
12 changes: 8 additions & 4 deletions localstack/http/hypercorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,19 @@ class GatewayServer(HypercornServer):
"""

def __init__(
self, gateway: Gateway, listen: HostAndPort | list[HostAndPort], use_ssl: bool = False
self,
gateway: Gateway,
listen: HostAndPort | list[HostAndPort],
use_ssl: bool = False,
threads: int | None = None,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery logo suggestion (performance): Consider providing a default value for threads that aligns with system capabilities.

Using None as a default for threads and later substituting it with a hardcoded value (1000) might not be optimal for all environments. Consider a default that scales with the number of available CPU cores.

Suggested change
threads: int | None = None,
threads: int = os.cpu_count() or 4,

):
"""
Creates a new GatewayServer instance.

:param gateway: which will be served by this server
:param port: defining the port of this server instance
:param bind_address: to bind this server instance to. Can be a host string or a list of host strings.
:param listen: defining the address and port pairs this server binds to. Can be a list of host and port pairs.
:param use_ssl: True if the LocalStack cert should be loaded and HTTP/HTTPS multiplexing should be enabled.
:param threads: Number of worker threads the gateway will use.
"""
# build server config
config = Config()
Expand All @@ -109,7 +113,7 @@ def __init__(

# build gateway
loop = asyncio.new_event_loop()
app = AsgiGateway(gateway, event_loop=loop)
app = AsgiGateway(gateway, event_loop=loop, threads=threads)

# start serving gateway
super().__init__(app, config, loop)
Expand Down