Skip to content

Commit 203ff68

Browse files
committed
Make apply_http2_patches_for_grpc_support safe for potential re-use
1 parent 36a627b commit 203ff68

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

typedb/localstack_typedb/utils/h2_proxy.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def close(self):
5555
pass
5656

5757

58+
patch_counter = 1
59+
5860
def apply_http2_patches_for_grpc_support(
5961
target_host: str, target_port: int, request_matcher: ProxyRequestMatcher
6062
):
@@ -102,17 +104,28 @@ def received_from_http2_client(self, data, default_handler):
102104
def close(self):
103105
self.backend.close()
104106

107+
# In case multiple such patches are attached (which is likely inadvisable anyway),
108+
# we need the instance vars to not clash. A simple counter at least shows us
109+
# when we've done that.
110+
global patch_counter
111+
bufvar = f"_ls_buffer_{patch_counter}"
112+
patch_counter += 1
113+
def bufget(o) -> ForwardingBuffer:
114+
return getattr(o, bufvar)
115+
def bufset(o, v: ForwardingBuffer):
116+
return setattr(o, bufvar, v)
117+
105118
@patch(H2Connection.connectionMade)
106119
def _connectionMade(fn, self, *args, **kwargs):
107-
self._ls_forwarding_buffer = ForwardingBuffer(self.transport)
120+
bufset(self, ForwardingBuffer(self.transport))
108121

109122
@patch(H2Connection.dataReceived)
110123
def _dataReceived(fn, self, data, *args, **kwargs):
111-
self._ls_forwarding_buffer.received_from_http2_client(data, lambda d: fn(d, *args, **kwargs))
124+
bufget(self).received_from_http2_client(data, lambda d: fn(d, *args, **kwargs))
112125

113126
@patch(H2Connection.connectionLost)
114127
def connectionLost(fn, self, *args, **kwargs):
115-
self._ls_forwarding_buffer.close()
128+
bufget(self).close()
116129

117130

118131
def get_headers_from_data_stream(data_list: Iterable[bytes]) -> Headers:

0 commit comments

Comments
 (0)