interfaces.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. """Constants and interfaces of the Beta API of gRPC Python."""
  15. import abc
  16. import grpc
  17. import six
  18. ChannelConnectivity = grpc.ChannelConnectivity
  19. # FATAL_FAILURE was a Beta-API name for SHUTDOWN
  20. ChannelConnectivity.FATAL_FAILURE = ChannelConnectivity.SHUTDOWN
  21. StatusCode = grpc.StatusCode
  22. class GRPCCallOptions(object):
  23. """A value encapsulating gRPC-specific options passed on RPC invocation.
  24. This class and its instances have no supported interface - it exists to
  25. define the type of its instances and its instances exist to be passed to
  26. other functions.
  27. """
  28. def __init__(self, disable_compression, subcall_of, credentials):
  29. self.disable_compression = disable_compression
  30. self.subcall_of = subcall_of
  31. self.credentials = credentials
  32. def grpc_call_options(disable_compression=False, credentials=None):
  33. """Creates a GRPCCallOptions value to be passed at RPC invocation.
  34. All parameters are optional and should always be passed by keyword.
  35. Args:
  36. disable_compression: A boolean indicating whether or not compression should
  37. be disabled for the request object of the RPC. Only valid for
  38. request-unary RPCs.
  39. credentials: A CallCredentials object to use for the invoked RPC.
  40. """
  41. return GRPCCallOptions(disable_compression, None, credentials)
  42. GRPCAuthMetadataContext = grpc.AuthMetadataContext
  43. GRPCAuthMetadataPluginCallback = grpc.AuthMetadataPluginCallback
  44. GRPCAuthMetadataPlugin = grpc.AuthMetadataPlugin
  45. class GRPCServicerContext(six.with_metaclass(abc.ABCMeta)):
  46. """Exposes gRPC-specific options and behaviors to code servicing RPCs."""
  47. @abc.abstractmethod
  48. def peer(self):
  49. """Identifies the peer that invoked the RPC being serviced.
  50. Returns:
  51. A string identifying the peer that invoked the RPC being serviced.
  52. """
  53. raise NotImplementedError()
  54. @abc.abstractmethod
  55. def disable_next_response_compression(self):
  56. """Disables compression of the next response passed by the application."""
  57. raise NotImplementedError()
  58. class GRPCInvocationContext(six.with_metaclass(abc.ABCMeta)):
  59. """Exposes gRPC-specific options and behaviors to code invoking RPCs."""
  60. @abc.abstractmethod
  61. def disable_next_request_compression(self):
  62. """Disables compression of the next request passed by the application."""
  63. raise NotImplementedError()
  64. class Server(six.with_metaclass(abc.ABCMeta)):
  65. """Services RPCs."""
  66. @abc.abstractmethod
  67. def add_insecure_port(self, address):
  68. """Reserves a port for insecure RPC service once this Server becomes active.
  69. This method may only be called before calling this Server's start method is
  70. called.
  71. Args:
  72. address: The address for which to open a port.
  73. Returns:
  74. An integer port on which RPCs will be serviced after this link has been
  75. started. This is typically the same number as the port number contained
  76. in the passed address, but will likely be different if the port number
  77. contained in the passed address was zero.
  78. """
  79. raise NotImplementedError()
  80. @abc.abstractmethod
  81. def add_secure_port(self, address, server_credentials):
  82. """Reserves a port for secure RPC service after this Server becomes active.
  83. This method may only be called before calling this Server's start method is
  84. called.
  85. Args:
  86. address: The address for which to open a port.
  87. server_credentials: A ServerCredentials.
  88. Returns:
  89. An integer port on which RPCs will be serviced after this link has been
  90. started. This is typically the same number as the port number contained
  91. in the passed address, but will likely be different if the port number
  92. contained in the passed address was zero.
  93. """
  94. raise NotImplementedError()
  95. @abc.abstractmethod
  96. def start(self):
  97. """Starts this Server's service of RPCs.
  98. This method may only be called while the server is not serving RPCs (i.e. it
  99. is not idempotent).
  100. """
  101. raise NotImplementedError()
  102. @abc.abstractmethod
  103. def stop(self, grace):
  104. """Stops this Server's service of RPCs.
  105. All calls to this method immediately stop service of new RPCs. When existing
  106. RPCs are aborted is controlled by the grace period parameter passed to this
  107. method.
  108. This method may be called at any time and is idempotent. Passing a smaller
  109. grace value than has been passed in a previous call will have the effect of
  110. stopping the Server sooner. Passing a larger grace value than has been
  111. passed in a previous call will not have the effect of stopping the server
  112. later.
  113. Args:
  114. grace: A duration of time in seconds to allow existing RPCs to complete
  115. before being aborted by this Server's stopping. May be zero for
  116. immediate abortion of all in-progress RPCs.
  117. Returns:
  118. A threading.Event that will be set when this Server has completely
  119. stopped. The returned event may not be set until after the full grace
  120. period (if some ongoing RPC continues for the full length of the period)
  121. of it may be set much sooner (such as if this Server had no RPCs underway
  122. at the time it was stopped or if all RPCs that it had underway completed
  123. very early in the grace period).
  124. """
  125. raise NotImplementedError()