File size: 9,170 Bytes
375a1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
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
# Copyright 2019 The gRPC Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Server-side implementation of gRPC Asyncio Python."""

from concurrent.futures import Executor
from typing import Any, Dict, Optional, Sequence

import grpc
from grpc import _common
from grpc import _compression
from grpc._cython import cygrpc

from . import _base_server
from ._interceptor import ServerInterceptor
from ._typing import ChannelArgumentType


def _augment_channel_arguments(

    base_options: ChannelArgumentType, compression: Optional[grpc.Compression]

):
    compression_option = _compression.create_channel_option(compression)
    return tuple(base_options) + compression_option


class Server(_base_server.Server):
    """Serves RPCs."""

    def __init__(

        self,

        thread_pool: Optional[Executor],

        generic_handlers: Optional[Sequence[grpc.GenericRpcHandler]],

        interceptors: Optional[Sequence[Any]],

        options: ChannelArgumentType,

        maximum_concurrent_rpcs: Optional[int],

        compression: Optional[grpc.Compression],

    ):
        self._loop = cygrpc.get_working_loop()
        if interceptors:
            invalid_interceptors = [
                interceptor
                for interceptor in interceptors
                if not isinstance(interceptor, ServerInterceptor)
            ]
            if invalid_interceptors:
                raise ValueError(
                    "Interceptor must be ServerInterceptor, the "
                    f"following are invalid: {invalid_interceptors}"
                )
        self._server = cygrpc.AioServer(
            self._loop,
            thread_pool,
            generic_handlers,
            interceptors,
            _augment_channel_arguments(options, compression),
            maximum_concurrent_rpcs,
        )

    def add_generic_rpc_handlers(

        self, generic_rpc_handlers: Sequence[grpc.GenericRpcHandler]

    ) -> None:
        """Registers GenericRpcHandlers with this Server.



        This method is only safe to call before the server is started.



        Args:

          generic_rpc_handlers: A sequence of GenericRpcHandlers that will be

          used to service RPCs.

        """
        self._server.add_generic_rpc_handlers(generic_rpc_handlers)

    def add_registered_method_handlers(

        self,

        service_name: str,

        method_handlers: Dict[str, grpc.RpcMethodHandler],

    ) -> None:
        # TODO(xuanwn): Implement this for AsyncIO.
        pass

    def add_insecure_port(self, address: str) -> int:
        """Opens an insecure port for accepting RPCs.



        This method may only be called before starting the server.



        Args:

          address: The address for which to open a port. If the port is 0,

            or not specified in the address, then the gRPC runtime will choose a port.



        Returns:

          An integer port on which the server will accept RPC requests.

        """
        return _common.validate_port_binding_result(
            address, self._server.add_insecure_port(_common.encode(address))
        )

    def add_secure_port(

        self, address: str, server_credentials: grpc.ServerCredentials

    ) -> int:
        """Opens a secure port for accepting RPCs.



        This method may only be called before starting the server.



        Args:

          address: The address for which to open a port.

            if the port is 0, or not specified in the address, then the gRPC

            runtime will choose a port.

          server_credentials: A ServerCredentials object.



        Returns:

          An integer port on which the server will accept RPC requests.

        """
        return _common.validate_port_binding_result(
            address,
            self._server.add_secure_port(
                _common.encode(address), server_credentials
            ),
        )

    async def start(self) -> None:
        """Starts this Server.



        This method may only be called once. (i.e. it is not idempotent).

        """
        await self._server.start()

    async def stop(self, grace: Optional[float]) -> None:
        """Stops this Server.



        This method immediately stops the server from servicing new RPCs in

        all cases.



        If a grace period is specified, this method waits until all active

        RPCs are finished or until the grace period is reached. RPCs that haven't

        been terminated within the grace period are aborted.

        If a grace period is not specified (by passing None for grace), all

        existing RPCs are aborted immediately and this method blocks until

        the last RPC handler terminates.



        This method is idempotent and may be called at any time. Passing a

        smaller grace value in a subsequent call will have the effect of

        stopping the Server sooner (passing None will have the effect of

        stopping the server immediately). Passing a larger grace value in a

        subsequent call will not have the effect of stopping the server later

        (i.e. the most restrictive grace value is used).



        Args:

          grace: A duration of time in seconds or None.

        """
        await self._server.shutdown(grace)

    async def wait_for_termination(

        self, timeout: Optional[float] = None

    ) -> bool:
        """Block current coroutine until the server stops.



        This is an EXPERIMENTAL API.



        The wait will not consume computational resources during blocking, and

        it will block until one of the two following conditions are met:



        1) The server is stopped or terminated;

        2) A timeout occurs if timeout is not `None`.



        The timeout argument works in the same way as `threading.Event.wait()`.

        https://docs.python.org/3/library/threading.html#threading.Event.wait



        Args:

          timeout: A floating point number specifying a timeout for the

            operation in seconds.



        Returns:

          A bool indicates if the operation times out.

        """
        return await self._server.wait_for_termination(timeout)

    def __del__(self):
        """Schedules a graceful shutdown in current event loop.



        The Cython AioServer doesn't hold a ref-count to this class. It should

        be safe to slightly extend the underlying Cython object's life span.

        """
        if hasattr(self, "_server"):
            if self._server.is_running():
                cygrpc.schedule_coro_threadsafe(
                    self._server.shutdown(None),
                    self._loop,
                )


def server(

    migration_thread_pool: Optional[Executor] = None,

    handlers: Optional[Sequence[grpc.GenericRpcHandler]] = None,

    interceptors: Optional[Sequence[Any]] = None,

    options: Optional[ChannelArgumentType] = None,

    maximum_concurrent_rpcs: Optional[int] = None,

    compression: Optional[grpc.Compression] = None,

):
    """Creates a Server with which RPCs can be serviced.



    Args:

      migration_thread_pool: A futures.ThreadPoolExecutor to be used by the

        Server to execute non-AsyncIO RPC handlers for migration purpose.

      handlers: An optional list of GenericRpcHandlers used for executing RPCs.

        More handlers may be added by calling add_generic_rpc_handlers any time

        before the server is started.

      interceptors: An optional list of ServerInterceptor objects that observe

        and optionally manipulate the incoming RPCs before handing them over to

        handlers. The interceptors are given control in the order they are

        specified. This is an EXPERIMENTAL API.

      options: An optional list of key-value pairs (:term:`channel_arguments` in gRPC runtime)

        to configure the channel.

      maximum_concurrent_rpcs: The maximum number of concurrent RPCs this server

        will service before returning RESOURCE_EXHAUSTED status, or None to

        indicate no limit.

      compression: An element of grpc.compression, e.g.

        grpc.compression.Gzip. This compression algorithm will be used for the

        lifetime of the server unless overridden by set_compression.



    Returns:

      A Server object.

    """
    return Server(
        migration_thread_pool,
        () if handlers is None else handlers,
        () if interceptors is None else interceptors,
        () if options is None else options,
        maximum_concurrent_rpcs,
        compression,
    )