diff options
| author | Joshua Watt <JPEWhacker@gmail.com> | 2023-11-03 08:26:20 -0600 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2023-11-09 17:33:02 +0000 |
| commit | 2484bd893189bc24ac210edf2de3b7e1115eebd3 (patch) | |
| tree | be9433e53a5bb645816f5b78730830a73bc6ee10 /bitbake/lib/hashserv | |
| parent | 8f8501ed403dec27acbe780b936bc087fc5006d0 (diff) | |
| download | poky-2484bd893189bc24ac210edf2de3b7e1115eebd3.tar.gz | |
bitbake: hashserv: Add websocket connection implementation
Adds support to the hash equivalence client and server to communicate
over websockets. Since websockets are message orientated instead of
stream orientated, and new connection class is needed to handle them.
Note that websocket support does require the 3rd party websockets python
module be installed on the host, but it should not be required unless
websockets are actually being used.
(Bitbake rev: 56dd2fdbfb6350a9eef43a12aa529c8637887a7e)
Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/hashserv')
| -rw-r--r-- | bitbake/lib/hashserv/__init__.py | 13 | ||||
| -rw-r--r-- | bitbake/lib/hashserv/client.py | 1 | ||||
| -rw-r--r-- | bitbake/lib/hashserv/tests.py | 17 |
3 files changed, 31 insertions, 0 deletions
diff --git a/bitbake/lib/hashserv/__init__.py b/bitbake/lib/hashserv/__init__.py index 3a4018353f..56b9c6bc82 100644 --- a/bitbake/lib/hashserv/__init__.py +++ b/bitbake/lib/hashserv/__init__.py | |||
| @@ -9,11 +9,15 @@ import re | |||
| 9 | import sqlite3 | 9 | import sqlite3 |
| 10 | import itertools | 10 | import itertools |
| 11 | import json | 11 | import json |
| 12 | from urllib.parse import urlparse | ||
| 12 | 13 | ||
| 13 | UNIX_PREFIX = "unix://" | 14 | UNIX_PREFIX = "unix://" |
| 15 | WS_PREFIX = "ws://" | ||
| 16 | WSS_PREFIX = "wss://" | ||
| 14 | 17 | ||
| 15 | ADDR_TYPE_UNIX = 0 | 18 | ADDR_TYPE_UNIX = 0 |
| 16 | ADDR_TYPE_TCP = 1 | 19 | ADDR_TYPE_TCP = 1 |
| 20 | ADDR_TYPE_WS = 2 | ||
| 17 | 21 | ||
| 18 | UNIHASH_TABLE_DEFINITION = ( | 22 | UNIHASH_TABLE_DEFINITION = ( |
| 19 | ("method", "TEXT NOT NULL", "UNIQUE"), | 23 | ("method", "TEXT NOT NULL", "UNIQUE"), |
| @@ -84,6 +88,8 @@ def setup_database(database, sync=True): | |||
| 84 | def parse_address(addr): | 88 | def parse_address(addr): |
| 85 | if addr.startswith(UNIX_PREFIX): | 89 | if addr.startswith(UNIX_PREFIX): |
| 86 | return (ADDR_TYPE_UNIX, (addr[len(UNIX_PREFIX):],)) | 90 | return (ADDR_TYPE_UNIX, (addr[len(UNIX_PREFIX):],)) |
| 91 | elif addr.startswith(WS_PREFIX) or addr.startswith(WSS_PREFIX): | ||
| 92 | return (ADDR_TYPE_WS, (addr,)) | ||
| 87 | else: | 93 | else: |
| 88 | m = re.match(r'\[(?P<host>[^\]]*)\]:(?P<port>\d+)$', addr) | 94 | m = re.match(r'\[(?P<host>[^\]]*)\]:(?P<port>\d+)$', addr) |
| 89 | if m is not None: | 95 | if m is not None: |
| @@ -103,6 +109,9 @@ def create_server(addr, dbname, *, sync=True, upstream=None, read_only=False): | |||
| 103 | (typ, a) = parse_address(addr) | 109 | (typ, a) = parse_address(addr) |
| 104 | if typ == ADDR_TYPE_UNIX: | 110 | if typ == ADDR_TYPE_UNIX: |
| 105 | s.start_unix_server(*a) | 111 | s.start_unix_server(*a) |
| 112 | elif typ == ADDR_TYPE_WS: | ||
| 113 | url = urlparse(a[0]) | ||
| 114 | s.start_websocket_server(url.hostname, url.port) | ||
| 106 | else: | 115 | else: |
| 107 | s.start_tcp_server(*a) | 116 | s.start_tcp_server(*a) |
| 108 | 117 | ||
| @@ -116,6 +125,8 @@ def create_client(addr): | |||
| 116 | (typ, a) = parse_address(addr) | 125 | (typ, a) = parse_address(addr) |
| 117 | if typ == ADDR_TYPE_UNIX: | 126 | if typ == ADDR_TYPE_UNIX: |
| 118 | c.connect_unix(*a) | 127 | c.connect_unix(*a) |
| 128 | elif typ == ADDR_TYPE_WS: | ||
| 129 | c.connect_websocket(*a) | ||
| 119 | else: | 130 | else: |
| 120 | c.connect_tcp(*a) | 131 | c.connect_tcp(*a) |
| 121 | 132 | ||
| @@ -128,6 +139,8 @@ async def create_async_client(addr): | |||
| 128 | (typ, a) = parse_address(addr) | 139 | (typ, a) = parse_address(addr) |
| 129 | if typ == ADDR_TYPE_UNIX: | 140 | if typ == ADDR_TYPE_UNIX: |
| 130 | await c.connect_unix(*a) | 141 | await c.connect_unix(*a) |
| 142 | elif typ == ADDR_TYPE_WS: | ||
| 143 | await c.connect_websocket(*a) | ||
| 131 | else: | 144 | else: |
| 132 | await c.connect_tcp(*a) | 145 | await c.connect_tcp(*a) |
| 133 | 146 | ||
diff --git a/bitbake/lib/hashserv/client.py b/bitbake/lib/hashserv/client.py index 5f7d22ab13..9542d72f6c 100644 --- a/bitbake/lib/hashserv/client.py +++ b/bitbake/lib/hashserv/client.py | |||
| @@ -115,6 +115,7 @@ class Client(bb.asyncrpc.Client): | |||
| 115 | super().__init__() | 115 | super().__init__() |
| 116 | self._add_methods( | 116 | self._add_methods( |
| 117 | "connect_tcp", | 117 | "connect_tcp", |
| 118 | "connect_websocket", | ||
| 118 | "get_unihash", | 119 | "get_unihash", |
| 119 | "report_unihash", | 120 | "report_unihash", |
| 120 | "report_unihash_equiv", | 121 | "report_unihash_equiv", |
diff --git a/bitbake/lib/hashserv/tests.py b/bitbake/lib/hashserv/tests.py index f343c586b5..01ffd52c1d 100644 --- a/bitbake/lib/hashserv/tests.py +++ b/bitbake/lib/hashserv/tests.py | |||
| @@ -483,3 +483,20 @@ class TestHashEquivalenceTCPServer(HashEquivalenceTestSetup, HashEquivalenceComm | |||
| 483 | # If IPv6 is enabled, it should be safe to use localhost directly, in general | 483 | # If IPv6 is enabled, it should be safe to use localhost directly, in general |
| 484 | # case it is more reliable to resolve the IP address explicitly. | 484 | # case it is more reliable to resolve the IP address explicitly. |
| 485 | return socket.gethostbyname("localhost") + ":0" | 485 | return socket.gethostbyname("localhost") + ":0" |
| 486 | |||
| 487 | |||
| 488 | class TestHashEquivalenceWebsocketServer(HashEquivalenceTestSetup, HashEquivalenceCommonTests, unittest.TestCase): | ||
| 489 | def setUp(self): | ||
| 490 | try: | ||
| 491 | import websockets | ||
| 492 | except ImportError as e: | ||
| 493 | self.skipTest(str(e)) | ||
| 494 | |||
| 495 | super().setUp() | ||
| 496 | |||
| 497 | def get_server_addr(self, server_idx): | ||
| 498 | # Some hosts cause asyncio module to misbehave, when IPv6 is not enabled. | ||
| 499 | # If IPv6 is enabled, it should be safe to use localhost directly, in general | ||
| 500 | # case it is more reliable to resolve the IP address explicitly. | ||
| 501 | host = socket.gethostbyname("localhost") | ||
| 502 | return "ws://%s:0" % host | ||
