interfaces.py 5.7 KB

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