Abstract the validation into a common function

pull/23632/head
Lidi Zheng 5 years ago
parent 655319e286
commit d13c5834af
  1. 22
      src/python/grpcio/grpc/_common.py
  2. 25
      src/python/grpcio/grpc/_server.py
  3. 25
      src/python/grpcio/grpc/experimental/aio/_server.py

@ -61,6 +61,9 @@ STATUS_CODE_TO_CYGRPC_STATUS_CODE = {
MAXIMUM_WAIT_TIMEOUT = 0.1 MAXIMUM_WAIT_TIMEOUT = 0.1
_ERROR_MESSAGE_PORT_BINDING_FAILED = 'Failed to bind to address %s; set ' \
'GRPC_VERBOSITY=debug environment variable to see detailed error message.'
def encode(s): def encode(s):
if isinstance(s, bytes): if isinstance(s, bytes):
@ -144,3 +147,22 @@ def wait(wait_fn, wait_complete_fn, timeout=None, spin_cb=None):
return True return True
_wait_once(wait_fn, remaining, spin_cb) _wait_once(wait_fn, remaining, spin_cb)
return False return False
def validate_port_binding_result(address, port):
"""Validates if the port binding succeed.
If the port returned by Core is 0, the binding is failed. However, in that
case, the Core API doesn't return a detailed failing reason. The best we
can do is raising an exception to prevent further confusion.
Args:
address: The address string to be bound.
port: An int returned by core
"""
if port == 0:
# The Core API doesn't return a failure message. The best we can do
# is raising an exception to prevent further confusion.
raise RuntimeError(_ERROR_MESSAGE_PORT_BINDING_FAILED % address)
else:
return port

@ -958,27 +958,14 @@ class _Server(grpc.Server):
_add_generic_handlers(self._state, generic_rpc_handlers) _add_generic_handlers(self._state, generic_rpc_handlers)
def add_insecure_port(self, address): def add_insecure_port(self, address):
port = _add_insecure_port(self._state, _common.encode(address)) return _common.validate_port_binding_result(
if port == 0: address, _add_insecure_port(self._state, _common.encode(address)))
# The Core API doesn't return a failure message. The best we can do
# is raising an exception to prevent further confusion.
raise RuntimeError('Failed to bind to address %s; set '
'GRPC_VERBOSITY=debug env to see detailed error '
'message.' % address)
else:
return port
def add_secure_port(self, address, server_credentials): def add_secure_port(self, address, server_credentials):
port = _add_secure_port(self._state, _common.encode(address), return _common.validate_port_binding_result(
server_credentials) address,
if port == 0: _add_secure_port(self._state, _common.encode(address),
# The Core API doesn't return a failure message. The best we can do server_credentials))
# is raising an exception to prevent further confusion.
raise RuntimeError('Failed to bind to address %s; set '
'GRPC_VERBOSITY=debug env to see detailed error '
'message.' % address)
else:
return port
def start(self): def start(self):
_start(self._state) _start(self._state)

@ -80,15 +80,8 @@ class Server(_base_server.Server):
Returns: Returns:
An integer port on which the server will accept RPC requests. An integer port on which the server will accept RPC requests.
""" """
port = self._server.add_insecure_port(_common.encode(address)) return _common.validate_port_binding_result(
if port == 0: address, self._server.add_insecure_port(_common.encode(address)))
# The Core API doesn't return a failure message. The best we can do
# is raising an exception to prevent further confusion.
raise RuntimeError('Failed to bind to address %s; set '
'GRPC_VERBOSITY=debug env to see detailed error '
'message.' % address)
else:
return port
def add_secure_port(self, address: str, def add_secure_port(self, address: str,
server_credentials: grpc.ServerCredentials) -> int: server_credentials: grpc.ServerCredentials) -> int:
@ -105,16 +98,10 @@ class Server(_base_server.Server):
Returns: Returns:
An integer port on which the server will accept RPC requests. An integer port on which the server will accept RPC requests.
""" """
port = self._server.add_secure_port(_common.encode(address), return _common.validate_port_binding_result(
server_credentials) address,
if port == 0: self._server.add_secure_port(_common.encode(address),
# The Core API doesn't return a failure message. The best we can do server_credentials))
# is raising an exception to prevent further confusion.
raise RuntimeError('Failed to bind to address %s; set '
'GRPC_VERBOSITY=debug env to see detailed error '
'message.' % address)
else:
return port
async def start(self) -> None: async def start(self) -> None:
"""Starts this Server. """Starts this Server.

Loading…
Cancel
Save