pygrpc-native 0.5.2__tar.gz → 0.6.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/CHANGELOG.md +17 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/Cargo.lock +1 -1
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/Cargo.toml +1 -1
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/PKG-INFO +11 -5
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/README.md +10 -4
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/python/pygrpc_native/_native.pyi +13 -3
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/python/pygrpc_native/_server.py +226 -22
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/python/pygrpc_native/aio.py +112 -15
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/src/lib.rs +58 -4
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/src/server.rs +201 -30
- pygrpc_native-0.6.0/tests/test_concurrency_limit.py +471 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_fallback.py +0 -24
- pygrpc_native-0.6.0/tests/test_generic_methods.py +169 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/.github/CODEOWNERS +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/.github/ISSUE_TEMPLATE/config.yml +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/.github/dependabot.yml +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/.github/workflows/ci.yml +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/.github/workflows/dco.yml +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/.github/workflows/release.yml +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/.github/workflows/stale.yml +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/.gitignore +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/.pre-commit-config.yaml +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/CODE_OF_CONDUCT.md +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/CONTRIBUTING.md +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/DCO +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/LICENSE +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/SECURITY.md +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/benchmarks/README.md +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/benchmarks/echo.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/docs/low-level-api.md +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/pyproject.toml +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/python/pygrpc_native/__init__.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/python/pygrpc_native/py.typed +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/src/metrics.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_add_methods.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_aio.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_api.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_bearer_auth.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_call_details.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_calls.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_connection.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_delay_stream_headers.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_downstream.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_front.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_interceptors.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_metrics.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_response_metadata.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_server.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/tests/test_stream_try_send.py +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/CHANGELOG.md +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/Cargo.toml +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/LICENSE-APACHE +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/LICENSE-MIT +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/README.md +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/byte_str.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/convert.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/error.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/extensions.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/header/map.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/header/mod.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/header/name.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/header/value.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/lib.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/method.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/request.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/response.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/status.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/uri/authority.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/uri/builder.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/uri/mod.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/uri/path.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/uri/port.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/uri/scheme.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/uri/tests.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/src/version.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/tests/header_map.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/tests/header_map_fuzz.rs +0 -0
- {pygrpc_native-0.5.2 → pygrpc_native-0.6.0}/vendor/http/tests/status_code.rs +0 -0
|
@@ -4,6 +4,23 @@ All notable changes are listed here. The project follows
|
|
|
4
4
|
[semantic versioning](https://semver.org); see
|
|
5
5
|
[Compatibility](README.md#compatibility) for what that covers.
|
|
6
6
|
|
|
7
|
+
## 0.6.0 (2026-10-29)
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- `server()` and `aio.server()` take `maximum_concurrent_rpcs`. Calls over the
|
|
12
|
+
limit get `RESOURCE_EXHAUSTED`, as with grpcio, and the methods on the
|
|
13
|
+
fallback server count against the same limit. (#73, #75)
|
|
14
|
+
- Unary and server-streaming methods added only with `add_generic_rpc_handlers`,
|
|
15
|
+
as in code generated by grpcio-tools before 1.64, are served natively instead
|
|
16
|
+
of by the fallback server. (#77)
|
|
17
|
+
|
|
18
|
+
### Fixed
|
|
19
|
+
|
|
20
|
+
- `stop()` with a grace period could drop the response of a call that had just
|
|
21
|
+
finished, and the client got `UNAVAILABLE`. Closing now waits briefly for
|
|
22
|
+
calls in flight to finish sending. (#81)
|
|
23
|
+
|
|
7
24
|
## 0.5.2 (2026-10-28)
|
|
8
25
|
|
|
9
26
|
### Fixed
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pygrpc-native
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Classifier: Programming Language :: Python :: 3
|
|
5
5
|
Classifier: Programming Language :: Rust
|
|
6
6
|
Classifier: Operating System :: POSIX :: Linux
|
|
@@ -119,8 +119,7 @@ this and run your handlers the way grpcio does.
|
|
|
119
119
|
|:--|:-:|:-:|
|
|
120
120
|
| Unary and server-streaming methods | ✓ | |
|
|
121
121
|
| Client-streaming and bidirectional methods | | ✓ |
|
|
122
|
-
| Code generated by grpcio-tools
|
|
123
|
-
| Code generated by older grpcio-tools, or anything else added only with `add_generic_rpc_handlers` | | ✓ |
|
|
122
|
+
| Code generated by any grpcio-tools, and handlers added with `add_generic_rpc_handlers` | ✓ | |
|
|
124
123
|
| Server reflection | | ✓ |
|
|
125
124
|
| Interceptors | ✓ | ✓ |
|
|
126
125
|
|
|
@@ -135,8 +134,8 @@ the socket. The native server listens on a single insecure port.
|
|
|
135
134
|
|
|
136
135
|
| | Options |
|
|
137
136
|
|:--|:--|
|
|
138
|
-
| Same as grpcio | `grpc.max_receive_message_length`, `grpc.max_send_message_length`, `grpc.keepalive_time_ms`, `grpc.keepalive_timeout_ms`, `grpc.keepalive_permit_without_calls`, `grpc.max_concurrent_streams`, `grpc.max_metadata_size`, `grpc.absolute_max_metadata_size`, `grpc.so_reuseport` |
|
|
139
|
-
| Raise an error | `grpc.max_connection_age_ms`, `grpc.max_connection_age_grace_ms`, `grpc.max_connection_idle_ms`, `grpc.max_allowed_incoming_connections`, response compression,
|
|
137
|
+
| Same as grpcio | `grpc.max_receive_message_length`, `grpc.max_send_message_length`, `grpc.keepalive_time_ms`, `grpc.keepalive_timeout_ms`, `grpc.keepalive_permit_without_calls`, `grpc.max_concurrent_streams`, `grpc.max_metadata_size`, `grpc.absolute_max_metadata_size`, `grpc.so_reuseport`, `maximum_concurrent_rpcs` |
|
|
138
|
+
| Raise an error | `grpc.max_connection_age_ms`, `grpc.max_connection_age_grace_ms`, `grpc.max_connection_idle_ms`, `grpc.max_allowed_incoming_connections`, response compression, TLS |
|
|
140
139
|
| Ignored | Everything else. grpcio also ignores the many options that only clients read. This includes the limits on client pings, which are accepted but not enforced, and `grpc.http2.max_frame_size`, which grpc-core's flow control overrides anyway. |
|
|
141
140
|
|
|
142
141
|
As in grpcio, a value that isn't an integer is ignored.
|
|
@@ -234,6 +233,13 @@ has all the numbers and explains how they were measured.
|
|
|
234
233
|
- Each connection allows up to 1024 concurrent streams unless
|
|
235
234
|
`grpc.max_concurrent_streams` says otherwise. A connection that hasn't
|
|
236
235
|
finished the HTTP/2 preface within 120 s is dropped.
|
|
236
|
+
- `maximum_concurrent_rpcs` counts a call to a method on the fallback server
|
|
237
|
+
only while its handler runs, while grpcio also counts calls waiting for a
|
|
238
|
+
worker. With `server()`, such a call over the limit is refused when a worker
|
|
239
|
+
picks it up rather than as it arrives.
|
|
240
|
+
- A method that only generic handlers know is looked up when it is first
|
|
241
|
+
called, so their `service()` runs once more for that call. A method none of
|
|
242
|
+
them knows is looked up on each call before it goes to the fallback server.
|
|
237
243
|
- A call whose request metadata is over the limit (16 KiB unless set) is
|
|
238
244
|
refused. Between `grpc.max_metadata_size` and that limit, grpcio also
|
|
239
245
|
refuses some calls at random. This server doesn't.
|
|
@@ -105,8 +105,7 @@ this and run your handlers the way grpcio does.
|
|
|
105
105
|
|:--|:-:|:-:|
|
|
106
106
|
| Unary and server-streaming methods | ✓ | |
|
|
107
107
|
| Client-streaming and bidirectional methods | | ✓ |
|
|
108
|
-
| Code generated by grpcio-tools
|
|
109
|
-
| Code generated by older grpcio-tools, or anything else added only with `add_generic_rpc_handlers` | | ✓ |
|
|
108
|
+
| Code generated by any grpcio-tools, and handlers added with `add_generic_rpc_handlers` | ✓ | |
|
|
110
109
|
| Server reflection | | ✓ |
|
|
111
110
|
| Interceptors | ✓ | ✓ |
|
|
112
111
|
|
|
@@ -121,8 +120,8 @@ the socket. The native server listens on a single insecure port.
|
|
|
121
120
|
|
|
122
121
|
| | Options |
|
|
123
122
|
|:--|:--|
|
|
124
|
-
| Same as grpcio | `grpc.max_receive_message_length`, `grpc.max_send_message_length`, `grpc.keepalive_time_ms`, `grpc.keepalive_timeout_ms`, `grpc.keepalive_permit_without_calls`, `grpc.max_concurrent_streams`, `grpc.max_metadata_size`, `grpc.absolute_max_metadata_size`, `grpc.so_reuseport` |
|
|
125
|
-
| Raise an error | `grpc.max_connection_age_ms`, `grpc.max_connection_age_grace_ms`, `grpc.max_connection_idle_ms`, `grpc.max_allowed_incoming_connections`, response compression,
|
|
123
|
+
| Same as grpcio | `grpc.max_receive_message_length`, `grpc.max_send_message_length`, `grpc.keepalive_time_ms`, `grpc.keepalive_timeout_ms`, `grpc.keepalive_permit_without_calls`, `grpc.max_concurrent_streams`, `grpc.max_metadata_size`, `grpc.absolute_max_metadata_size`, `grpc.so_reuseport`, `maximum_concurrent_rpcs` |
|
|
124
|
+
| Raise an error | `grpc.max_connection_age_ms`, `grpc.max_connection_age_grace_ms`, `grpc.max_connection_idle_ms`, `grpc.max_allowed_incoming_connections`, response compression, TLS |
|
|
126
125
|
| Ignored | Everything else. grpcio also ignores the many options that only clients read. This includes the limits on client pings, which are accepted but not enforced, and `grpc.http2.max_frame_size`, which grpc-core's flow control overrides anyway. |
|
|
127
126
|
|
|
128
127
|
As in grpcio, a value that isn't an integer is ignored.
|
|
@@ -220,6 +219,13 @@ has all the numbers and explains how they were measured.
|
|
|
220
219
|
- Each connection allows up to 1024 concurrent streams unless
|
|
221
220
|
`grpc.max_concurrent_streams` says otherwise. A connection that hasn't
|
|
222
221
|
finished the HTTP/2 preface within 120 s is dropped.
|
|
222
|
+
- `maximum_concurrent_rpcs` counts a call to a method on the fallback server
|
|
223
|
+
only while its handler runs, while grpcio also counts calls waiting for a
|
|
224
|
+
worker. With `server()`, such a call over the limit is refused when a worker
|
|
225
|
+
picks it up rather than as it arrives.
|
|
226
|
+
- A method that only generic handlers know is looked up when it is first
|
|
227
|
+
called, so their `service()` runs once more for that call. A method none of
|
|
228
|
+
them knows is looked up on each call before it goes to the fallback server.
|
|
223
229
|
- A call whose request metadata is over the limit (16 KiB unless set) is
|
|
224
230
|
refused. Between `grpc.max_metadata_size` and that limit, grpcio also
|
|
225
231
|
refuses some calls at random. This server doesn't.
|
|
@@ -6,18 +6,20 @@ Metadata = Sequence[tuple[str, str | bytes]]
|
|
|
6
6
|
__version__: str
|
|
7
7
|
EVENT_CALL: int
|
|
8
8
|
EVENT_CANCEL: int
|
|
9
|
+
_EVENT_RESOLVE: int
|
|
10
|
+
_MAX_RESOLVED: int
|
|
9
11
|
STREAM_SENT: int
|
|
10
12
|
STREAM_FULL: int
|
|
11
13
|
STREAM_CLOSED: int
|
|
12
14
|
|
|
13
15
|
# (kind, call_id, method_id, payload, client_key); payload and client_key are
|
|
14
|
-
# None for EVENT_CANCEL.
|
|
15
|
-
Event = tuple[int, int, int, bytes | None, bytes | str | None]
|
|
16
|
+
# None for EVENT_CANCEL, and payload is the method path for _EVENT_RESOLVE.
|
|
17
|
+
Event = tuple[int, int, int, bytes | str | None, bytes | str | None]
|
|
16
18
|
|
|
17
19
|
# (metadata, peer, time_remaining); metadata values are bytes for -bin keys.
|
|
18
20
|
CallDetails = tuple[tuple[tuple[str, str | bytes], ...], str, float | None]
|
|
19
21
|
DetailedEvent = tuple[
|
|
20
|
-
int, int, int, bytes | None, bytes | str | None, CallDetails | None
|
|
22
|
+
int, int, int, bytes | str | None, bytes | str | None, CallDetails | None
|
|
21
23
|
]
|
|
22
24
|
|
|
23
25
|
class NativeGrpcFront:
|
|
@@ -43,6 +45,14 @@ class NativeGrpcFront:
|
|
|
43
45
|
def bound_port(self) -> int: ...
|
|
44
46
|
def add_methods(self, methods: Sequence[tuple[str, bool]]) -> list[int]: ...
|
|
45
47
|
def set_fallback(self, path: str) -> None: ...
|
|
48
|
+
def _resolve_unknown_methods(self) -> None: ...
|
|
49
|
+
def _resolved(
|
|
50
|
+
self,
|
|
51
|
+
resolve_id: int,
|
|
52
|
+
method_id: int | None,
|
|
53
|
+
server_streaming: bool = False,
|
|
54
|
+
remember: bool = True,
|
|
55
|
+
) -> bool: ...
|
|
46
56
|
def start(self) -> None: ...
|
|
47
57
|
def recv_batch(
|
|
48
58
|
self, max_events: int, timeout_ms: int | None = None
|
|
@@ -13,7 +13,13 @@ from typing import Any
|
|
|
13
13
|
|
|
14
14
|
import grpc
|
|
15
15
|
|
|
16
|
-
from pygrpc_native._native import
|
|
16
|
+
from pygrpc_native._native import (
|
|
17
|
+
EVENT_CALL,
|
|
18
|
+
EVENT_CANCEL,
|
|
19
|
+
_EVENT_RESOLVE,
|
|
20
|
+
_MAX_RESOLVED,
|
|
21
|
+
NativeGrpcFront,
|
|
22
|
+
)
|
|
17
23
|
|
|
18
24
|
_LOGGER = logging.getLogger(__name__)
|
|
19
25
|
|
|
@@ -172,16 +178,167 @@ def _use_handler(
|
|
|
172
178
|
return True
|
|
173
179
|
|
|
174
180
|
|
|
181
|
+
class _Methods:
|
|
182
|
+
"""The methods a server serves natively: `_table` holds
|
|
183
|
+
`(path, registered handler, server streaming)` by method id, with None for
|
|
184
|
+
a method only generic handlers know, which is looked up on every call."""
|
|
185
|
+
|
|
186
|
+
_front: NativeGrpcFront | None
|
|
187
|
+
_registered: dict[str, grpc.RpcMethodHandler]
|
|
188
|
+
_generic: list[grpc.GenericRpcHandler]
|
|
189
|
+
_table: list[tuple[str, grpc.RpcMethodHandler | None, bool]]
|
|
190
|
+
# Paths answered for good: their method id, or None for the fallback.
|
|
191
|
+
_resolved: dict[str, int | None]
|
|
192
|
+
|
|
193
|
+
def _lookup(self, details: grpc.HandlerCallDetails) -> grpc.RpcMethodHandler | None:
|
|
194
|
+
for generic in self._generic:
|
|
195
|
+
handler = generic.service(details)
|
|
196
|
+
if handler is not None:
|
|
197
|
+
return handler
|
|
198
|
+
return None
|
|
199
|
+
|
|
200
|
+
def _resolve(self, resolve_id: int, path: str, metadata: Any) -> None:
|
|
201
|
+
"""Answers the front's first call to a method it doesn't know."""
|
|
202
|
+
assert self._front is not None
|
|
203
|
+
remember = True
|
|
204
|
+
if path in self._resolved:
|
|
205
|
+
method_id = self._resolved[path]
|
|
206
|
+
else:
|
|
207
|
+
method_id = None
|
|
208
|
+
# Registered client-streaming methods come first, as in grpcio.
|
|
209
|
+
if path not in self._registered:
|
|
210
|
+
details = _HandlerCallDetails(
|
|
211
|
+
path, tuple(_Metadatum(key, value) for key, value in metadata)
|
|
212
|
+
)
|
|
213
|
+
try:
|
|
214
|
+
handler = contextvars.Context().run(self._lookup, details)
|
|
215
|
+
except Exception:
|
|
216
|
+
# The fallback server reports it.
|
|
217
|
+
handler = None
|
|
218
|
+
if handler is None:
|
|
219
|
+
remember = False
|
|
220
|
+
elif not handler.request_streaming:
|
|
221
|
+
method_id = len(self._table)
|
|
222
|
+
if len(self._resolved) >= _MAX_RESOLVED:
|
|
223
|
+
method_id, remember = None, False
|
|
224
|
+
if remember:
|
|
225
|
+
self._resolved[path] = method_id
|
|
226
|
+
if method_id is not None:
|
|
227
|
+
self._table.append((path, None, handler.response_streaming))
|
|
228
|
+
self._front._resolved(
|
|
229
|
+
resolve_id,
|
|
230
|
+
method_id,
|
|
231
|
+
method_id is not None and self._table[method_id][2],
|
|
232
|
+
remember,
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
|
|
175
236
|
class _Abort(Exception):
|
|
176
237
|
pass
|
|
177
238
|
|
|
178
239
|
|
|
240
|
+
class _ConcurrencyLimit:
|
|
241
|
+
def __init__(self, maximum: int | None) -> None:
|
|
242
|
+
self.maximum = maximum
|
|
243
|
+
self._active = 0
|
|
244
|
+
self._lock = threading.Lock()
|
|
245
|
+
|
|
246
|
+
def admit(self) -> bool:
|
|
247
|
+
if self.maximum is None:
|
|
248
|
+
return True
|
|
249
|
+
with self._lock:
|
|
250
|
+
if self._active >= self.maximum:
|
|
251
|
+
return False
|
|
252
|
+
self._active += 1
|
|
253
|
+
return True
|
|
254
|
+
|
|
255
|
+
def release(self) -> None:
|
|
256
|
+
if self.maximum is None:
|
|
257
|
+
return
|
|
258
|
+
with self._lock:
|
|
259
|
+
self._active -= 1
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
_LIMIT_EXCEEDED = "Concurrent RPC limit exceeded!"
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def _behavior(handler: grpc.RpcMethodHandler) -> Callable[..., Any]:
|
|
266
|
+
kind = (
|
|
267
|
+
f"{'stream' if handler.request_streaming else 'unary'}_"
|
|
268
|
+
f"{'stream' if handler.response_streaming else 'unary'}"
|
|
269
|
+
)
|
|
270
|
+
return getattr(handler, kind)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def _with_behavior(
|
|
274
|
+
handler: grpc.RpcMethodHandler, behavior: Callable[..., Any]
|
|
275
|
+
) -> grpc.RpcMethodHandler:
|
|
276
|
+
make = {
|
|
277
|
+
(False, False): grpc.unary_unary_rpc_method_handler,
|
|
278
|
+
(False, True): grpc.unary_stream_rpc_method_handler,
|
|
279
|
+
(True, False): grpc.stream_unary_rpc_method_handler,
|
|
280
|
+
(True, True): grpc.stream_stream_rpc_method_handler,
|
|
281
|
+
}[(handler.request_streaming, handler.response_streaming)]
|
|
282
|
+
return make(
|
|
283
|
+
behavior,
|
|
284
|
+
request_deserializer=handler.request_deserializer,
|
|
285
|
+
response_serializer=handler.response_serializer,
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def _counted(
|
|
290
|
+
limit: _ConcurrencyLimit, behavior: Callable[..., Any], streaming: bool
|
|
291
|
+
) -> Callable[..., Any]:
|
|
292
|
+
"""Wraps a synchronous behavior so that it holds a place in `limit` while
|
|
293
|
+
it runs. A call grpcio never hands to its behavior holds none."""
|
|
294
|
+
if streaming:
|
|
295
|
+
|
|
296
|
+
def counted_stream(request: Any, context: grpc.ServicerContext) -> Any:
|
|
297
|
+
if not limit.admit():
|
|
298
|
+
context.abort(grpc.StatusCode.RESOURCE_EXHAUSTED, _LIMIT_EXCEEDED)
|
|
299
|
+
try:
|
|
300
|
+
yield from behavior(request, context)
|
|
301
|
+
finally:
|
|
302
|
+
limit.release()
|
|
303
|
+
|
|
304
|
+
return counted_stream
|
|
305
|
+
|
|
306
|
+
def counted(request: Any, context: grpc.ServicerContext) -> Any:
|
|
307
|
+
if not limit.admit():
|
|
308
|
+
context.abort(grpc.StatusCode.RESOURCE_EXHAUSTED, _LIMIT_EXCEEDED)
|
|
309
|
+
try:
|
|
310
|
+
return behavior(request, context)
|
|
311
|
+
finally:
|
|
312
|
+
limit.release()
|
|
313
|
+
|
|
314
|
+
return counted
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
class _FallbackLimit(grpc.ServerInterceptor):
|
|
318
|
+
"""Counts the fallback server's calls against the front's limit."""
|
|
319
|
+
|
|
320
|
+
def __init__(self, limit: _ConcurrencyLimit) -> None:
|
|
321
|
+
self._limit = limit
|
|
322
|
+
|
|
323
|
+
def intercept_service(
|
|
324
|
+
self,
|
|
325
|
+
continuation: Callable[[grpc.HandlerCallDetails], grpc.RpcMethodHandler | None],
|
|
326
|
+
handler_call_details: grpc.HandlerCallDetails,
|
|
327
|
+
) -> grpc.RpcMethodHandler | None:
|
|
328
|
+
handler = continuation(handler_call_details)
|
|
329
|
+
if handler is None:
|
|
330
|
+
return None
|
|
331
|
+
behavior = _counted(self._limit, _behavior(handler), handler.response_streaming)
|
|
332
|
+
return _with_behavior(handler, behavior)
|
|
333
|
+
|
|
334
|
+
|
|
179
335
|
class _Call:
|
|
180
336
|
def __init__(
|
|
181
337
|
self,
|
|
182
338
|
call_id: int,
|
|
183
339
|
method: str,
|
|
184
|
-
handler: grpc.RpcMethodHandler,
|
|
340
|
+
handler: grpc.RpcMethodHandler | None,
|
|
341
|
+
streaming: bool,
|
|
185
342
|
metadata: tuple[tuple[str, str | bytes], ...],
|
|
186
343
|
peer: str,
|
|
187
344
|
time_remaining: float | None,
|
|
@@ -191,13 +348,14 @@ class _Call:
|
|
|
191
348
|
self.handler = handler
|
|
192
349
|
# Whether the front serves the method as a stream; an interceptor
|
|
193
350
|
# can swap the handler, but not this.
|
|
194
|
-
self.streaming =
|
|
351
|
+
self.streaming = streaming
|
|
195
352
|
self.metadata = metadata
|
|
196
353
|
self.peer = peer
|
|
197
354
|
self.deadline = (
|
|
198
355
|
None if time_remaining is None else time.monotonic() + time_remaining
|
|
199
356
|
)
|
|
200
357
|
self.lock = threading.Lock()
|
|
358
|
+
self.admitted = True
|
|
201
359
|
self.active = True
|
|
202
360
|
self.callbacks: list[Callable[[], None]] = []
|
|
203
361
|
|
|
@@ -369,12 +527,11 @@ class _Codec:
|
|
|
369
527
|
return data
|
|
370
528
|
|
|
371
529
|
|
|
372
|
-
class Server(_Codec, grpc.Server):
|
|
530
|
+
class Server(_Codec, _Methods, grpc.Server):
|
|
373
531
|
"""A grpc.Server whose HTTP/2 and gRPC work runs in Rust.
|
|
374
532
|
|
|
375
|
-
Serves unary and server-streaming methods
|
|
376
|
-
|
|
377
|
-
insecure port. Other methods are served by a grpcio server behind it.
|
|
533
|
+
Serves unary and server-streaming methods on one insecure port. Other
|
|
534
|
+
methods are served by a grpcio server behind it.
|
|
378
535
|
"""
|
|
379
536
|
|
|
380
537
|
def __init__(
|
|
@@ -387,10 +544,9 @@ class Server(_Codec, grpc.Server):
|
|
|
387
544
|
compression: grpc.Compression | None = None,
|
|
388
545
|
io_threads: int = 2,
|
|
389
546
|
) -> None:
|
|
390
|
-
if maximum_concurrent_rpcs is not None:
|
|
391
|
-
raise NotImplementedError("maximum_concurrent_rpcs isn't supported yet")
|
|
392
547
|
if compression not in (None, grpc.Compression.NoCompression):
|
|
393
548
|
raise NotImplementedError("response compression isn't supported")
|
|
549
|
+
self._limit = _ConcurrencyLimit(maximum_concurrent_rpcs)
|
|
394
550
|
self._options = list(options or ())
|
|
395
551
|
self._receive_limit, self._send_limit, self._front_args = _parse_options(
|
|
396
552
|
self._options
|
|
@@ -403,7 +559,9 @@ class Server(_Codec, grpc.Server):
|
|
|
403
559
|
self._front: NativeGrpcFront | None = None
|
|
404
560
|
self._fallback: grpc.Server | None = None
|
|
405
561
|
self._fallback_dir: str | None = None
|
|
406
|
-
self.
|
|
562
|
+
self._generic: list[grpc.GenericRpcHandler] = []
|
|
563
|
+
self._table: list[tuple[str, grpc.RpcMethodHandler | None, bool]] = []
|
|
564
|
+
self._resolved: dict[str, int | None] = {}
|
|
407
565
|
self._calls: dict[int, _Call] = {}
|
|
408
566
|
self._lock = threading.Lock()
|
|
409
567
|
self._started = False
|
|
@@ -455,11 +613,16 @@ class Server(_Codec, grpc.Server):
|
|
|
455
613
|
# only the fallback server can run them for those.
|
|
456
614
|
if streamed or generic or self._interceptors:
|
|
457
615
|
self._start_fallback(streamed, generic)
|
|
458
|
-
|
|
616
|
+
if generic:
|
|
617
|
+
self._generic = generic
|
|
618
|
+
self._front._resolve_unknown_methods()
|
|
619
|
+
self._table = [
|
|
620
|
+
(path, handler, handler.response_streaming)
|
|
621
|
+
for path, handler in native.items()
|
|
622
|
+
]
|
|
459
623
|
self._front.add_methods(
|
|
460
|
-
[(path,
|
|
624
|
+
[(path, streaming) for path, _, streaming in self._table]
|
|
461
625
|
)
|
|
462
|
-
self._table = [(path, native[path]) for path in paths]
|
|
463
626
|
self._front.start()
|
|
464
627
|
self._started = True
|
|
465
628
|
threading.Thread(
|
|
@@ -473,8 +636,12 @@ class Server(_Codec, grpc.Server):
|
|
|
473
636
|
) -> None:
|
|
474
637
|
assert self._front is not None
|
|
475
638
|
self._fallback_dir, path = _fallback_socket()
|
|
639
|
+
interceptors = self._interceptors
|
|
640
|
+
if self._limit.maximum is not None:
|
|
641
|
+
# Last: grpcio applies its limit after the interceptors.
|
|
642
|
+
interceptors += (_FallbackLimit(self._limit),)
|
|
476
643
|
fallback = grpc.server(
|
|
477
|
-
self._executor, interceptors=
|
|
644
|
+
self._executor, interceptors=interceptors, options=self._options
|
|
478
645
|
)
|
|
479
646
|
fallback.add_generic_rpc_handlers(generic)
|
|
480
647
|
for service, handlers in streamed.items():
|
|
@@ -534,6 +701,9 @@ class Server(_Codec, grpc.Server):
|
|
|
534
701
|
if call is not None:
|
|
535
702
|
call.terminate()
|
|
536
703
|
continue
|
|
704
|
+
if kind == _EVENT_RESOLVE:
|
|
705
|
+
self._resolve(call_id, payload, details[0])
|
|
706
|
+
continue
|
|
537
707
|
if self._stopping:
|
|
538
708
|
front.fail(
|
|
539
709
|
call_id,
|
|
@@ -546,17 +716,31 @@ class Server(_Codec, grpc.Server):
|
|
|
546
716
|
and payload is not None
|
|
547
717
|
and details is not None
|
|
548
718
|
)
|
|
549
|
-
|
|
550
|
-
|
|
719
|
+
call = _Call(call_id, *self._table[method_id], *details)
|
|
720
|
+
if self._limit.maximum is not None and not self._limit.admit():
|
|
721
|
+
# Not on a worker, so busy workers can't delay it.
|
|
722
|
+
call.admitted = False
|
|
723
|
+
contextvars.Context().run(self._reject_over_limit, call)
|
|
724
|
+
continue
|
|
551
725
|
self._calls[call_id] = call
|
|
552
726
|
run = self._run_stream if call.streaming else self._run_unary
|
|
553
727
|
# Each call gets its own context, which its interceptors
|
|
554
728
|
# and handler share, as in grpcio.
|
|
555
|
-
self._executor.submit(
|
|
729
|
+
future = self._executor.submit(
|
|
730
|
+
contextvars.Context().run, run, call, payload
|
|
731
|
+
)
|
|
732
|
+
if self._limit.maximum is not None:
|
|
733
|
+
future.add_done_callback(
|
|
734
|
+
lambda _, call=call: self._release(call)
|
|
735
|
+
)
|
|
556
736
|
except Exception:
|
|
557
737
|
_LOGGER.exception("Error dispatching call %d", call_id)
|
|
558
|
-
self._calls.pop(call_id, None)
|
|
559
|
-
if
|
|
738
|
+
call = self._calls.pop(call_id, None)
|
|
739
|
+
if call is not None:
|
|
740
|
+
self._release(call)
|
|
741
|
+
if kind == _EVENT_RESOLVE:
|
|
742
|
+
front._resolved(call_id, None, remember=False)
|
|
743
|
+
elif kind == EVENT_CALL:
|
|
560
744
|
front.fail(
|
|
561
745
|
call_id,
|
|
562
746
|
_code(grpc.StatusCode.INTERNAL),
|
|
@@ -564,8 +748,9 @@ class Server(_Codec, grpc.Server):
|
|
|
564
748
|
)
|
|
565
749
|
|
|
566
750
|
def _intercept(self, call: _Call) -> bool:
|
|
567
|
-
"""Runs the interceptors, which may replace the call's handler
|
|
568
|
-
|
|
751
|
+
"""Runs the interceptors, which may replace the call's handler, and
|
|
752
|
+
looks up the handler of a method only generic handlers know."""
|
|
753
|
+
if not self._interceptors and call.handler is not None:
|
|
569
754
|
return True
|
|
570
755
|
front = self._front
|
|
571
756
|
assert front is not None
|
|
@@ -577,7 +762,7 @@ class Server(_Codec, grpc.Server):
|
|
|
577
762
|
|
|
578
763
|
def intercept_at(index: int, details: grpc.HandlerCallDetails) -> Any:
|
|
579
764
|
if index == len(self._interceptors):
|
|
580
|
-
return registered
|
|
765
|
+
return registered if registered is not None else self._lookup(details)
|
|
581
766
|
return self._interceptors[index].intercept_service(
|
|
582
767
|
lambda details: intercept_at(index + 1, details), details
|
|
583
768
|
)
|
|
@@ -594,6 +779,23 @@ class Server(_Codec, grpc.Server):
|
|
|
594
779
|
return False
|
|
595
780
|
return _use_handler(front, call, handler)
|
|
596
781
|
|
|
782
|
+
def _reject_over_limit(self, call: _Call) -> None:
|
|
783
|
+
try:
|
|
784
|
+
if self._intercept(call):
|
|
785
|
+
self._front.fail( # type: ignore[union-attr]
|
|
786
|
+
call.call_id,
|
|
787
|
+
_code(grpc.StatusCode.RESOURCE_EXHAUSTED),
|
|
788
|
+
_LIMIT_EXCEEDED,
|
|
789
|
+
)
|
|
790
|
+
finally:
|
|
791
|
+
call.terminate()
|
|
792
|
+
|
|
793
|
+
def _release(self, call: _Call) -> None:
|
|
794
|
+
with call.lock:
|
|
795
|
+
admitted, call.admitted = call.admitted, False
|
|
796
|
+
if admitted:
|
|
797
|
+
self._limit.release()
|
|
798
|
+
|
|
597
799
|
def _fail(
|
|
598
800
|
self, call: _Call, context: _Context, code: grpc.StatusCode, details: str
|
|
599
801
|
) -> None:
|
|
@@ -613,6 +815,7 @@ class Server(_Codec, grpc.Server):
|
|
|
613
815
|
def _run_unary(self, call: _Call, payload: bytes) -> None:
|
|
614
816
|
try:
|
|
615
817
|
if not self._intercept(call):
|
|
818
|
+
self._release(call)
|
|
616
819
|
return
|
|
617
820
|
ok, request = self._request(call, payload)
|
|
618
821
|
if not ok:
|
|
@@ -655,6 +858,7 @@ class Server(_Codec, grpc.Server):
|
|
|
655
858
|
responses = None
|
|
656
859
|
try:
|
|
657
860
|
if not self._intercept(call):
|
|
861
|
+
self._release(call)
|
|
658
862
|
return
|
|
659
863
|
ok, request = self._request(call, payload)
|
|
660
864
|
if not ok:
|