Skip to content

support ServerReflection with a custom DescriptorPool #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions grpclib/reflection/_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import Collection
from typing import Any, Collection, Optional

from google.protobuf.descriptor import FileDescriptor
from google.protobuf.descriptor_pb2 import FileDescriptorProto
Expand All @@ -35,10 +35,14 @@ class ServerReflection(ServerReflectionBase):
"""
Implements server reflection protocol.
"""
def __init__(self, *, _service_names: Collection[str]):
def __init__(
self, *,
_service_names: Collection[str],
_pool: Optional[Any] = None
):
self._service_names = _service_names
# FIXME: DescriptorPool has incomplete typings
self._pool = Default() # type: ignore
self._pool = _pool or Default() # type: ignore

def _not_found_response(self) -> ServerReflectionResponse:
return ServerReflectionResponse(
Expand Down
21 changes: 15 additions & 6 deletions grpclib/reflection/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import TYPE_CHECKING, Collection, List
from typing import TYPE_CHECKING, Any, Collection, List, Optional

from google.protobuf.descriptor import FileDescriptor
from google.protobuf.descriptor_pb2 import FileDescriptorProto
Expand All @@ -41,10 +41,14 @@ class ServerReflection(ServerReflectionBase):
"""
Implements server reflection protocol.
"""
def __init__(self, *, _service_names: Collection[str]):
def __init__(
self, *,
_service_names: Collection[str],
_pool: Optional[Any] = None
):
self._service_names = _service_names
# FIXME: DescriptorPool has incomplete typings
self._pool = Default() # type: ignore
self._pool = _pool or Default() # type: ignore

def _not_found_response(self) -> ServerReflectionResponse:
return ServerReflectionResponse(
Expand Down Expand Up @@ -161,7 +165,11 @@ async def ServerReflectionInfo(
await stream.send_message(response)

@classmethod
def extend(cls, services: 'Collection[IServable]') -> 'List[IServable]':
def extend(
cls, services: 'Collection[IServable]',
*,
pool: Optional[Any] = None
) -> 'List[IServable]':
"""
Extends services list with reflection service:

Expand All @@ -181,6 +189,7 @@ def extend(cls, services: 'Collection[IServable]') -> 'List[IServable]':
for service in services:
service_names.append(_service_name(service))
services = list(services)
services.append(cls(_service_names=service_names))
services.append(_ServerReflectionV1Alpha(_service_names=service_names))
services.append(cls(_service_names=service_names, _pool=pool))
services.append(
_ServerReflectionV1Alpha(_service_names=service_names, _pool=pool))
return services
44 changes: 44 additions & 0 deletions tests/test_reflection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import socket

from google.protobuf.descriptor_pool import DescriptorPool

import pytest
import pytest_asyncio

Expand Down Expand Up @@ -108,3 +110,45 @@ async def test_list_services_response(channel):

service, = r1.list_services_response.service
assert service.name == DESCRIPTOR.services_by_name['DummyService'].full_name


@pytest.mark.asyncio
async def test_file_containing_symbol_response_custom_pool(port):
my_pool = DescriptorPool()
services = [DummyService()]
services = ServerReflection.extend(services, pool=my_pool)

server = Server(services)
await server.start(port=port)

channel = Channel(port=port)
try:
# because we use our own pool (my_pool), there's no descriptors to find.
req = ServerReflectionRequest(
file_containing_symbol=(
DESCRIPTOR.message_types_by_name['DummyRequest'].full_name
),
)
resp, = await ServerReflectionStub(channel).ServerReflectionInfo([req])

assert resp == ServerReflectionResponse(
error_response=ErrorResponse(
error_code=5,
error_message='not found',
),
)

# once we update the pool, we should find the descriptor.
my_pool.AddSerializedFile(DESCRIPTOR.serialized_pb)

resp, = await ServerReflectionStub(channel).ServerReflectionInfo([req])

proto_bytes, = resp.file_descriptor_response.file_descriptor_proto
dummy_proto = FileDescriptorProto()
dummy_proto.ParseFromString(proto_bytes)
assert dummy_proto.name == DESCRIPTOR.name
assert dummy_proto.package == DESCRIPTOR.package
finally:
channel.close()
server.close()
await server.wait_closed()