API Reference¶
Auto-generated from source using mkdocstrings.
grpc_server_kit¶
grpc-server-kit — batteries-optional async gRPC server toolkit.
GrpcApp
¶
Async gRPC application: configuration, servicers, and lifecycle in one place.
The app is configured with plain method calls, then :meth:run builds the
server, binds the port (TLS-aware), installs signal handlers, and serves
until termination or :meth:request_shutdown.
A GrpcApp instance is single-use: once its server has run and stopped
(via :meth:run or the async context manager), create a new instance to
serve again — gRPC servers cannot be restarted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
GrpcServerSettingsProtocol | None
|
Full server settings (anything satisfying
|
None
|
host
|
str | None
|
Bind host shortcut (only when |
None
|
port
|
int | None
|
Bind port shortcut (only when |
None
|
interceptors
|
Sequence[ServerInterceptor] | None
|
Initial interceptor chain (outermost first). |
None
|
Source code in grpc_server_kit/aio/app.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | |
bound_port
property
¶
The actually bound port (useful with port=0).
server
property
¶
The built server (available after :meth:build / :meth:run).
settings
property
¶
The settings this app was configured with.
__aenter__()
async
¶
Start the server without signal handling (embedding/tests).
Source code in grpc_server_kit/aio/app.py
__aexit__(exc_type, exc_val, exc_tb)
async
¶
Stop the server with the configured grace period.
Source code in grpc_server_kit/aio/app.py
add_interceptors(interceptors)
¶
Append interceptors to the chain (outermost first).
add_servicer(servicer, add_to_server)
¶
Add a servicer via its generated add_*Servicer_to_server function.
Example::
app.add_servicer(MyServicer(), add_MyServiceServicer_to_server)
Source code in grpc_server_kit/aio/app.py
build()
¶
Build the server, register servicers/health, and bind the port.
Idempotent: subsequent calls return the already-built server. Nothing is cached on failure, so a failed build (e.g. a busy port or a bad TLS file) can simply be retried after fixing the cause.
Source code in grpc_server_kit/aio/app.py
enable_channelz()
¶
enable_health(checkers=None, *, cache_ttl=_UNSET, check_timeout=_UNSET, service_names=None)
¶
Enable the gRPC Health Checking Protocol v1 servicer.
Requires the [health] extra. When cache_ttl / check_timeout
are not passed explicitly, they are read from settings.health if the
app's settings object carries a health block (see
:class:~grpc_server_kit.settings.BaseHealthSettings), else from the
kit defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkers
|
Sequence[AsyncHealthChecker] | None
|
Dependency checkers ( |
None
|
cache_ttl
|
float | None
|
TTL for cached check results in seconds. |
_UNSET
|
check_timeout
|
float
|
Timeout for one health check run in seconds. |
_UNSET
|
service_names
|
Sequence[str] | None
|
Additional service names to report health for. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in grpc_server_kit/aio/app.py
enable_reflection(service_names)
¶
Enable server reflection for the given fully-qualified service names.
Requires the [reflection] extra.
Source code in grpc_server_kit/aio/app.py
register(callback)
¶
Add a registration callback receiving the raw grpc.aio.Server.
Escape hatch for registrations that need the server object directly (generic handlers, third-party integrations).
Source code in grpc_server_kit/aio/app.py
request_shutdown(reason='requested')
¶
Request a graceful stop of this app.
Idempotent and safe at any point of the lifecycle — stopping something that is not running is not an error, and any "is it still running?" check by the caller would be racy anyway (the server can terminate on its own between the check and the call):
- while serving: the :meth:
runloop drains and returns; - before :meth:
run: the request is remembered and honored as soon as the server starts, so a signal racing startup is never lost; - after the app has stopped: no-op.
Source code in grpc_server_kit/aio/app.py
run(*, setup_signals=True)
async
¶
Build (if needed) and serve until termination, signal, or shutdown request.
Source code in grpc_server_kit/aio/app.py
run_sync(*, setup_signals=True)
¶
GrpcAsyncServerProtocol
¶
Bases: Protocol
Protocol for an asynchronous gRPC server.
Source code in grpc_server_kit/protocols.py
GrpcServerConfig
dataclass
¶
gRPC server configuration with production-grade defaults (stdlib only).
Source code in grpc_server_kit/config.py
GrpcServerProtocol
¶
Bases: Protocol
Protocol for a synchronous gRPC server.
Source code in grpc_server_kit/protocols.py
GrpcServerSettingsProtocol
¶
Bases: GrpcSettingsProtocol, GrpcSslSettingsProtocol, Protocol
Protocol for the full gRPC server settings.
Source code in grpc_server_kit/protocols.py
GrpcSettingsProtocol
¶
Bases: Protocol
Protocol for gRPC channel-tuning settings.
Source code in grpc_server_kit/protocols.py
GrpcSslSettingsProtocol
¶
Bases: Protocol
Protocol for gRPC SSL/TLS settings.
Source code in grpc_server_kit/protocols.py
bind_server_port(server, settings)
¶
Bind gRPC server to the configured port with or without TLS.
Compatible with both sync and async gRPC servers.
Note
If settings.port is 0, the OS will choose an available ephemeral port. The actual port bound is returned by this function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
GrpcServerProtocol | GrpcAsyncServerProtocol
|
gRPC server instance (sync or async) |
required |
settings
|
GrpcServerSettingsProtocol
|
Server settings including host, port, and SSL configuration |
required |
Returns:
| Type | Description |
|---|---|
int
|
The actual bound port number. |
Source code in grpc_server_kit/server.py
build_grpc_options(settings)
¶
Build gRPC server options from settings.
Settings fields set to None fall back to the kit's defaults; invalid
values fail loudly — a misconfigured server must not start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
GrpcSettingsProtocol
|
gRPC configuration settings following GrpcSettingsProtocol |
required |
Returns:
| Type | Description |
|---|---|
GrpcOptions
|
List of tuples containing gRPC channel options |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any option value is negative or the compression algorithm is not supported. |
Source code in grpc_server_kit/options.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
load_server_credentials(settings, strict=True)
¶
Load TLS credentials for secure gRPC server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
GrpcSslSettingsProtocol
|
SSL/TLS configuration protocol |
required |
strict
|
bool
|
If True (default), fail on insecure file permissions (Unix only) |
True
|
Returns:
| Type | Description |
|---|---|
ServerCredentials
|
Configured gRPC ServerCredentials |
Raises:
| Type | Description |
|---|---|
ValueError
|
If configuration is invalid or files are too large/invalid |
FileNotFoundError
|
If certificate/key files are missing |
PermissionError
|
If files cannot be accessed due to permissions |
OSError
|
If other I/O errors occur |
Source code in grpc_server_kit/credentials.py
setup_signal_handlers(shutdown_callback)
¶
Setup signal handlers using the default manager.