Skip to content

Commit

Permalink
Merge pull request #32 from Aksem/feature/web_aiohttp
Browse files Browse the repository at this point in the history
Update deps. Get free port in web aiohttp transport if no port is pas…
  • Loading branch information
Aksem authored Nov 1, 2024
2 parents 0577350 + bc3a4a8 commit 638af19
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 409 deletions.
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.11
Empty file.
10 changes: 10 additions & 0 deletions modapp/transports/utils/free_port.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import socket
from contextlib import closing


def get_free_port():
# find free port
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(("localhost", 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
free_port = s.getsockname()[1]
8 changes: 5 additions & 3 deletions modapp/transports/web_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from modapp.routing import Cardinality, Route

from .web_aiohttp_config import DEFAULT_CONFIG, WebAiohttpTransportConfig
from .utils.free_port import get_free_port

if TYPE_CHECKING:
from modapp.routing import RoutesDict
Expand Down Expand Up @@ -214,8 +215,8 @@ async def start(self, routes: RoutesDict) -> None:
self.app.add_routes(app_routes)

port = self.config.get("port", DEFAULT_CONFIG["port"])
if port is None:
port = 0
if port is None or port == 0:
port = get_free_port()
assert isinstance(port, int), "Int expected to be an int"

for static_dir_route, static_dir_path in self._static_dirs.items():
Expand All @@ -242,7 +243,8 @@ async def start(self, routes: RoutesDict) -> None:
site = web.TCPSite(self._runner, "127.0.0.1", port)
await site.start()
self.port = port
logger.info(f"Start web aiohttp server: localhost:{port}")
logger.info(f"Start server: 127.0.0.1:{port}")
logger.trace(f"Start web aiohttp server: 127.0.0.1:{port}")

@override
def stop(self) -> None:
Expand Down
823 changes: 420 additions & 403 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions poetry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[virtualenvs]
prefer-active-python = true
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "modapp"
version = "0.3.22"
version = "0.3.23"
description = ""
authors = ["Vladyslav Hnatiuk <[email protected]>"]
license = "MIT"
Expand All @@ -14,8 +14,7 @@ uvloop = { version = "^0.19.0", markers = "sys_platform != 'win32'", optional =
winloop = { version = "^0.0.8", markers = "sys_platform == 'win32'", optional = true }
grpclib = { version = "^0.4.7", optional = true }
googleapis-common-protos = { version = "^1.63.2", optional = true }
# fix socketify to 0.0.27, because 0.0.28 has critical issue: https://github.com/cirospaciari/socketify.py/issues/194
socketify = { version = "0.0.27", optional = true }
socketify = { version = "^0.0.31", optional = true }
orjson = { version = "^3.10.3", optional = true }
aiohttp = { extras = ["speedups"], version = "^3.9.5", optional = true }
anyio = {version = "^4.4.0", optional = true}
Expand Down

0 comments on commit 638af19

Please sign in to comment.