_server.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # Copyright 2019 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. """Server-side implementation of gRPC Asyncio Python."""
  15. from concurrent.futures import Executor
  16. from typing import Any, Dict, Optional, Sequence
  17. import grpc
  18. from grpc import _common
  19. from grpc import _compression
  20. from grpc._cython import cygrpc
  21. from . import _base_server
  22. from ._interceptor import ServerInterceptor
  23. from ._typing import ChannelArgumentType
  24. def _augment_channel_arguments(
  25. base_options: ChannelArgumentType, compression: Optional[grpc.Compression]
  26. ):
  27. compression_option = _compression.create_channel_option(compression)
  28. return tuple(base_options) + compression_option
  29. class Server(_base_server.Server):
  30. """Serves RPCs."""
  31. def __init__(
  32. self,
  33. thread_pool: Optional[Executor],
  34. generic_handlers: Optional[Sequence[grpc.GenericRpcHandler]],
  35. interceptors: Optional[Sequence[Any]],
  36. options: ChannelArgumentType,
  37. maximum_concurrent_rpcs: Optional[int],
  38. compression: Optional[grpc.Compression],
  39. ):
  40. self._loop = cygrpc.get_working_loop()
  41. if interceptors:
  42. invalid_interceptors = [
  43. interceptor
  44. for interceptor in interceptors
  45. if not isinstance(interceptor, ServerInterceptor)
  46. ]
  47. if invalid_interceptors:
  48. raise ValueError(
  49. "Interceptor must be ServerInterceptor, the "
  50. f"following are invalid: {invalid_interceptors}"
  51. )
  52. self._server = cygrpc.AioServer(
  53. self._loop,
  54. thread_pool,
  55. generic_handlers,
  56. interceptors,
  57. _augment_channel_arguments(options, compression),
  58. maximum_concurrent_rpcs,
  59. )
  60. def add_generic_rpc_handlers(
  61. self, generic_rpc_handlers: Sequence[grpc.GenericRpcHandler]
  62. ) -> None:
  63. """Registers GenericRpcHandlers with this Server.
  64. This method is only safe to call before the server is started.
  65. Args:
  66. generic_rpc_handlers: A sequence of GenericRpcHandlers that will be
  67. used to service RPCs.
  68. """
  69. self._server.add_generic_rpc_handlers(generic_rpc_handlers)
  70. def add_registered_method_handlers(
  71. self,
  72. service_name: str,
  73. method_handlers: Dict[str, grpc.RpcMethodHandler],
  74. ) -> None:
  75. # TODO(xuanwn): Implement this for AsyncIO.
  76. pass
  77. def add_insecure_port(self, address: str) -> int:
  78. """Opens an insecure port for accepting RPCs.
  79. This method may only be called before starting the server.
  80. Args:
  81. address: The address for which to open a port. If the port is 0,
  82. or not specified in the address, then the gRPC runtime will choose a port.
  83. Returns:
  84. An integer port on which the server will accept RPC requests.
  85. """
  86. return _common.validate_port_binding_result(
  87. address, self._server.add_insecure_port(_common.encode(address))
  88. )
  89. def add_secure_port(
  90. self, address: str, server_credentials: grpc.ServerCredentials
  91. ) -> int:
  92. """Opens a secure port for accepting RPCs.
  93. This method may only be called before starting the server.
  94. Args:
  95. address: The address for which to open a port.
  96. if the port is 0, or not specified in the address, then the gRPC
  97. runtime will choose a port.
  98. server_credentials: A ServerCredentials object.
  99. Returns:
  100. An integer port on which the server will accept RPC requests.
  101. """
  102. return _common.validate_port_binding_result(
  103. address,
  104. self._server.add_secure_port(
  105. _common.encode(address), server_credentials
  106. ),
  107. )
  108. async def start(self) -> None:
  109. """Starts this Server.
  110. This method may only be called once. (i.e. it is not idempotent).
  111. """
  112. await self._server.start()
  113. async def stop(self, grace: Optional[float]) -> None:
  114. """Stops this Server.
  115. This method immediately stops the server from servicing new RPCs in
  116. all cases.
  117. If a grace period is specified, this method waits until all active
  118. RPCs are finished or until the grace period is reached. RPCs that haven't
  119. been terminated within the grace period are aborted.
  120. If a grace period is not specified (by passing None for grace), all
  121. existing RPCs are aborted immediately and this method blocks until
  122. the last RPC handler terminates.
  123. This method is idempotent and may be called at any time. Passing a
  124. smaller grace value in a subsequent call will have the effect of
  125. stopping the Server sooner (passing None will have the effect of
  126. stopping the server immediately). Passing a larger grace value in a
  127. subsequent call will not have the effect of stopping the server later
  128. (i.e. the most restrictive grace value is used).
  129. Args:
  130. grace: A duration of time in seconds or None.
  131. """
  132. await self._server.shutdown(grace)
  133. async def wait_for_termination(
  134. self, timeout: Optional[float] = None
  135. ) -> bool:
  136. """Block current coroutine until the server stops.
  137. This is an EXPERIMENTAL API.
  138. The wait will not consume computational resources during blocking, and
  139. it will block until one of the two following conditions are met:
  140. 1) The server is stopped or terminated;
  141. 2) A timeout occurs if timeout is not `None`.
  142. The timeout argument works in the same way as `threading.Event.wait()`.
  143. https://docs.python.org/3/library/threading.html#threading.Event.wait
  144. Args:
  145. timeout: A floating point number specifying a timeout for the
  146. operation in seconds.
  147. Returns:
  148. A bool indicates if the operation times out.
  149. """
  150. return await self._server.wait_for_termination(timeout)
  151. def __del__(self):
  152. """Schedules a graceful shutdown in current event loop.
  153. The Cython AioServer doesn't hold a ref-count to this class. It should
  154. be safe to slightly extend the underlying Cython object's life span.
  155. """
  156. if hasattr(self, "_server"):
  157. if self._server.is_running():
  158. cygrpc.schedule_coro_threadsafe(
  159. self._server.shutdown(None),
  160. self._loop,
  161. )
  162. def server(
  163. migration_thread_pool: Optional[Executor] = None,
  164. handlers: Optional[Sequence[grpc.GenericRpcHandler]] = None,
  165. interceptors: Optional[Sequence[Any]] = None,
  166. options: Optional[ChannelArgumentType] = None,
  167. maximum_concurrent_rpcs: Optional[int] = None,
  168. compression: Optional[grpc.Compression] = None,
  169. ):
  170. """Creates a Server with which RPCs can be serviced.
  171. Args:
  172. migration_thread_pool: A futures.ThreadPoolExecutor to be used by the
  173. Server to execute non-AsyncIO RPC handlers for migration purpose.
  174. handlers: An optional list of GenericRpcHandlers used for executing RPCs.
  175. More handlers may be added by calling add_generic_rpc_handlers any time
  176. before the server is started.
  177. interceptors: An optional list of ServerInterceptor objects that observe
  178. and optionally manipulate the incoming RPCs before handing them over to
  179. handlers. The interceptors are given control in the order they are
  180. specified. This is an EXPERIMENTAL API.
  181. options: An optional list of key-value pairs (:term:`channel_arguments` in gRPC runtime)
  182. to configure the channel.
  183. maximum_concurrent_rpcs: The maximum number of concurrent RPCs this server
  184. will service before returning RESOURCE_EXHAUSTED status, or None to
  185. indicate no limit.
  186. compression: An element of grpc.compression, e.g.
  187. grpc.compression.Gzip. This compression algorithm will be used for the
  188. lifetime of the server unless overridden by set_compression.
  189. Returns:
  190. A Server object.
  191. """
  192. return Server(
  193. migration_thread_pool,
  194. () if handlers is None else handlers,
  195. () if interceptors is None else interceptors,
  196. () if options is None else options,
  197. maximum_concurrent_rpcs,
  198. compression,
  199. )