Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/grpc/aio/_base_server.py: 95%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright 2020 The gRPC Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Abstract base classes for server-side classes."""
16import abc
17from typing import Generic, Iterable, Mapping, NoReturn, Optional, Sequence
19import grpc
21from ._metadata import Metadata
22from ._typing import DoneCallbackType
23from ._typing import MetadataType
24from ._typing import RequestType
25from ._typing import ResponseType
28class Server(abc.ABC):
29 """Serves RPCs."""
31 @abc.abstractmethod
32 def add_generic_rpc_handlers(
33 self, generic_rpc_handlers: Sequence[grpc.GenericRpcHandler]
34 ) -> None:
35 """Registers GenericRpcHandlers with this Server.
37 This method is only safe to call before the server is started.
39 Args:
40 generic_rpc_handlers: A sequence of GenericRpcHandlers that will be
41 used to service RPCs.
42 """
44 @abc.abstractmethod
45 def add_insecure_port(self, address: str) -> int:
46 """Opens an insecure port for accepting RPCs.
48 A port is a communication endpoint that used by networking protocols,
49 like TCP and UDP. To date, we only support TCP.
51 This method may only be called before starting the server.
53 Args:
54 address: The address for which to open a port. If the port is 0,
55 or not specified in the address, then the gRPC runtime will choose a port.
57 Returns:
58 An integer port on which the server will accept RPC requests.
59 """
61 @abc.abstractmethod
62 def add_secure_port(
63 self, address: str, server_credentials: grpc.ServerCredentials
64 ) -> int:
65 """Opens a secure port for accepting RPCs.
67 A port is a communication endpoint that used by networking protocols,
68 like TCP and UDP. To date, we only support TCP.
70 This method may only be called before starting the server.
72 Args:
73 address: The address for which to open a port.
74 if the port is 0, or not specified in the address, then the gRPC
75 runtime will choose a port.
76 server_credentials: A ServerCredentials object.
78 Returns:
79 An integer port on which the server will accept RPC requests.
80 """
82 @abc.abstractmethod
83 async def start(self) -> None:
84 """Starts this Server.
86 This method may only be called once. (i.e. it is not idempotent).
87 """
89 @abc.abstractmethod
90 async def stop(self, grace: Optional[float]) -> None:
91 """Stops this Server.
93 This method immediately stops the server from servicing new RPCs in
94 all cases.
96 If a grace period is specified, this method waits until all active
97 RPCs are finished or until the grace period is reached. RPCs that haven't
98 been terminated within the grace period are aborted.
99 If a grace period is not specified (by passing None for grace), all
100 existing RPCs are aborted immediately and this method blocks until
101 the last RPC handler terminates.
103 This method is idempotent and may be called at any time. Passing a
104 smaller grace value in a subsequent call will have the effect of
105 stopping the Server sooner (passing None will have the effect of
106 stopping the server immediately). Passing a larger grace value in a
107 subsequent call will not have the effect of stopping the server later
108 (i.e. the most restrictive grace value is used).
110 Args:
111 grace: A duration of time in seconds or None.
112 """
114 @abc.abstractmethod
115 async def wait_for_termination(
116 self, timeout: Optional[float] = None
117 ) -> bool:
118 """Continues current coroutine once the server stops.
120 This is an EXPERIMENTAL API.
122 The wait will not consume computational resources during blocking, and
123 it will block until one of the two following conditions are met:
125 1) The server is stopped or terminated;
126 2) A timeout occurs if timeout is not `None`.
128 The timeout argument works in the same way as `threading.Event.wait()`.
129 https://docs.python.org/3/library/threading.html#threading.Event.wait
131 Args:
132 timeout: A floating point number specifying a timeout for the
133 operation in seconds.
135 Returns:
136 A bool indicates if the operation times out.
137 """
140# pylint: disable=too-many-public-methods
141class ServicerContext(Generic[RequestType, ResponseType], abc.ABC):
142 """A context object passed to method implementations."""
144 @abc.abstractmethod
145 async def read(self) -> RequestType:
146 """Reads one message from the RPC.
148 Only one read operation is allowed simultaneously.
150 Returns:
151 A response message of the RPC.
153 Raises:
154 An RpcError exception if the read failed.
155 """
157 @abc.abstractmethod
158 async def write(self, message: ResponseType) -> None:
159 """Writes one message to the RPC.
161 Only one write operation is allowed simultaneously.
163 Raises:
164 An RpcError exception if the write failed.
165 """
167 @abc.abstractmethod
168 async def send_initial_metadata(
169 self, initial_metadata: MetadataType
170 ) -> None:
171 """Sends the initial metadata value to the client.
173 This method need not be called by implementations if they have no
174 metadata to add to what the gRPC runtime will transmit.
176 Args:
177 initial_metadata: The initial :term:`metadata`.
178 """
180 @abc.abstractmethod
181 async def abort(
182 self,
183 code: grpc.StatusCode,
184 details: str = "",
185 trailing_metadata: MetadataType = tuple(),
186 ) -> NoReturn:
187 """Raises an exception to terminate the RPC with a non-OK status.
189 The code and details passed as arguments will supercede any existing
190 ones.
192 Args:
193 code: A StatusCode object to be sent to the client.
194 It must not be StatusCode.OK.
195 details: A UTF-8-encodable string to be sent to the client upon
196 termination of the RPC.
197 trailing_metadata: A sequence of tuple represents the trailing
198 :term:`metadata`.
200 Raises:
201 Exception: An exception is always raised to signal the abortion the
202 RPC to the gRPC runtime.
203 """
205 @abc.abstractmethod
206 def set_trailing_metadata(self, trailing_metadata: MetadataType) -> None:
207 """Sends the trailing metadata for the RPC.
209 This method need not be called by implementations if they have no
210 metadata to add to what the gRPC runtime will transmit.
212 Args:
213 trailing_metadata: The trailing :term:`metadata`.
214 """
216 @abc.abstractmethod
217 def invocation_metadata(self) -> Optional[Metadata]:
218 """Accesses the metadata sent by the client.
220 Returns:
221 The invocation :term:`metadata`.
222 """
224 @abc.abstractmethod
225 def set_code(self, code: grpc.StatusCode) -> None:
226 """Sets the value to be used as status code upon RPC completion.
228 This method need not be called by method implementations if they wish
229 the gRPC runtime to determine the status code of the RPC.
231 Args:
232 code: A StatusCode object to be sent to the client.
233 """
235 @abc.abstractmethod
236 def set_details(self, details: str) -> None:
237 """Sets the value to be used the as detail string upon RPC completion.
239 This method need not be called by method implementations if they have
240 no details to transmit.
242 Args:
243 details: A UTF-8-encodable string to be sent to the client upon
244 termination of the RPC.
245 """
247 @abc.abstractmethod
248 def set_compression(self, compression: grpc.Compression) -> None:
249 """Set the compression algorithm to be used for the entire call.
251 Args:
252 compression: An element of grpc.compression, e.g.
253 grpc.compression.Gzip.
254 """
256 @abc.abstractmethod
257 def disable_next_message_compression(self) -> None:
258 """Disables compression for the next response message.
260 This method will override any compression configuration set during
261 server creation or set on the call.
262 """
264 @abc.abstractmethod
265 def peer(self) -> str:
266 """Identifies the peer that invoked the RPC being serviced.
268 Returns:
269 A string identifying the peer that invoked the RPC being serviced.
270 The string format is determined by gRPC runtime.
271 """
273 @abc.abstractmethod
274 def peer_identities(self) -> Optional[Iterable[bytes]]:
275 """Gets one or more peer identity(s).
277 Equivalent to
278 servicer_context.auth_context().get(servicer_context.peer_identity_key())
280 Returns:
281 An iterable of the identities, or None if the call is not
282 authenticated. Each identity is returned as a raw bytes type.
283 """
285 @abc.abstractmethod
286 def peer_identity_key(self) -> Optional[str]:
287 """The auth property used to identify the peer.
289 For example, "x509_common_name" or "x509_subject_alternative_name" are
290 used to identify an SSL peer.
292 Returns:
293 The auth property (string) that indicates the
294 peer identity, or None if the call is not authenticated.
295 """
297 @abc.abstractmethod
298 def auth_context(self) -> Mapping[str, Iterable[bytes]]:
299 """Gets the auth context for the call.
301 Returns:
302 A map of strings to an iterable of bytes for each auth property.
303 """
305 def time_remaining(self) -> float:
306 """Describes the length of allowed time remaining for the RPC.
308 Returns:
309 A nonnegative float indicating the length of allowed time in seconds
310 remaining for the RPC to complete before it is considered to have
311 timed out, or None if no deadline was specified for the RPC.
312 """
314 def trailing_metadata(self):
315 """Access value to be used as trailing metadata upon RPC completion.
317 This is an EXPERIMENTAL API.
319 Returns:
320 The trailing :term:`metadata` for the RPC.
321 """
322 raise NotImplementedError()
324 def code(self):
325 """Accesses the value to be used as status code upon RPC completion.
327 This is an EXPERIMENTAL API.
329 Returns:
330 The StatusCode value for the RPC.
331 """
332 raise NotImplementedError()
334 def details(self):
335 """Accesses the value to be used as detail string upon RPC completion.
337 This is an EXPERIMENTAL API.
339 Returns:
340 The details string of the RPC.
341 """
342 raise NotImplementedError()
344 def add_done_callback(self, callback: DoneCallbackType) -> None:
345 """Registers a callback to be called on RPC termination.
347 This is an EXPERIMENTAL API.
349 Args:
350 callback: A callable object will be called with the servicer context
351 object as its only argument.
352 """
354 def cancelled(self) -> bool:
355 """Return True if the RPC is cancelled.
357 The RPC is cancelled when the cancellation was requested with cancel().
359 This is an EXPERIMENTAL API.
361 Returns:
362 A bool indicates whether the RPC is cancelled or not.
363 """
365 def done(self) -> bool:
366 """Return True if the RPC is done.
368 An RPC is done if the RPC is completed, cancelled or aborted.
370 This is an EXPERIMENTAL API.
372 Returns:
373 A bool indicates if the RPC is done.
374 """