pygrpc-native 0.6.0__tar.gz → 0.7.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.
Files changed (82) hide show
  1. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/CHANGELOG.md +20 -0
  2. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/Cargo.lock +1 -1
  3. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/Cargo.toml +1 -1
  4. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/PKG-INFO +4 -1
  5. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/README.md +3 -0
  6. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/docs/low-level-api.md +5 -1
  7. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/python/pygrpc_native/_native.pyi +8 -3
  8. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/python/pygrpc_native/aio.py +16 -9
  9. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/src/lib.rs +41 -15
  10. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/src/server.rs +183 -12
  11. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_aio.py +30 -0
  12. pygrpc_native-0.7.0/tests/test_front.py +206 -0
  13. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_server.py +28 -0
  14. pygrpc_native-0.6.0/tests/test_front.py +0 -101
  15. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/.github/CODEOWNERS +0 -0
  16. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
  17. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/.github/ISSUE_TEMPLATE/config.yml +0 -0
  18. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
  19. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
  20. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/.github/dependabot.yml +0 -0
  21. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/.github/workflows/ci.yml +0 -0
  22. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/.github/workflows/dco.yml +0 -0
  23. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/.github/workflows/release.yml +0 -0
  24. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/.github/workflows/stale.yml +0 -0
  25. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/.gitignore +0 -0
  26. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/.pre-commit-config.yaml +0 -0
  27. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/CODE_OF_CONDUCT.md +0 -0
  28. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/CONTRIBUTING.md +0 -0
  29. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/DCO +0 -0
  30. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/LICENSE +0 -0
  31. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/SECURITY.md +0 -0
  32. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/benchmarks/README.md +0 -0
  33. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/benchmarks/echo.py +0 -0
  34. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/pyproject.toml +0 -0
  35. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/python/pygrpc_native/__init__.py +0 -0
  36. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/python/pygrpc_native/_server.py +0 -0
  37. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/python/pygrpc_native/py.typed +0 -0
  38. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/src/metrics.rs +0 -0
  39. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_add_methods.py +0 -0
  40. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_api.py +0 -0
  41. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_bearer_auth.py +0 -0
  42. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_call_details.py +0 -0
  43. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_calls.py +0 -0
  44. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_concurrency_limit.py +0 -0
  45. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_connection.py +0 -0
  46. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_delay_stream_headers.py +0 -0
  47. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_downstream.py +0 -0
  48. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_fallback.py +0 -0
  49. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_generic_methods.py +0 -0
  50. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_interceptors.py +0 -0
  51. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_metrics.py +0 -0
  52. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_response_metadata.py +0 -0
  53. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/tests/test_stream_try_send.py +0 -0
  54. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/CHANGELOG.md +0 -0
  55. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/Cargo.toml +0 -0
  56. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/LICENSE-APACHE +0 -0
  57. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/LICENSE-MIT +0 -0
  58. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/README.md +0 -0
  59. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/byte_str.rs +0 -0
  60. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/convert.rs +0 -0
  61. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/error.rs +0 -0
  62. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/extensions.rs +0 -0
  63. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/header/map.rs +0 -0
  64. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/header/mod.rs +0 -0
  65. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/header/name.rs +0 -0
  66. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/header/value.rs +0 -0
  67. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/lib.rs +0 -0
  68. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/method.rs +0 -0
  69. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/request.rs +0 -0
  70. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/response.rs +0 -0
  71. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/status.rs +0 -0
  72. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/uri/authority.rs +0 -0
  73. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/uri/builder.rs +0 -0
  74. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/uri/mod.rs +0 -0
  75. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/uri/path.rs +0 -0
  76. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/uri/port.rs +0 -0
  77. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/uri/scheme.rs +0 -0
  78. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/uri/tests.rs +0 -0
  79. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/src/version.rs +0 -0
  80. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/tests/header_map.rs +0 -0
  81. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/tests/header_map_fuzz.rs +0 -0
  82. {pygrpc_native-0.6.0 → pygrpc_native-0.7.0}/vendor/http/tests/status_code.rs +0 -0
@@ -4,6 +4,26 @@ 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.7.0 (2026-10-30)
8
+
9
+ ### Added
10
+
11
+ - `NativeGrpcFront(ordered=True)` hands a connection's calls to Python in the
12
+ order the client started them. Without it, calls sent back to back on one
13
+ connection can arrive out of order. (#89)
14
+
15
+ ## 0.6.1 (2026-10-29)
16
+
17
+ ### Changed
18
+
19
+ - A server-streaming call queues messages until they add up to 256 KiB
20
+ before the handler waits for the client, instead of queueing one message.
21
+ Streams of small messages use much less server CPU; a message over 256 KiB
22
+ still goes alone, and a client that stops reading still stops the handler.
23
+ (#85)
24
+ - On `aio.server()`, a stream write that has to wait for room waits on the
25
+ event loop instead of a worker thread. (#83)
26
+
7
27
  ## 0.6.0 (2026-10-29)
8
28
 
9
29
  ### Added
@@ -395,7 +395,7 @@ dependencies = [
395
395
 
396
396
  [[package]]
397
397
  name = "pygrpc_native"
398
- version = "0.6.0"
398
+ version = "0.7.0"
399
399
  dependencies = [
400
400
  "bytes",
401
401
  "crossbeam-channel",
@@ -2,7 +2,7 @@
2
2
 
3
3
  [package]
4
4
  name = "pygrpc_native"
5
- version = "0.6.0"
5
+ version = "0.7.0"
6
6
  edition = "2021"
7
7
  license = "Apache-2.0"
8
8
  description = "A gRPC server in Rust that hands calls to Python handlers in batches"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pygrpc-native
3
- Version: 0.6.0
3
+ Version: 0.7.0
4
4
  Classifier: Programming Language :: Python :: 3
5
5
  Classifier: Programming Language :: Rust
6
6
  Classifier: Operating System :: POSIX :: Linux
@@ -240,6 +240,9 @@ has all the numbers and explains how they were measured.
240
240
  - A method that only generic handlers know is looked up when it is first
241
241
  called, so their `service()` runs once more for that call. A method none of
242
242
  them knows is looked up on each call before it goes to the fallback server.
243
+ - Calls sent back to back on one connection can reach handlers out of order.
244
+ gRPC doesn't guarantee an order across calls, though grpcio usually keeps
245
+ it. `NativeGrpcFront(ordered=True)` keeps it.
243
246
  - A call whose request metadata is over the limit (16 KiB unless set) is
244
247
  refused. Between `grpc.max_metadata_size` and that limit, grpcio also
245
248
  refuses some calls at random. This server doesn't.
@@ -226,6 +226,9 @@ has all the numbers and explains how they were measured.
226
226
  - A method that only generic handlers know is looked up when it is first
227
227
  called, so their `service()` runs once more for that call. A method none of
228
228
  them knows is looked up on each call before it goes to the fallback server.
229
+ - Calls sent back to back on one connection can reach handlers out of order.
230
+ gRPC doesn't guarantee an order across calls, though grpcio usually keeps
231
+ it. `NativeGrpcFront(ordered=True)` keeps it.
229
232
  - A call whose request metadata is over the limit (16 KiB unless set) is
230
233
  refused. Between `grpc.max_metadata_size` and that limit, grpcio also
231
234
  refuses some calls at random. This server doesn't.
@@ -38,7 +38,7 @@ pass the serialized reply to `respond`.
38
38
 
39
39
  ## Reference
40
40
 
41
- `NativeGrpcFront(target, methods, io_threads=2, client_key_metadata=None, call_details=False, delay_stream_headers=False, bearer_token_sha256=None, unauthenticated_message="Unauthenticated", metrics=False, keepalive_time_ms=7200000, keepalive_timeout_ms=20000, keepalive_permit_without_calls=True, max_concurrent_streams=1024, max_metadata_size=16384, reuse_port=False)`
41
+ `NativeGrpcFront(target, methods, io_threads=2, client_key_metadata=None, call_details=False, delay_stream_headers=False, bearer_token_sha256=None, unauthenticated_message="Unauthenticated", metrics=False, keepalive_time_ms=7200000, keepalive_timeout_ms=20000, keepalive_permit_without_calls=True, max_concurrent_streams=1024, max_metadata_size=16384, reuse_port=False, ordered=False)`
42
42
  binds `host:port` (every address the host resolves to), `unix:path` or
43
43
  `unix:///path`. Port 0 picks a free port. `methods` lists
44
44
  `(path, server_streaming)` pairs, where a path is `/package.Service/Method`
@@ -122,6 +122,10 @@ counts a header list; a call with more is refused. With `reuse_port=True`,
122
122
  several processes can bind the same TCP port; on Linux the kernel spreads
123
123
  connections across them.
124
124
 
125
+ A connection's calls can reach `recv_batch` out of order. With `ordered=True`
126
+ they come in the order the client started them, and each call waits until the
127
+ ones before it have arrived in full or ended.
128
+
125
129
  `set_fallback` lets one port serve methods you don't list: run a regular
126
130
  `grpc.Server` on a Unix socket and its methods are proxied there, with its
127
131
  interceptors.
@@ -8,18 +8,21 @@ EVENT_CALL: int
8
8
  EVENT_CANCEL: int
9
9
  _EVENT_RESOLVE: int
10
10
  _MAX_RESOLVED: int
11
+ _EVENT_WRITABLE: int
12
+ _STREAM_PARKED: int
11
13
  STREAM_SENT: int
12
14
  STREAM_FULL: int
13
15
  STREAM_CLOSED: int
14
16
 
15
17
  # (kind, call_id, method_id, payload, client_key); payload and client_key are
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]
18
+ # None for EVENT_CANCEL. payload is the method path for _EVENT_RESOLVE, and
19
+ # whether the held message went out for _EVENT_WRITABLE.
20
+ Event = tuple[int, int, int, bytes | str | bool | None, bytes | str | None]
18
21
 
19
22
  # (metadata, peer, time_remaining); metadata values are bytes for -bin keys.
20
23
  CallDetails = tuple[tuple[tuple[str, str | bytes], ...], str, float | None]
21
24
  DetailedEvent = tuple[
22
- int, int, int, bytes | str | None, bytes | str | None, CallDetails | None
25
+ int, int, int, bytes | str | bool | None, bytes | str | None, CallDetails | None
23
26
  ]
24
27
 
25
28
  class NativeGrpcFront:
@@ -40,6 +43,7 @@ class NativeGrpcFront:
40
43
  max_concurrent_streams: int = 1024,
41
44
  max_metadata_size: int = 16384,
42
45
  reuse_port: bool = False,
46
+ ordered: bool = False,
43
47
  ) -> None: ...
44
48
  @property
45
49
  def bound_port(self) -> int: ...
@@ -76,6 +80,7 @@ class NativeGrpcFront:
76
80
  ) -> bool: ...
77
81
  def stream_send(self, call_id: int, payload: bytes) -> bool: ...
78
82
  def stream_try_send(self, call_id: int, payload: bytes) -> int: ...
83
+ def _stream_send_or_park(self, call_id: int, payload: bytes) -> int: ...
79
84
  def stream_send_initial_metadata(
80
85
  self, call_id: int, metadata: Metadata
81
86
  ) -> bool: ...
@@ -14,10 +14,11 @@ import grpc
14
14
  from grpc import aio
15
15
 
16
16
  from pygrpc_native._native import (
17
- EVENT_CANCEL,
18
17
  _EVENT_RESOLVE,
18
+ _EVENT_WRITABLE,
19
+ _STREAM_PARKED,
20
+ EVENT_CANCEL,
19
21
  STREAM_CLOSED,
20
- STREAM_FULL,
21
22
  NativeGrpcFront,
22
23
  )
23
24
  from pygrpc_native._server import (
@@ -74,6 +75,7 @@ class _Call:
74
75
  self.cancelled = False
75
76
  self.done = False
76
77
  self.callbacks: list[Callable[[Any], None]] = []
78
+ self.writable: asyncio.Future[bool] | None = None
77
79
 
78
80
  def cancel(self) -> None:
79
81
  self.cancelled = True
@@ -535,6 +537,11 @@ class Server(_Codec, _Methods, aio.Server):
535
537
  if call is not None:
536
538
  call.cancel()
537
539
  continue
540
+ if kind == _EVENT_WRITABLE:
541
+ call = self._calls.get(call_id)
542
+ if call is not None and call.writable is not None:
543
+ call.writable.set_result(payload)
544
+ continue
538
545
  if kind == _EVENT_RESOLVE:
539
546
  try:
540
547
  self._resolve(call_id, payload, details[0])
@@ -755,13 +762,13 @@ class Server(_Codec, _Methods, aio.Server):
755
762
  if data is None:
756
763
  return False
757
764
  context._initial_sent = True
758
- sent = front.stream_try_send(call.call_id, data)
759
- if sent == STREAM_FULL:
760
- # The client is behind; wait for room off the loop.
761
- loop = asyncio.get_running_loop()
762
- return await loop.run_in_executor(
763
- None, front.stream_send, call.call_id, data
764
- )
765
+ sent = front._stream_send_or_park(call.call_id, data)
766
+ if sent == _STREAM_PARKED:
767
+ call.writable = asyncio.get_running_loop().create_future()
768
+ try:
769
+ return await call.writable
770
+ finally:
771
+ call.writable = None
765
772
  return sent != STREAM_CLOSED
766
773
 
767
774
 
@@ -42,9 +42,11 @@ const RUNTIME_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(2);
42
42
  const EVENT_CALL: u8 = 0;
43
43
  const EVENT_CANCEL: u8 = 1;
44
44
  const EVENT_RESOLVE: u8 = 2;
45
+ const EVENT_WRITABLE: u8 = 3;
45
46
  const STREAM_SENT: u8 = 0;
46
47
  const STREAM_FULL: u8 = 1;
47
48
  const STREAM_CLOSED: u8 = 2;
49
+ const STREAM_PARKED: u8 = 3;
48
50
 
49
51
  struct Running {
50
52
  runtime: Runtime,
@@ -94,6 +96,9 @@ struct Running {
94
96
  /// reuse_port: Set SO_REUSEPORT on TCP listeners, so several processes
95
97
  /// can serve one port. On Linux the kernel spreads connections
96
98
  /// across them. Ignored for Unix sockets.
99
+ /// ordered: Hand a connection's calls to Python in the order the client
100
+ /// started them. Each call waits until the ones before it have
101
+ /// arrived in full or ended.
97
102
  ///
98
103
  /// Raises:
99
104
  /// OSError: If the address cannot be bound.
@@ -105,6 +110,7 @@ struct NativeGrpcFront {
105
110
  shared: Arc<Shared>,
106
111
  events: Receiver<Event>,
107
112
  running: Mutex<Option<Running>>,
113
+ handle: tokio::runtime::Handle,
108
114
  bound_port: u16,
109
115
  socket_path: Option<PathBuf>,
110
116
  }
@@ -128,6 +134,7 @@ impl NativeGrpcFront {
128
134
  max_concurrent_streams=1024,
129
135
  max_metadata_size=16384,
130
136
  reuse_port=false,
137
+ ordered=false,
131
138
  ))]
132
139
  // Python keyword arguments.
133
140
  #[allow(clippy::too_many_arguments)]
@@ -147,6 +154,7 @@ impl NativeGrpcFront {
147
154
  max_concurrent_streams: u32,
148
155
  max_metadata_size: u32,
149
156
  reuse_port: bool,
157
+ ordered: bool,
150
158
  ) -> PyResult<Self> {
151
159
  if io_threads == 0 {
152
160
  return Err(PyValueError::new_err("io_threads must be greater than 0"));
@@ -226,8 +234,10 @@ impl NativeGrpcFront {
226
234
  max_metadata_size,
227
235
  },
228
236
  metrics.then_some(io_threads),
237
+ ordered,
229
238
  )),
230
239
  events,
240
+ handle: runtime.handle().clone(),
231
241
  running: Mutex::new(Some(Running {
232
242
  runtime,
233
243
  listeners,
@@ -274,12 +284,8 @@ impl NativeGrpcFront {
274
284
  })
275
285
  }
276
286
 
277
- /// Internal to ``server()`` and ``aio.server()``: before forwarding a call
278
- /// to a method not registered, send an _EVENT_RESOLVE event (resolve id,
279
- /// method path) and wait for ``_resolved()``. Call before ``start()``.
280
- ///
281
- /// Raises:
282
- /// RuntimeError: After ``start()``.
287
+ /// Internal to ``server()`` and ``aio.server()``: ask with _EVENT_RESOLVE
288
+ /// (resolve id, path) before forwarding an unknown method. Before ``start()``.
283
289
  #[pyo3(name = "_resolve_unknown_methods")]
284
290
  fn resolve_unknown_methods(&self) -> PyResult<()> {
285
291
  if self.shared.resolve_unknown_methods() {
@@ -289,14 +295,8 @@ impl NativeGrpcFront {
289
295
  }
290
296
  }
291
297
 
292
- /// Serve the method natively as ``method_id``, which follows the
293
- /// registered ids (at most _MAX_RESOLVED more), or forward it with None.
294
- /// Native methods are always remembered; others only with ``remember``.
295
- ///
296
- /// Returns False if the call that asked is gone.
297
- ///
298
- /// Raises:
299
- /// ValueError: If ``method_id`` is out of that range.
298
+ /// Answers _EVENT_RESOLVE: serve natively as ``method_id``, or forward
299
+ /// with None. Native answers are always remembered.
300
300
  #[pyo3(
301
301
  name = "_resolved",
302
302
  signature = (resolve_id, method_id, server_streaming=false, remember=true)
@@ -448,7 +448,7 @@ impl NativeGrpcFront {
448
448
  return false;
449
449
  };
450
450
  let payload = Bytes::from_owner(payload);
451
- py.detach(|| sender.blocking_send(payload).is_ok())
451
+ py.detach(|| sender.blocking_send(&self.handle, payload))
452
452
  }
453
453
 
454
454
  /// ``stream_send`` without blocking, for callers on an event loop.
@@ -466,6 +466,27 @@ impl NativeGrpcFront {
466
466
  }
467
467
  }
468
468
 
469
+ /// Internal to ``aio.server()``: ``stream_try_send`` that holds a message
470
+ /// that doesn't fit, then sends _EVENT_WRITABLE (call id, sent).
471
+ #[pyo3(name = "_stream_send_or_park")]
472
+ fn stream_send_or_park(&self, call_id: u64, payload: PyBackedBytes) -> u8 {
473
+ let Some(sender) = self.shared.message_sender(call_id) else {
474
+ return STREAM_CLOSED;
475
+ };
476
+ match sender.try_send(Bytes::from_owner(payload)) {
477
+ Ok(()) => STREAM_SENT,
478
+ Err(TrySendError::Full(payload)) => {
479
+ let shared = self.shared.clone();
480
+ self.handle.spawn(async move {
481
+ let sent = sender.send(payload).await;
482
+ shared.writable(call_id, sent);
483
+ });
484
+ STREAM_PARKED
485
+ }
486
+ Err(TrySendError::Closed(_)) => STREAM_CLOSED,
487
+ }
488
+ }
489
+
469
490
  /// Send a stream's response headers now, with this metadata. Only for a
470
491
  /// server created with ``delay_stream_headers=True``.
471
492
  ///
@@ -698,6 +719,9 @@ fn event_tuple<'py>(py: Python<'py>, event: Event) -> PyResult<Bound<'py, PyTupl
698
719
  Event::Resolve {
699
720
  resolve_id, path, ..
700
721
  } => (EVENT_RESOLVE, resolve_id, 0u32, path, py.None()).into_pyobject(py),
722
+ Event::Writable { call_id, sent } => {
723
+ (EVENT_WRITABLE, call_id, 0u32, sent, py.None()).into_pyobject(py)
724
+ }
701
725
  Event::Closed => Err(PyRuntimeError::new_err("unexpected close event")),
702
726
  }
703
727
  }
@@ -859,6 +883,8 @@ fn _native(m: &Bound<'_, PyModule>) -> PyResult<()> {
859
883
  m.add("EVENT_CANCEL", EVENT_CANCEL)?;
860
884
  m.add("_EVENT_RESOLVE", EVENT_RESOLVE)?;
861
885
  m.add("_MAX_RESOLVED", MAX_RESOLVED)?;
886
+ m.add("_EVENT_WRITABLE", EVENT_WRITABLE)?;
887
+ m.add("_STREAM_PARKED", STREAM_PARKED)?;
862
888
  m.add("STREAM_SENT", STREAM_SENT)?;
863
889
  m.add("STREAM_FULL", STREAM_FULL)?;
864
890
  m.add("STREAM_CLOSED", STREAM_CLOSED)?;