Merge pull request #16153 from mehrdada/backport-SERVICE_NAME-to-1.14.x

Add SERVICE_NAME to reflection/health (1.14.x backport)
pull/16157/head^2
Mehrdad Afshari 6 years ago committed by GitHub
commit 62ce6a8e49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      src/python/grpcio_health_checking/grpc_health/v1/health.py
  2. 32
      src/python/grpcio_reflection/grpc_reflection/v1alpha/reflection.py
  3. 3
      src/python/grpcio_tests/tests/health_check/_health_servicer_test.py
  4. 4
      src/python/grpcio_tests/tests/reflection/_reflection_servicer_test.py

@ -17,11 +17,13 @@ import threading
import grpc
from grpc_health.v1 import health_pb2
from grpc_health.v1 import health_pb2_grpc
from grpc_health.v1 import health_pb2 as _health_pb2
from grpc_health.v1 import health_pb2_grpc as _health_pb2_grpc
SERVICE_NAME = _health_pb2.DESCRIPTOR.services_by_name['Health'].full_name
class HealthServicer(health_pb2_grpc.HealthServicer):
class HealthServicer(_health_pb2_grpc.HealthServicer):
"""Servicer handling RPCs for service statuses."""
def __init__(self):
@ -33,9 +35,9 @@ class HealthServicer(health_pb2_grpc.HealthServicer):
status = self._server_status.get(request.service)
if status is None:
context.set_code(grpc.StatusCode.NOT_FOUND)
return health_pb2.HealthCheckResponse()
return _health_pb2.HealthCheckResponse()
else:
return health_pb2.HealthCheckResponse(status=status)
return _health_pb2.HealthCheckResponse(status=status)
def set(self, service, status):
"""Sets the status of a service.

@ -17,15 +17,17 @@ import grpc
from google.protobuf import descriptor_pb2
from google.protobuf import descriptor_pool
from grpc_reflection.v1alpha import reflection_pb2
from grpc_reflection.v1alpha import reflection_pb2_grpc
from grpc_reflection.v1alpha import reflection_pb2 as _reflection_pb2
from grpc_reflection.v1alpha import reflection_pb2_grpc as _reflection_pb2_grpc
_POOL = descriptor_pool.Default()
SERVICE_NAME = _reflection_pb2.DESCRIPTOR.services_by_name[
'ServerReflection'].full_name
def _not_found_error():
return reflection_pb2.ServerReflectionResponse(
error_response=reflection_pb2.ErrorResponse(
return _reflection_pb2.ServerReflectionResponse(
error_response=_reflection_pb2.ErrorResponse(
error_code=grpc.StatusCode.NOT_FOUND.value[0],
error_message=grpc.StatusCode.NOT_FOUND.value[1].encode(),
))
@ -35,12 +37,12 @@ def _file_descriptor_response(descriptor):
proto = descriptor_pb2.FileDescriptorProto()
descriptor.CopyToProto(proto)
serialized_proto = proto.SerializeToString()
return reflection_pb2.ServerReflectionResponse(
file_descriptor_response=reflection_pb2.FileDescriptorResponse(
return _reflection_pb2.ServerReflectionResponse(
file_descriptor_response=_reflection_pb2.FileDescriptorResponse(
file_descriptor_proto=(serialized_proto,)),)
class ReflectionServicer(reflection_pb2_grpc.ServerReflectionServicer):
class ReflectionServicer(_reflection_pb2_grpc.ServerReflectionServicer):
"""Servicer handling RPCs for service statuses."""
def __init__(self, service_names, pool=None):
@ -94,17 +96,17 @@ class ReflectionServicer(reflection_pb2_grpc.ServerReflectionServicer):
except KeyError:
return _not_found_error()
else:
return reflection_pb2.ServerReflectionResponse(
all_extension_numbers_response=reflection_pb2.
return _reflection_pb2.ServerReflectionResponse(
all_extension_numbers_response=_reflection_pb2.
ExtensionNumberResponse(
base_type_name=message_descriptor.full_name,
extension_number=extension_numbers))
def _list_services(self):
return reflection_pb2.ServerReflectionResponse(
list_services_response=reflection_pb2.ListServiceResponse(
return _reflection_pb2.ServerReflectionResponse(
list_services_response=_reflection_pb2.ListServiceResponse(
service=[
reflection_pb2.ServiceResponse(name=service_name)
_reflection_pb2.ServiceResponse(name=service_name)
for service_name in self._service_names
]))
@ -126,8 +128,8 @@ class ReflectionServicer(reflection_pb2_grpc.ServerReflectionServicer):
elif request.HasField('list_services'):
yield self._list_services()
else:
yield reflection_pb2.ServerReflectionResponse(
error_response=reflection_pb2.ErrorResponse(
yield _reflection_pb2.ServerReflectionResponse(
error_response=_reflection_pb2.ErrorResponse(
error_code=grpc.StatusCode.INVALID_ARGUMENT.value[0],
error_message=grpc.StatusCode.INVALID_ARGUMENT.value[1]
.encode(),
@ -142,5 +144,5 @@ def enable_server_reflection(service_names, server, pool=None):
server: grpc.Server to which reflection service will be added.
pool: DescriptorPool object to use (descriptor_pool.Default() if None).
"""
reflection_pb2_grpc.add_ServerReflectionServicer_to_server(
_reflection_pb2_grpc.add_ServerReflectionServicer_to_server(
ReflectionServicer(service_names, pool=pool), server)

@ -73,6 +73,9 @@ class HealthServicerTest(unittest.TestCase):
self.assertEqual(grpc.StatusCode.NOT_FOUND, context.exception.code())
def test_health_service_name(self):
self.assertEqual(health.SERVICE_NAME, 'grpc.health.v1.Health')
if __name__ == '__main__':
unittest.main(verbosity=2)

@ -171,6 +171,10 @@ class ReflectionServicerTest(unittest.TestCase):
for name in _SERVICE_NAMES))),)
self.assertSequenceEqual(expected_responses, responses)
def testReflectionServiceName(self):
self.assertEqual(reflection.SERVICE_NAME,
'grpc.reflection.v1alpha.ServerReflection')
if __name__ == '__main__':
unittest.main(verbosity=2)

Loading…
Cancel
Save