base.py
Go to the documentation of this file.
1 # Copyright 2015 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 """The base interface of RPC Framework.
15 
16 Implementations of this interface support the conduct of "operations":
17 exchanges between two distinct ends of an arbitrary number of data payloads
18 and metadata such as a name for the operation, initial and terminal metadata
19 in each direction, and flow control. These operations may be used for transfers
20 of data, remote procedure calls, status indication, or anything else
21 applications choose.
22 """
23 
24 # threading is referenced from specification in this module.
25 import abc
26 import enum
27 import threading # pylint: disable=unused-import
28 
29 import six
30 
31 # pylint: disable=too-many-arguments
32 
33 
34 class NoSuchMethodError(Exception):
35  """Indicates that an unrecognized operation has been called.
36 
37  Attributes:
38  code: A code value to communicate to the other side of the operation
39  along with indication of operation termination. May be None.
40  details: A details value to communicate to the other side of the
41  operation along with indication of operation termination. May be None.
42  """
43 
44  def __init__(self, code, details):
45  """Constructor.
46 
47  Args:
48  code: A code value to communicate to the other side of the operation
49  along with indication of operation termination. May be None.
50  details: A details value to communicate to the other side of the
51  operation along with indication of operation termination. May be None.
52  """
53  super(NoSuchMethodError, self).__init__()
54  self.code = code
55  self.details = details
56 
57 
58 class Outcome(object):
59  """The outcome of an operation.
60 
61  Attributes:
62  kind: A Kind value coarsely identifying how the operation terminated.
63  code: An application-specific code value or None if no such value was
64  provided.
65  details: An application-specific details value or None if no such value was
66  provided.
67  """
68 
69  @enum.unique
70  class Kind(enum.Enum):
71  """Ways in which an operation can terminate."""
72 
73  COMPLETED = 'completed'
74  CANCELLED = 'cancelled'
75  EXPIRED = 'expired'
76  LOCAL_SHUTDOWN = 'local shutdown'
77  REMOTE_SHUTDOWN = 'remote shutdown'
78  RECEPTION_FAILURE = 'reception failure'
79  TRANSMISSION_FAILURE = 'transmission failure'
80  LOCAL_FAILURE = 'local failure'
81  REMOTE_FAILURE = 'remote failure'
82 
83 
84 class Completion(six.with_metaclass(abc.ABCMeta)):
85  """An aggregate of the values exchanged upon operation completion.
86 
87  Attributes:
88  terminal_metadata: A terminal metadata value for the operaton.
89  code: A code value for the operation.
90  message: A message value for the operation.
91  """
92 
93 
94 class OperationContext(six.with_metaclass(abc.ABCMeta)):
95  """Provides operation-related information and action."""
96 
97  @abc.abstractmethod
98  def outcome(self):
99  """Indicates the operation's outcome (or that the operation is ongoing).
100 
101  Returns:
102  None if the operation is still active or the Outcome value for the
103  operation if it has terminated.
104  """
105  raise NotImplementedError()
106 
107  @abc.abstractmethod
108  def add_termination_callback(self, callback):
109  """Adds a function to be called upon operation termination.
110 
111  Args:
112  callback: A callable to be passed an Outcome value on operation
113  termination.
114 
115  Returns:
116  None if the operation has not yet terminated and the passed callback will
117  later be called when it does terminate, or if the operation has already
118  terminated an Outcome value describing the operation termination and the
119  passed callback will not be called as a result of this method call.
120  """
121  raise NotImplementedError()
122 
123  @abc.abstractmethod
124  def time_remaining(self):
125  """Describes the length of allowed time remaining for the operation.
126 
127  Returns:
128  A nonnegative float indicating the length of allowed time in seconds
129  remaining for the operation to complete before it is considered to have
130  timed out. Zero is returned if the operation has terminated.
131  """
132  raise NotImplementedError()
133 
134  @abc.abstractmethod
135  def cancel(self):
136  """Cancels the operation if the operation has not yet terminated."""
137  raise NotImplementedError()
138 
139  @abc.abstractmethod
140  def fail(self, exception):
141  """Indicates that the operation has failed.
142 
143  Args:
144  exception: An exception germane to the operation failure. May be None.
145  """
146  raise NotImplementedError()
147 
148 
149 class Operator(six.with_metaclass(abc.ABCMeta)):
150  """An interface through which to participate in an operation."""
151 
152  @abc.abstractmethod
153  def advance(self,
154  initial_metadata=None,
155  payload=None,
156  completion=None,
157  allowance=None):
158  """Progresses the operation.
159 
160  Args:
161  initial_metadata: An initial metadata value. Only one may ever be
162  communicated in each direction for an operation, and they must be
163  communicated no later than either the first payload or the completion.
164  payload: A payload value.
165  completion: A Completion value. May only ever be non-None once in either
166  direction, and no payloads may be passed after it has been communicated.
167  allowance: A positive integer communicating the number of additional
168  payloads allowed to be passed by the remote side of the operation.
169  """
170  raise NotImplementedError()
171 
172 
173 class ProtocolReceiver(six.with_metaclass(abc.ABCMeta)):
174  """A means of receiving protocol values during an operation."""
175 
176  @abc.abstractmethod
177  def context(self, protocol_context):
178  """Accepts the protocol context object for the operation.
179 
180  Args:
181  protocol_context: The protocol context object for the operation.
182  """
183  raise NotImplementedError()
184 
185 
186 class Subscription(six.with_metaclass(abc.ABCMeta)):
187  """Describes customer code's interest in values from the other side.
188 
189  Attributes:
190  kind: A Kind value describing the overall kind of this value.
191  termination_callback: A callable to be passed the Outcome associated with
192  the operation after it has terminated. Must be non-None if kind is
193  Kind.TERMINATION_ONLY. Must be None otherwise.
194  allowance: A callable behavior that accepts positive integers representing
195  the number of additional payloads allowed to be passed to the other side
196  of the operation. Must be None if kind is Kind.FULL. Must not be None
197  otherwise.
198  operator: An Operator to be passed values from the other side of the
199  operation. Must be non-None if kind is Kind.FULL. Must be None otherwise.
200  protocol_receiver: A ProtocolReceiver to be passed protocol objects as they
201  become available during the operation. Must be non-None if kind is
202  Kind.FULL.
203  """
204 
205  @enum.unique
206  class Kind(enum.Enum):
207 
208  NONE = 'none'
209  TERMINATION_ONLY = 'termination only'
210  FULL = 'full'
211 
212 
213 class Servicer(six.with_metaclass(abc.ABCMeta)):
214  """Interface for service implementations."""
215 
216  @abc.abstractmethod
217  def service(self, group, method, context, output_operator):
218  """Services an operation.
219 
220  Args:
221  group: The group identifier of the operation to be serviced.
222  method: The method identifier of the operation to be serviced.
223  context: An OperationContext object affording contextual information and
224  actions.
225  output_operator: An Operator that will accept output values of the
226  operation.
227 
228  Returns:
229  A Subscription via which this object may or may not accept more values of
230  the operation.
231 
232  Raises:
233  NoSuchMethodError: If this Servicer does not handle operations with the
234  given group and method.
235  abandonment.Abandoned: If the operation has been aborted and there no
236  longer is any reason to service the operation.
237  """
238  raise NotImplementedError()
239 
240 
241 class End(six.with_metaclass(abc.ABCMeta)):
242  """Common type for entry-point objects on both sides of an operation."""
243 
244  @abc.abstractmethod
245  def start(self):
246  """Starts this object's service of operations."""
247  raise NotImplementedError()
248 
249  @abc.abstractmethod
250  def stop(self, grace):
251  """Stops this object's service of operations.
252 
253  This object will refuse service of new operations as soon as this method is
254  called but operations under way at the time of the call may be given a
255  grace period during which they are allowed to finish.
256 
257  Args:
258  grace: A duration of time in seconds to allow ongoing operations to
259  terminate before being forcefully terminated by the stopping of this
260  End. May be zero to terminate all ongoing operations and immediately
261  stop.
262 
263  Returns:
264  A threading.Event that will be set to indicate all operations having
265  terminated and this End having completely stopped. The returned event
266  may not be set until after the full grace period (if some ongoing
267  operation continues for the full length of the period) or it may be set
268  much sooner (if for example this End had no operations in progress at
269  the time its stop method was called).
270  """
271  raise NotImplementedError()
272 
273  @abc.abstractmethod
274  def operate(self,
275  group,
276  method,
277  subscription,
278  timeout,
279  initial_metadata=None,
280  payload=None,
281  completion=None,
282  protocol_options=None):
283  """Commences an operation.
284 
285  Args:
286  group: The group identifier of the invoked operation.
287  method: The method identifier of the invoked operation.
288  subscription: A Subscription to which the results of the operation will be
289  passed.
290  timeout: A length of time in seconds to allow for the operation.
291  initial_metadata: An initial metadata value to be sent to the other side
292  of the operation. May be None if the initial metadata will be later
293  passed via the returned operator or if there will be no initial metadata
294  passed at all.
295  payload: An initial payload for the operation.
296  completion: A Completion value indicating the end of transmission to the
297  other side of the operation.
298  protocol_options: A value specified by the provider of a Base interface
299  implementation affording custom state and behavior.
300 
301  Returns:
302  A pair of objects affording information about the operation and action
303  continuing the operation. The first element of the returned pair is an
304  OperationContext for the operation and the second element of the
305  returned pair is an Operator to which operation values not passed in
306  this call should later be passed.
307  """
308  raise NotImplementedError()
309 
310  @abc.abstractmethod
311  def operation_stats(self):
312  """Reports the number of terminated operations broken down by outcome.
313 
314  Returns:
315  A dictionary from Outcome.Kind value to an integer identifying the number
316  of operations that terminated with that outcome kind.
317  """
318  raise NotImplementedError()
319 
320  @abc.abstractmethod
321  def add_idle_action(self, action):
322  """Adds an action to be called when this End has no ongoing operations.
323 
324  Args:
325  action: A callable that accepts no arguments.
326  """
327  raise NotImplementedError()
grpc.framework.interfaces.base.base.OperationContext.time_remaining
def time_remaining(self)
Definition: base.py:124
grpc.framework.interfaces.base.base.OperationContext.fail
def fail(self, exception)
Definition: base.py:140
grpc.framework.interfaces.base.base.End.add_idle_action
def add_idle_action(self, action)
Definition: base.py:321
grpc.framework.interfaces.base.base.End.stop
def stop(self, grace)
Definition: base.py:250
grpc.framework.interfaces.base.base.Outcome.Kind
Definition: base.py:70
grpc.framework.interfaces.base.base.Subscription.Kind
Definition: base.py:206
grpc.framework.interfaces.base.base.End.start
def start(self)
Definition: base.py:245
grpc.framework.interfaces.base.base.Servicer.service
def service(self, group, method, context, output_operator)
Definition: base.py:217
grpc.framework.interfaces.base.base.End
Definition: base.py:241
grpc.framework.interfaces.base.base.NoSuchMethodError.details
details
Definition: base.py:55
grpc.framework.interfaces.base.base.Subscription
Definition: base.py:186
grpc.framework.interfaces.base.base.OperationContext.add_termination_callback
def add_termination_callback(self, callback)
Definition: base.py:108
grpc.framework.interfaces.base.base.Servicer
Definition: base.py:213
grpc.framework.interfaces.base.base.End.operate
def operate(self, group, method, subscription, timeout, initial_metadata=None, payload=None, completion=None, protocol_options=None)
Definition: base.py:274
grpc.framework.interfaces.base.base.Operator
Definition: base.py:149
grpc.framework.interfaces.base.base.Outcome
Definition: base.py:58
grpc.framework.interfaces.base.base.NoSuchMethodError
Definition: base.py:34
grpc.framework.interfaces.base.base.OperationContext.cancel
def cancel(self)
Definition: base.py:135
grpc.framework.interfaces.base.base.OperationContext.outcome
def outcome(self)
Definition: base.py:98
grpc.framework.interfaces.base.base.End.operation_stats
def operation_stats(self)
Definition: base.py:311
grpc.framework.interfaces.base.base.NoSuchMethodError.code
code
Definition: base.py:54
grpc.framework.interfaces.base.base.ProtocolReceiver
Definition: base.py:173
grpc.framework.interfaces.base.base.Completion
Definition: base.py:84
grpc.framework.interfaces.base.base.ProtocolReceiver.context
def context(self, protocol_context)
Definition: base.py:177
grpc.framework.interfaces.base.base.NoSuchMethodError.__init__
def __init__(self, code, details)
Definition: base.py:44
grpc.framework.interfaces.base.base.Operator.advance
def advance(self, initial_metadata=None, payload=None, completion=None, allowance=None)
Definition: base.py:153


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:35