Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: third_party/grpc/src/python/grpcio/grpc/framework/alpha/interfaces.py

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2015, Google Inc.
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 """Interfaces of GRPC."""
31
32 import abc
33 import enum
34
35 # exceptions is referenced from specification in this module.
36 from grpc.framework.alpha import exceptions # pylint: disable=unused-import
37 from grpc.framework.foundation import activated
38 from grpc.framework.foundation import future
39
40
41 @enum.unique
42 class Cardinality(enum.Enum):
43 """Constants for the four cardinalities of RPC."""
44
45 UNARY_UNARY = 'request-unary/response-unary'
46 UNARY_STREAM = 'request-unary/response-streaming'
47 STREAM_UNARY = 'request-streaming/response-unary'
48 STREAM_STREAM = 'request-streaming/response-streaming'
49
50
51 @enum.unique
52 class Abortion(enum.Enum):
53 """Categories of RPC abortion."""
54
55 CANCELLED = 'cancelled'
56 EXPIRED = 'expired'
57 NETWORK_FAILURE = 'network failure'
58 SERVICED_FAILURE = 'serviced failure'
59 SERVICER_FAILURE = 'servicer failure'
60
61
62 class CancellableIterator(object):
63 """Implements the Iterator protocol and affords a cancel method."""
64 __metaclass__ = abc.ABCMeta
65
66 @abc.abstractmethod
67 def __iter__(self):
68 """Returns the self object in accordance with the Iterator protocol."""
69 raise NotImplementedError()
70
71 @abc.abstractmethod
72 def next(self):
73 """Returns a value or raises StopIteration per the Iterator protocol."""
74 raise NotImplementedError()
75
76 @abc.abstractmethod
77 def cancel(self):
78 """Requests cancellation of whatever computation underlies this iterator."""
79 raise NotImplementedError()
80
81
82 class RpcContext(object):
83 """Provides RPC-related information and action."""
84 __metaclass__ = abc.ABCMeta
85
86 @abc.abstractmethod
87 def is_active(self):
88 """Describes whether the RPC is active or has terminated."""
89 raise NotImplementedError()
90
91 @abc.abstractmethod
92 def time_remaining(self):
93 """Describes the length of allowed time remaining for the RPC.
94 Returns:
95 A nonnegative float indicating the length of allowed time in seconds
96 remaining for the RPC to complete before it is considered to have timed
97 out.
98 """
99 raise NotImplementedError()
100
101 @abc.abstractmethod
102 def add_abortion_callback(self, abortion_callback):
103 """Registers a callback to be called if the RPC is aborted.
104 Args:
105 abortion_callback: A callable to be called and passed an Abortion value
106 in the event of RPC abortion.
107 """
108 raise NotImplementedError()
109
110
111 class UnaryUnarySyncAsync(object):
112 """Affords invoking a unary-unary RPC synchronously or asynchronously.
113 Values implementing this interface are directly callable and present an
114 "async" method. Both calls take a request value and a numeric timeout.
115 Direct invocation of a value of this type invokes its associated RPC and
116 blocks until the RPC's response is available. Calling the "async" method
117 of a value of this type invokes its associated RPC and immediately returns a
118 future.Future bound to the asynchronous execution of the RPC.
119 """
120 __metaclass__ = abc.ABCMeta
121
122 @abc.abstractmethod
123 def __call__(self, request, timeout):
124 """Synchronously invokes the underlying RPC.
125 Args:
126 request: The request value for the RPC.
127 timeout: A duration of time in seconds to allow for the RPC.
128 Returns:
129 The response value for the RPC.
130 Raises:
131 exceptions.RpcError: Indicating that the RPC was aborted.
132 """
133 raise NotImplementedError()
134
135 @abc.abstractmethod
136 def async(self, request, timeout):
137 """Asynchronously invokes the underlying RPC.
138 Args:
139 request: The request value for the RPC.
140 timeout: A duration of time in seconds to allow for the RPC.
141 Returns:
142 A future.Future representing the RPC. In the event of RPC completion, the
143 returned Future's result value will be the response value of the RPC.
144 In the event of RPC abortion, the returned Future's exception value
145 will be an exceptions.RpcError.
146 """
147 raise NotImplementedError()
148
149
150 class StreamUnarySyncAsync(object):
151 """Affords invoking a stream-unary RPC synchronously or asynchronously.
152 Values implementing this interface are directly callable and present an
153 "async" method. Both calls take an iterator of request values and a numeric
154 timeout. Direct invocation of a value of this type invokes its associated RPC
155 and blocks until the RPC's response is available. Calling the "async" method
156 of a value of this type invokes its associated RPC and immediately returns a
157 future.Future bound to the asynchronous execution of the RPC.
158 """
159 __metaclass__ = abc.ABCMeta
160
161 @abc.abstractmethod
162 def __call__(self, request_iterator, timeout):
163 """Synchronously invokes the underlying RPC.
164
165 Args:
166 request_iterator: An iterator that yields request values for the RPC.
167 timeout: A duration of time in seconds to allow for the RPC.
168
169 Returns:
170 The response value for the RPC.
171
172 Raises:
173 exceptions.RpcError: Indicating that the RPC was aborted.
174 """
175 raise NotImplementedError()
176
177 @abc.abstractmethod
178 def async(self, request_iterator, timeout):
179 """Asynchronously invokes the underlying RPC.
180
181 Args:
182 request_iterator: An iterator that yields request values for the RPC.
183 timeout: A duration of time in seconds to allow for the RPC.
184
185 Returns:
186 A future.Future representing the RPC. In the event of RPC completion, the
187 returned Future's result value will be the response value of the RPC.
188 In the event of RPC abortion, the returned Future's exception value
189 will be an exceptions.RpcError.
190 """
191 raise NotImplementedError()
192
193
194 class RpcMethodDescription(object):
195 """A type for the common aspects of RPC method descriptions."""
196 __metaclass__ = abc.ABCMeta
197
198 @abc.abstractmethod
199 def cardinality(self):
200 """Identifies the cardinality of this RpcMethodDescription.
201
202 Returns:
203 A Cardinality value identifying whether or not this
204 RpcMethodDescription is request-unary or request-streaming and
205 whether or not it is response-unary or response-streaming.
206 """
207 raise NotImplementedError()
208
209
210 class RpcMethodInvocationDescription(RpcMethodDescription):
211 """Invocation-side description of an RPC method."""
212 __metaclass__ = abc.ABCMeta
213
214 @abc.abstractmethod
215 def serialize_request(self, request):
216 """Serializes a request value.
217
218 Args:
219 request: A request value appropriate for the RPC method described by this
220 RpcMethodInvocationDescription.
221
222 Returns:
223 The serialization of the given request value as a
224 bytestring.
225 """
226 raise NotImplementedError()
227
228 @abc.abstractmethod
229 def deserialize_response(self, serialized_response):
230 """Deserializes a response value.
231
232 Args:
233 serialized_response: A bytestring that is the serialization of a response
234 value appropriate for the RPC method described by this
235 RpcMethodInvocationDescription.
236
237 Returns:
238 A response value corresponding to the given bytestring.
239 """
240 raise NotImplementedError()
241
242
243 class RpcMethodServiceDescription(RpcMethodDescription):
244 """Service-side description of an RPC method."""
245 __metaclass__ = abc.ABCMeta
246
247 @abc.abstractmethod
248 def deserialize_request(self, serialized_request):
249 """Deserializes a request value.
250
251 Args:
252 serialized_request: A bytestring that is the serialization of a request
253 value appropriate for the RPC method described by this
254 RpcMethodServiceDescription.
255
256 Returns:
257 A request value corresponding to the given bytestring.
258 """
259 raise NotImplementedError()
260
261 @abc.abstractmethod
262 def serialize_response(self, response):
263 """Serializes a response value.
264
265 Args:
266 response: A response value appropriate for the RPC method described by
267 this RpcMethodServiceDescription.
268
269 Returns:
270 The serialization of the given response value as a
271 bytestring.
272 """
273 raise NotImplementedError()
274
275 @abc.abstractmethod
276 def service_unary_unary(self, request, context):
277 """Carries out this RPC.
278
279 This method may only be called if the cardinality of this
280 RpcMethodServiceDescription is Cardinality.UNARY_UNARY.
281
282 Args:
283 request: A request value appropriate for the RPC method described by this
284 RpcMethodServiceDescription.
285 context: An RpcContext object for the RPC.
286
287 Returns:
288 A response value appropriate for the RPC method described by this
289 RpcMethodServiceDescription.
290 """
291 raise NotImplementedError()
292
293 @abc.abstractmethod
294 def service_unary_stream(self, request, context):
295 """Carries out this RPC.
296
297 This method may only be called if the cardinality of this
298 RpcMethodServiceDescription is Cardinality.UNARY_STREAM.
299
300 Args:
301 request: A request value appropriate for the RPC method described by this
302 RpcMethodServiceDescription.
303 context: An RpcContext object for the RPC.
304
305 Yields:
306 Zero or more response values appropriate for the RPC method described by
307 this RpcMethodServiceDescription.
308 """
309 raise NotImplementedError()
310
311 @abc.abstractmethod
312 def service_stream_unary(self, request_iterator, context):
313 """Carries out this RPC.
314
315 This method may only be called if the cardinality of this
316 RpcMethodServiceDescription is Cardinality.STREAM_UNARY.
317
318 Args:
319 request_iterator: An iterator of request values appropriate for the RPC
320 method described by this RpcMethodServiceDescription.
321 context: An RpcContext object for the RPC.
322
323 Returns:
324 A response value appropriate for the RPC method described by this
325 RpcMethodServiceDescription.
326 """
327 raise NotImplementedError()
328
329 @abc.abstractmethod
330 def service_stream_stream(self, request_iterator, context):
331 """Carries out this RPC.
332
333 This method may only be called if the cardinality of this
334 RpcMethodServiceDescription is Cardinality.STREAM_STREAM.
335
336 Args:
337 request_iterator: An iterator of request values appropriate for the RPC
338 method described by this RpcMethodServiceDescription.
339 context: An RpcContext object for the RPC.
340
341 Yields:
342 Zero or more response values appropriate for the RPC method described by
343 this RpcMethodServiceDescription.
344 """
345 raise NotImplementedError()
346
347
348 class Stub(object):
349 """A stub with callable RPC method names for attributes.
350
351 Instances of this type are context managers and only afford RPC invocation
352 when used in context.
353
354 Instances of this type, when used in context, respond to attribute access
355 as follows: if the requested attribute is the name of a unary-unary RPC
356 method, the value of the attribute will be a UnaryUnarySyncAsync with which
357 to invoke the RPC method. If the requested attribute is the name of a
358 unary-stream RPC method, the value of the attribute will be a callable taking
359 a request object and a timeout parameter and returning a CancellableIterator
360 that yields the response values of the RPC. If the requested attribute is the
361 name of a stream-unary RPC method, the value of the attribute will be a
362 StreamUnarySyncAsync with which to invoke the RPC method. If the requested
363 attribute is the name of a stream-stream RPC method, the value of the
364 attribute will be a callable taking an iterator of request objects and a
365 timeout and returning a CancellableIterator that yields the response values
366 of the RPC.
367
368 In all cases indication of abortion is indicated by raising of
369 exceptions.RpcError, exceptions.CancellationError,
370 and exceptions.ExpirationError.
371 """
372 __metaclass__ = abc.ABCMeta
373
374
375 class Server(activated.Activated):
376 """A GRPC Server."""
377 __metaclass__ = abc.ABCMeta
378
379 @abc.abstractmethod
380 def port(self):
381 """Reports the port on which the server is serving.
382
383 This method may only be called while the server is activated.
384
385 Returns:
386 The port on which the server is serving.
387 """
388 raise NotImplementedError()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698