-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: initial attempt at adding webtransport to grizzly
- Loading branch information
Showing
30 changed files
with
1,101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
ignore: | ||
- "grizzly/services/webtransport/wpt_h3_server" | ||
codecov: | ||
ci: | ||
- community-tc.services.mozilla.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
__all__ = ( | ||
"ServiceName", | ||
"WebServices", | ||
"WebTransportServer", | ||
) | ||
|
||
from .core import ServiceName, WebServices | ||
from .webtransport.core import WebTransportServer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class BaseService(ABC): | ||
"""Base service class""" | ||
|
||
@property | ||
@abstractmethod | ||
def location(self): | ||
"""Location to use with Sapphire.set_dynamic_response""" | ||
|
||
@property | ||
@abstractmethod | ||
def port(self): | ||
"""The port on which the server is listening""" | ||
|
||
@abstractmethod | ||
def url(self, _query): | ||
"""Returns the URL of the server.""" | ||
|
||
@abstractmethod | ||
async def is_ready(self): | ||
"""Wait until the service is ready""" | ||
|
||
@abstractmethod | ||
def cleanup(self): | ||
"""Stop the server.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
import asyncio | ||
from enum import Enum | ||
from logging import getLogger | ||
from typing import Dict | ||
|
||
from sapphire import create_listening_socket | ||
|
||
from .base import BaseService | ||
from .webtransport.core import WebTransportServer | ||
|
||
LOG = getLogger(__name__) | ||
|
||
|
||
class ServiceName(Enum): | ||
"""Enum for listing available services""" | ||
|
||
WEB_TRANSPORT = 1 | ||
|
||
|
||
class WebServices: | ||
"""Class for running additional web services""" | ||
|
||
def __init__(self, services: Dict[ServiceName, BaseService]): | ||
"""Initialize new WebServices instance | ||
Args: | ||
services (dict of ServiceName: BaseService): Collection of services. | ||
""" | ||
self.services = services | ||
|
||
@staticmethod | ||
def get_free_port(): | ||
"""Returns an open port""" | ||
sock = create_listening_socket() | ||
port = sock.getsockname()[1] | ||
sock.close() | ||
|
||
return port | ||
|
||
async def is_running(self, timeout=20): | ||
"""Polls all available services to ensure they are running and accessible. | ||
Args: | ||
timeout (int): Total time to wait. | ||
Returns: | ||
bool: Indicates if all services started successfully. | ||
""" | ||
tasks = {} | ||
for name, service in self.services.items(): | ||
task = asyncio.create_task(service.is_ready()) | ||
tasks[name] = task | ||
|
||
try: | ||
await asyncio.wait_for(asyncio.gather(*tasks.values()), timeout) | ||
except asyncio.TimeoutError: | ||
for name, task in tasks.items(): | ||
if not task.done(): | ||
LOG.warning("Failed to start service (%s)", ServiceName(name).name) | ||
return False | ||
|
||
return True | ||
|
||
def cleanup(self): | ||
"""Stops all running services and join's the service thread""" | ||
for service in self.services.values(): | ||
service.cleanup() | ||
|
||
def map_locations(self, server_map): | ||
"""Configure server map""" | ||
for service in self.services.values(): | ||
server_map.set_dynamic_response( | ||
service.location, service.url, mime_type="text/plain", required=False | ||
) | ||
|
||
@classmethod | ||
def start_services(cls, cert, key): | ||
"""Start all available services | ||
Args: | ||
cert (Path): Path to the certificate file | ||
key (Path): Path to the certificate's private key | ||
""" | ||
services = {} | ||
# Start WebTransport service | ||
wt_port = cls.get_free_port() | ||
services[ServiceName.WEB_TRANSPORT] = WebTransportServer(wt_port, cert, key) | ||
services[ServiceName.WEB_TRANSPORT].start() | ||
|
||
ext_services = cls(services) | ||
assert asyncio.run(ext_services.is_running()) | ||
|
||
return ext_services |
Oops, something went wrong.