pygrpc-native 0.4.0__tar.gz → 0.5.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 (79) hide show
  1. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/CHANGELOG.md +10 -0
  2. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/Cargo.lock +2 -1
  3. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/Cargo.toml +3 -1
  4. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/PKG-INFO +12 -12
  5. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/README.md +11 -11
  6. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/docs/low-level-api.md +4 -2
  7. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/python/pygrpc_native/_native.pyi +1 -0
  8. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/python/pygrpc_native/_server.py +3 -1
  9. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/src/lib.rs +33 -5
  10. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_connection.py +3 -4
  11. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/.github/CODEOWNERS +0 -0
  12. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
  13. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/.github/ISSUE_TEMPLATE/config.yml +0 -0
  14. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
  15. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
  16. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/.github/dependabot.yml +0 -0
  17. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/.github/workflows/ci.yml +0 -0
  18. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/.github/workflows/dco.yml +0 -0
  19. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/.github/workflows/release.yml +0 -0
  20. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/.github/workflows/stale.yml +0 -0
  21. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/.gitignore +0 -0
  22. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/.pre-commit-config.yaml +0 -0
  23. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/CODE_OF_CONDUCT.md +0 -0
  24. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/CONTRIBUTING.md +0 -0
  25. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/DCO +0 -0
  26. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/LICENSE +0 -0
  27. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/SECURITY.md +0 -0
  28. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/benchmarks/README.md +0 -0
  29. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/benchmarks/echo.py +0 -0
  30. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/pyproject.toml +0 -0
  31. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/python/pygrpc_native/__init__.py +0 -0
  32. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/python/pygrpc_native/aio.py +0 -0
  33. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/python/pygrpc_native/py.typed +0 -0
  34. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/src/metrics.rs +0 -0
  35. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/src/server.rs +0 -0
  36. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_add_methods.py +0 -0
  37. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_aio.py +0 -0
  38. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_api.py +0 -0
  39. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_bearer_auth.py +0 -0
  40. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_call_details.py +0 -0
  41. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_calls.py +0 -0
  42. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_delay_stream_headers.py +0 -0
  43. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_downstream.py +0 -0
  44. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_fallback.py +0 -0
  45. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_front.py +0 -0
  46. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_interceptors.py +0 -0
  47. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_metrics.py +0 -0
  48. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_response_metadata.py +0 -0
  49. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_server.py +0 -0
  50. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/tests/test_stream_try_send.py +0 -0
  51. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/CHANGELOG.md +0 -0
  52. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/Cargo.toml +0 -0
  53. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/LICENSE-APACHE +0 -0
  54. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/LICENSE-MIT +0 -0
  55. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/README.md +0 -0
  56. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/byte_str.rs +0 -0
  57. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/convert.rs +0 -0
  58. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/error.rs +0 -0
  59. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/extensions.rs +0 -0
  60. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/header/map.rs +0 -0
  61. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/header/mod.rs +0 -0
  62. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/header/name.rs +0 -0
  63. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/header/value.rs +0 -0
  64. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/lib.rs +0 -0
  65. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/method.rs +0 -0
  66. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/request.rs +0 -0
  67. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/response.rs +0 -0
  68. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/status.rs +0 -0
  69. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/uri/authority.rs +0 -0
  70. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/uri/builder.rs +0 -0
  71. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/uri/mod.rs +0 -0
  72. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/uri/path.rs +0 -0
  73. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/uri/port.rs +0 -0
  74. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/uri/scheme.rs +0 -0
  75. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/uri/tests.rs +0 -0
  76. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/src/version.rs +0 -0
  77. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/tests/header_map.rs +0 -0
  78. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/tests/header_map_fuzz.rs +0 -0
  79. {pygrpc_native-0.4.0 → pygrpc_native-0.5.0}/vendor/http/tests/status_code.rs +0 -0
@@ -4,6 +4,16 @@ 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.5.0 (2026-10-27)
8
+
9
+ Everything here is an addition, so code written for 0.4 keeps working.
10
+
11
+ ### Added
12
+
13
+ - `server()` and `aio.server()` take `grpc.so_reuseport` set to 1, so several
14
+ processes can serve one port, and `NativeGrpcFront` a matching `reuse_port`
15
+ argument. It stays off unless asked for. (#57)
16
+
7
17
  ## 0.4.0 (2026-10-27)
8
18
 
9
19
  `server()` and `aio.server()` now take the interceptors and options grpcio
@@ -395,7 +395,7 @@ dependencies = [
395
395
 
396
396
  [[package]]
397
397
  name = "pygrpc_native"
398
- version = "0.4.0"
398
+ version = "0.5.0"
399
399
  dependencies = [
400
400
  "bytes",
401
401
  "crossbeam-channel",
@@ -409,6 +409,7 @@ dependencies = [
409
409
  "pyo3",
410
410
  "pyo3-log",
411
411
  "sha2",
412
+ "socket2",
412
413
  "subtle",
413
414
  "tokio",
414
415
  "tonic",
@@ -2,7 +2,7 @@
2
2
 
3
3
  [package]
4
4
  name = "pygrpc_native"
5
- version = "0.4.0"
5
+ version = "0.5.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"
@@ -33,6 +33,8 @@ log = "0.4"
33
33
  pyo3 = { version = "0.29", features = ["abi3-py310"] }
34
34
  pyo3-log = "0.13"
35
35
  sha2 = "0.11"
36
+ # "all" for set_reuse_port.
37
+ socket2 = { version = "0.6", features = ["all"] }
36
38
  subtle = "2.6"
37
39
  tokio = { version = "1", features = ["rt-multi-thread", "net", "sync", "time", "macros"] }
38
40
  tonic = { version = "0.14", default-features = false, features = ["server", "gzip"] }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pygrpc-native
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Classifier: Programming Language :: Python :: 3
5
5
  Classifier: Programming Language :: Rust
6
6
  Classifier: Operating System :: POSIX :: Linux
@@ -81,19 +81,19 @@ socket behind it. They work, but they aren't any faster, and `peer()` in
81
81
  them is the socket.
82
82
 
83
83
  Interceptors run as in grpcio, on the methods served here and on the fallback
84
- server. Of the server options, the message size limits, the keepalive
85
- settings (`grpc.keepalive_time_ms`, `grpc.keepalive_timeout_ms` and
84
+ server. Of the server options, the message size limits, the keepalive settings
85
+ (`grpc.keepalive_time_ms`, `grpc.keepalive_timeout_ms` and
86
86
  `grpc.keepalive_permit_without_calls`), `grpc.max_concurrent_streams` and the
87
87
  metadata size limits (`grpc.max_metadata_size` and
88
88
  `grpc.absolute_max_metadata_size`) work as in grpcio. Options that ask for
89
89
  something this server can't do raise an error: closing connections by age or
90
90
  idleness (`grpc.max_connection_age_ms`, `grpc.max_connection_age_grace_ms`,
91
- `grpc.max_connection_idle_ms`), `grpc.max_allowed_incoming_connections`,
92
- response compression and `grpc.so_reuseport`, as do `maximum_concurrent_rpcs`
93
- and TLS. Any other option is ignored, as grpcio ignores the many that only
94
- clients read. That covers the limits on client pings, which are accepted but
95
- not enforced, and `grpc.http2.max_frame_size`, which grpc-core's flow control
96
- overrides anyway. Values that aren't integers are ignored too, as in grpcio.
91
+ `grpc.max_connection_idle_ms`), `grpc.max_allowed_incoming_connections` and
92
+ response compression, as do `maximum_concurrent_rpcs` and TLS. Any other
93
+ option is ignored, as grpcio ignores the many that only clients read. That
94
+ covers the limits on client pings, which are accepted but not enforced, and
95
+ `grpc.http2.max_frame_size`, which grpc-core's flow control overrides anyway.
96
+ Values that aren't integers are ignored too, as in grpcio.
97
97
 
98
98
  There are two known differences. If a unary handler calls
99
99
  `send_initial_metadata()` and then fails, the metadata goes out with the
@@ -167,9 +167,9 @@ this README. Changes are listed in [CHANGELOG.md](CHANGELOG.md).
167
167
  `aio.server()` run grpcio interceptors instead.
168
168
  - Like grpc-core, binding a Unix socket path replaces any socket file
169
169
  already there.
170
- - The server never sets `SO_REUSEPORT`, so binding a port another server is
171
- using fails, where grpcio's default lets both bind it. `grpc.so_reuseport`
172
- set to 0 is accepted; other integers raise an error.
170
+ - The server sets `SO_REUSEPORT` only when `grpc.so_reuseport` is 1. grpcio
171
+ sets it by default, so binding a port another server already uses succeeds
172
+ with grpcio but fails here.
173
173
  - Per connection: at most 1024 concurrent streams unless
174
174
  `grpc.max_concurrent_streams` says otherwise. A connection that hasn't
175
175
  finished the HTTP/2 preface after 120 s is dropped.
@@ -67,19 +67,19 @@ socket behind it. They work, but they aren't any faster, and `peer()` in
67
67
  them is the socket.
68
68
 
69
69
  Interceptors run as in grpcio, on the methods served here and on the fallback
70
- server. Of the server options, the message size limits, the keepalive
71
- settings (`grpc.keepalive_time_ms`, `grpc.keepalive_timeout_ms` and
70
+ server. Of the server options, the message size limits, the keepalive settings
71
+ (`grpc.keepalive_time_ms`, `grpc.keepalive_timeout_ms` and
72
72
  `grpc.keepalive_permit_without_calls`), `grpc.max_concurrent_streams` and the
73
73
  metadata size limits (`grpc.max_metadata_size` and
74
74
  `grpc.absolute_max_metadata_size`) work as in grpcio. Options that ask for
75
75
  something this server can't do raise an error: closing connections by age or
76
76
  idleness (`grpc.max_connection_age_ms`, `grpc.max_connection_age_grace_ms`,
77
- `grpc.max_connection_idle_ms`), `grpc.max_allowed_incoming_connections`,
78
- response compression and `grpc.so_reuseport`, as do `maximum_concurrent_rpcs`
79
- and TLS. Any other option is ignored, as grpcio ignores the many that only
80
- clients read. That covers the limits on client pings, which are accepted but
81
- not enforced, and `grpc.http2.max_frame_size`, which grpc-core's flow control
82
- overrides anyway. Values that aren't integers are ignored too, as in grpcio.
77
+ `grpc.max_connection_idle_ms`), `grpc.max_allowed_incoming_connections` and
78
+ response compression, as do `maximum_concurrent_rpcs` and TLS. Any other
79
+ option is ignored, as grpcio ignores the many that only clients read. That
80
+ covers the limits on client pings, which are accepted but not enforced, and
81
+ `grpc.http2.max_frame_size`, which grpc-core's flow control overrides anyway.
82
+ Values that aren't integers are ignored too, as in grpcio.
83
83
 
84
84
  There are two known differences. If a unary handler calls
85
85
  `send_initial_metadata()` and then fails, the metadata goes out with the
@@ -153,9 +153,9 @@ this README. Changes are listed in [CHANGELOG.md](CHANGELOG.md).
153
153
  `aio.server()` run grpcio interceptors instead.
154
154
  - Like grpc-core, binding a Unix socket path replaces any socket file
155
155
  already there.
156
- - The server never sets `SO_REUSEPORT`, so binding a port another server is
157
- using fails, where grpcio's default lets both bind it. `grpc.so_reuseport`
158
- set to 0 is accepted; other integers raise an error.
156
+ - The server sets `SO_REUSEPORT` only when `grpc.so_reuseport` is 1. grpcio
157
+ sets it by default, so binding a port another server already uses succeeds
158
+ with grpcio but fails here.
159
159
  - Per connection: at most 1024 concurrent streams unless
160
160
  `grpc.max_concurrent_streams` says otherwise. A connection that hasn't
161
161
  finished the HTTP/2 preface after 120 s is dropped.
@@ -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)`
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)`
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`
@@ -118,7 +118,9 @@ flight; `server()` and `aio.server()` default to that.
118
118
  `max_concurrent_streams` limits the calls one connection can have open; a
119
119
  client waits for one to finish before starting another. `max_metadata_size`
120
120
  is the most request metadata, in bytes, a call may carry, counted as HTTP/2
121
- counts a header list; a call with more is refused.
121
+ counts a header list; a call with more is refused. With `reuse_port=True`,
122
+ several processes can bind the same TCP port; on Linux the kernel spreads
123
+ connections across them.
122
124
 
123
125
  `set_fallback` lets one port serve methods you don't list: run a regular
124
126
  `grpc.Server` on a Unix socket and its methods are proxied there, with its
@@ -37,6 +37,7 @@ class NativeGrpcFront:
37
37
  keepalive_permit_without_calls: bool = True,
38
38
  max_concurrent_streams: int = 1024,
39
39
  max_metadata_size: int = 16384,
40
+ reuse_port: bool = False,
40
41
  ) -> None: ...
41
42
  @property
42
43
  def bound_port(self) -> int: ...
@@ -47,7 +47,6 @@ _UNSUPPORTED_OPTIONS = frozenset(
47
47
  "grpc.max_connection_age_grace_ms",
48
48
  "grpc.max_connection_age_ms",
49
49
  "grpc.max_connection_idle_ms",
50
- "grpc.so_reuseport",
51
50
  }
52
51
  )
53
52
  # grpc-core's limit on request metadata when no option sets it.
@@ -98,6 +97,9 @@ def _parse_options(
98
97
  elif key == "grpc.absolute_max_metadata_size":
99
98
  if integer:
100
99
  hard_metadata = value
100
+ elif key == "grpc.so_reuseport":
101
+ if integer:
102
+ front["reuse_port"] = bool(value)
101
103
  elif key in _UNSUPPORTED_OPTIONS and integer and value:
102
104
  raise ValueError(f"unsupported server option {key!r}")
103
105
  if hard_metadata >= 0 or soft_metadata >= 0:
@@ -89,6 +89,9 @@ struct Running {
89
89
  /// max_metadata_size: The most request metadata, in bytes, a call may
90
90
  /// carry; a call with more is refused. Counted as HTTP/2 counts a
91
91
  /// header list, as grpc-core does.
92
+ /// reuse_port: Set SO_REUSEPORT on TCP listeners, so several processes
93
+ /// can serve one port. On Linux the kernel spreads connections
94
+ /// across them. Ignored for Unix sockets.
92
95
  ///
93
96
  /// Raises:
94
97
  /// OSError: If the address cannot be bound.
@@ -122,6 +125,7 @@ impl NativeGrpcFront {
122
125
  keepalive_permit_without_calls=true,
123
126
  max_concurrent_streams=1024,
124
127
  max_metadata_size=16384,
128
+ reuse_port=false,
125
129
  ))]
126
130
  // Python keyword arguments.
127
131
  #[allow(clippy::too_many_arguments)]
@@ -140,6 +144,7 @@ impl NativeGrpcFront {
140
144
  keepalive_permit_without_calls: bool,
141
145
  max_concurrent_streams: u32,
142
146
  max_metadata_size: u32,
147
+ reuse_port: bool,
143
148
  ) -> PyResult<Self> {
144
149
  if io_threads == 0 {
145
150
  return Err(PyValueError::new_err("io_threads must be greater than 0"));
@@ -187,7 +192,7 @@ impl NativeGrpcFront {
187
192
  PyOSError::new_err((err.raw_os_error().unwrap_or(0), format!("{target}: {err}")))
188
193
  };
189
194
  // Bind before starting any thread, so a failed bind leaves none behind.
190
- let bound = bind(target).map_err(os_error)?;
195
+ let bound = bind(target, reuse_port).map_err(os_error)?;
191
196
  let runtime = tokio::runtime::Builder::new_multi_thread()
192
197
  .worker_threads(io_threads)
193
198
  .thread_name("grpc-native-io")
@@ -712,7 +717,7 @@ struct BoundSockets {
712
717
  socket_path: Option<PathBuf>,
713
718
  }
714
719
 
715
- fn bind(target: &str) -> std::io::Result<BoundSockets> {
720
+ fn bind(target: &str, reuse_port: bool) -> std::io::Result<BoundSockets> {
716
721
  let unix_path = target
717
722
  .strip_prefix("unix://")
718
723
  .or_else(|| target.strip_prefix("unix:"));
@@ -732,8 +737,8 @@ fn bind(target: &str) -> std::io::Result<BoundSockets> {
732
737
  });
733
738
  }
734
739
  // Like grpc-core, bind every address the host resolves to (localhost is
735
- // both 127.0.0.1 and ::1), all on the port the first one got. std's bind
736
- // never sets SO_REUSEPORT, so a port already in use fails.
740
+ // both 127.0.0.1 and ::1), all on the port the first one got. Without
741
+ // reuse_port, a port already in use fails.
737
742
  let mut addrs: Vec<std::net::SocketAddr> = Vec::new();
738
743
  for addr in target.to_socket_addrs()? {
739
744
  if !addrs.contains(&addr) {
@@ -747,7 +752,7 @@ fn bind(target: &str) -> std::io::Result<BoundSockets> {
747
752
  if port != 0 {
748
753
  addr.set_port(port);
749
754
  }
750
- match std::net::TcpListener::bind(addr) {
755
+ match bind_tcp(addr, reuse_port) {
751
756
  Ok(listener) => {
752
757
  listener.set_nonblocking(true)?;
753
758
  port = listener.local_addr()?.port();
@@ -771,6 +776,29 @@ fn bind(target: &str) -> std::io::Result<BoundSockets> {
771
776
  })
772
777
  }
773
778
 
779
+ // std's backlog.
780
+ const LISTEN_BACKLOG: i32 = 128;
781
+
782
+ fn bind_tcp(
783
+ addr: std::net::SocketAddr,
784
+ reuse_port: bool,
785
+ ) -> std::io::Result<std::net::TcpListener> {
786
+ if !reuse_port {
787
+ return std::net::TcpListener::bind(addr);
788
+ }
789
+ // SO_REUSEPORT has to be set before bind, which std doesn't allow.
790
+ let socket = socket2::Socket::new(
791
+ socket2::Domain::for_address(addr),
792
+ socket2::Type::STREAM,
793
+ Some(socket2::Protocol::TCP),
794
+ )?;
795
+ socket.set_reuse_address(true)?;
796
+ socket.set_reuse_port(true)?;
797
+ socket.bind(&addr.into())?;
798
+ socket.listen(LISTEN_BACKLOG)?;
799
+ Ok(socket.into())
800
+ }
801
+
774
802
  #[pymodule]
775
803
  fn _native(m: &Bound<'_, PyModule>) -> PyResult<()> {
776
804
  let _ = pyo3_log::try_init();
@@ -274,12 +274,11 @@ def test_options_grpc_core_ignores_are_accepted() -> None:
274
274
  peer.close()
275
275
  finally:
276
276
  server.stop(None)
277
+ # Unlike grpcio, the server only sets SO_REUSEPORT when asked to.
278
+ assert not _bind_twice([])
277
279
  assert not _bind_twice([("grpc.so_reuseport", 0)])
278
280
  assert not _bind_twice([("grpc.so_reuseport", "0")])
279
- with pytest.raises(ValueError, match="unsupported server option"):
280
- pygrpc_native.server(
281
- futures.ThreadPoolExecutor(1), options=[("grpc.so_reuseport", 1)]
282
- )
281
+ assert _bind_twice([("grpc.so_reuseport", 1)])
283
282
 
284
283
 
285
284
  def test_grpcio_ignores_max_message_length() -> None:
File without changes
File without changes
File without changes
File without changes