Skip to content

Commit 552fb99

Browse files
authored
[Middleware] HttpClientConnection preparation (#995)
* Turn usual suspects to warnings, not error * Add `HttpClientConnection` skeleton * Fix doc build * Update references in http tests * Make `work` core agnostic to work object construction by adding an abstract static method to `Work` interface called `create` * Make mypy happy * Fix tests broken due to change in how work objects are now constructed * Doc ko bhi happy karo
1 parent c6fceb6 commit 552fb99

30 files changed

+144
-81
lines changed

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,14 @@
308308
(_py_class_role, 'unittest.result.TestResult'),
309309
(_py_class_role, 'UUID'),
310310
(_py_class_role, 'UpstreamConnectionPool'),
311+
(_py_class_role, 'HttpClientConnection'),
311312
(_py_class_role, 'Url'),
312313
(_py_class_role, 'WebsocketFrame'),
313314
(_py_class_role, 'Work'),
314315
(_py_class_role, 'proxy.core.acceptor.work.Work'),
315316
(_py_class_role, 'connection.Connection'),
316317
(_py_class_role, 'EventQueue'),
318+
(_py_class_role, 'T'),
317319
(_py_obj_role, 'proxy.core.work.threadless.T'),
318320
(_py_obj_role, 'proxy.core.work.work.T'),
319321
(_py_obj_role, 'proxy.core.base.tcp_server.T'),

examples/ssl_echo_server.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111
import time
12-
from typing import Optional
12+
from typing import Any, Optional
1313

1414
from proxy import Proxy
1515
from proxy.core.base import BaseTcpServerHandler
@@ -20,6 +20,10 @@
2020
class EchoSSLServerHandler(BaseTcpServerHandler[TcpClientConnection]):
2121
"""Wraps client socket during initialization."""
2222

23+
@staticmethod
24+
def create(**kwargs: Any) -> TcpClientConnection:
25+
return TcpClientConnection(**kwargs)
26+
2327
def initialize(self) -> None:
2428
# Acceptors don't perform TLS handshake. Perform the same
2529
# here using wrap_socket() utility.

examples/tcp_echo_server.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
:license: BSD, see LICENSE for more details.
1010
"""
1111
import time
12-
from typing import Optional
12+
from typing import Any, Optional
1313

1414
from proxy import Proxy
1515
from proxy.core.base import BaseTcpServerHandler
@@ -19,6 +19,10 @@
1919
class EchoServerHandler(BaseTcpServerHandler[TcpClientConnection]):
2020
"""Sets client socket to non-blocking during initialization."""
2121

22+
@staticmethod
23+
def create(**kwargs: Any) -> TcpClientConnection:
24+
return TcpClientConnection(**kwargs)
25+
2226
def initialize(self) -> None:
2327
self.work.connection.setblocking(False)
2428

proxy/core/acceptor/acceptor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ def _work(self, conn: socket.socket, addr: Optional[Tuple[str, int]]) -> None:
224224
),
225225
)
226226
thread.start()
227-
logger.debug(
227+
# TODO: Move me into target method
228+
logger.debug( # pragma: no cover
228229
'Dispatched work#{0}.{1}.{2} to worker#{3}'.format(
229230
conn.fileno(), self.idd, self._total, index,
230231
),
@@ -237,6 +238,7 @@ def _work(self, conn: socket.socket, addr: Optional[Tuple[str, int]]) -> None:
237238
event_queue=self.event_queue,
238239
publisher_id=self.__class__.__name__,
239240
)
241+
# TODO: Move me into target method
240242
logger.debug( # pragma: no cover
241243
'Started work#{0}.{1}.{2} in thread#{3}'.format(
242244
conn.fileno(), self.idd, self._total, thread.ident,

proxy/core/base/tcp_tunnel.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
4747
def handle_data(self, data: memoryview) -> Optional[bool]:
4848
pass # pragma: no cover
4949

50+
@staticmethod
51+
def create(**kwargs: Any) -> TcpClientConnection:
52+
return TcpClientConnection(**kwargs)
53+
5054
def initialize(self) -> None:
5155
self.work.connection.setblocking(False)
5256

proxy/core/connection/pool.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import logging
1717
import selectors
1818

19-
from typing import TYPE_CHECKING, Set, Dict, Tuple
19+
from typing import TYPE_CHECKING, Any, Set, Dict, Tuple
2020

2121
from ...common.flag import flags
2222
from ...common.types import Readables, SelectableEvents, Writables
@@ -77,6 +77,10 @@ def __init__(self) -> None:
7777
self.connections: Dict[int, TcpServerConnection] = {}
7878
self.pools: Dict[Tuple[str, int], Set[TcpServerConnection]] = {}
7979

80+
@staticmethod
81+
def create(**kwargs: Any) -> TcpServerConnection:
82+
return TcpServerConnection(**kwargs)
83+
8084
def acquire(self, addr: Tuple[str, int]) -> Tuple[bool, TcpServerConnection]:
8185
"""Returns a reusable connection from the pool.
8286
@@ -152,7 +156,7 @@ def add(self, addr: Tuple[str, int]) -> TcpServerConnection:
152156
153157
NOTE: You must not use the returned connection, instead use `acquire`.
154158
"""
155-
new_conn = TcpServerConnection(addr[0], addr[1])
159+
new_conn = self.create(host=addr[0], port=addr[1])
156160
new_conn.connect()
157161
self._add(new_conn)
158162
logger.debug(

proxy/core/ssh/handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from typing import TYPE_CHECKING, Tuple
1414

15-
if TYPE_CHECKING:
15+
if TYPE_CHECKING: # pragma: no cover
1616
try:
1717
from paramiko.channel import Channel
1818
except ImportError:

proxy/core/ssh/listener.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
try:
1717
from paramiko import SSHClient, AutoAddPolicy
1818
from paramiko.transport import Transport
19-
if TYPE_CHECKING:
19+
if TYPE_CHECKING: # pragma: no cover
2020
from paramiko.channel import Channel
2121
except ImportError:
2222
pass

proxy/core/work/threaded.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,27 @@
1212
import argparse
1313
import threading
1414

15-
from typing import TYPE_CHECKING, Optional, Tuple
15+
from typing import TYPE_CHECKING, Optional, Tuple, TypeVar
1616

17-
from ..connection import TcpClientConnection
1817
from ..event import EventQueue, eventNames
1918

2019
if TYPE_CHECKING: # pragma: no cover
2120
from .work import Work
2221

22+
T = TypeVar('T')
2323

24+
25+
# TODO: Add generic T
2426
def start_threaded_work(
2527
flags: argparse.Namespace,
2628
conn: socket.socket,
2729
addr: Optional[Tuple[str, int]],
2830
event_queue: Optional[EventQueue] = None,
2931
publisher_id: Optional[str] = None,
30-
) -> Tuple['Work[TcpClientConnection]', threading.Thread]:
32+
) -> Tuple['Work[T]', threading.Thread]:
3133
"""Utility method to start a work in a new thread."""
3234
work = flags.work_klass(
33-
TcpClientConnection(conn, addr),
35+
flags.work_klass.create(conn=conn, addr=addr),
3436
flags=flags,
3537
event_queue=event_queue,
3638
upstream_conn_pool=None,

proxy/core/work/threadless.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from ...common.constants import DEFAULT_INACTIVE_CONN_CLEANUP_TIMEOUT, DEFAULT_SELECTOR_SELECT_TIMEOUT
2626
from ...common.constants import DEFAULT_WAIT_FOR_TASKS_TIMEOUT
2727

28-
from ..connection import TcpClientConnection
2928
from ..event import eventNames
3029

3130
if TYPE_CHECKING: # pragma: no cover
@@ -138,7 +137,7 @@ def work_on_tcp_conn(
138137
)
139138
uid = '%s-%s-%s' % (self.iid, self._total, fileno)
140139
self.works[fileno] = self.flags.work_klass(
141-
TcpClientConnection(
140+
self.flags.work_klass.create(
142141
conn=conn,
143142
addr=addr,
144143
),

0 commit comments

Comments
 (0)