Skip to content
Draft
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
11 changes: 11 additions & 0 deletions packages/google-api-core/google/api_core/grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,17 @@ def create_channel(
if attempt_direct_path:
target = _modify_target_for_direct_path(target)

if "options" in kwargs and isinstance(kwargs["options"], (list, tuple)):
opts = list(kwargs["options"])
if not any(
isinstance(opt, (list, tuple))
and len(opt) >= 2
and opt[0] == "grpc.max_metadata_size"
for opt in opts
):
opts.append(("grpc.max_metadata_size", 32768))
kwargs["options"] = opts
Comment on lines +378 to +387

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation only configures grpc.max_metadata_size if options is already present in kwargs. This means that for the default case where no custom options are provided, the default max_metadata_size will not be set. To ensure that the default limit is always configured, we should initialize options as an empty list if it is not provided.

Suggested change
if "options" in kwargs and isinstance(kwargs["options"], (list, tuple)):
opts = list(kwargs["options"])
if not any(
isinstance(opt, (list, tuple))
and len(opt) >= 2
and opt[0] == "grpc.max_metadata_size"
for opt in opts
):
opts.append(("grpc.max_metadata_size", 32768))
kwargs["options"] = opts
options = kwargs.get("options")
if options is None:
options = []
elif isinstance(options, (list, tuple)):
options = list(options)
else:
options = None
if options is not None:
if not any(
isinstance(opt, (list, tuple))
and len(opt) >= 2
and opt[0] == "grpc.max_metadata_size"
for opt in options
):
options.append(("grpc.max_metadata_size", 32768))
kwargs["options"] = options


return grpc.secure_channel(
target, composite_credentials, compression=compression, **kwargs
)
Expand Down
11 changes: 11 additions & 0 deletions packages/google-api-core/google/api_core/grpc_helpers_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ def create_channel(
if attempt_direct_path:
target = grpc_helpers._modify_target_for_direct_path(target)

if "options" in kwargs and isinstance(kwargs["options"], (list, tuple)):
opts = list(kwargs["options"])
if not any(
isinstance(opt, (list, tuple))
and len(opt) >= 2
and opt[0] == "grpc.max_metadata_size"
for opt in opts
):
opts.append(("grpc.max_metadata_size", 32768))
kwargs["options"] = opts
Comment on lines +307 to +316

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation only configures grpc.max_metadata_size if options is already present in kwargs. This means that for the default case where no custom options are provided, the default max_metadata_size will not be set. To ensure that the default limit is always configured, we should initialize options as an empty list if it is not provided.

Suggested change
if "options" in kwargs and isinstance(kwargs["options"], (list, tuple)):
opts = list(kwargs["options"])
if not any(
isinstance(opt, (list, tuple))
and len(opt) >= 2
and opt[0] == "grpc.max_metadata_size"
for opt in opts
):
opts.append(("grpc.max_metadata_size", 32768))
kwargs["options"] = opts
options = kwargs.get("options")
if options is None:
options = []
elif isinstance(options, (list, tuple)):
options = list(options)
else:
options = None
if options is not None:
if not any(
isinstance(opt, (list, tuple))
and len(opt) >= 2
and opt[0] == "grpc.max_metadata_size"
for opt in options
):
options.append(("grpc.max_metadata_size", 32768))
kwargs["options"] = options


return aio.secure_channel(
target, composite_credentials, compression=compression, **kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,15 @@ def test_create_channel(grpc_secure_channel):
credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None)


@mock.patch("google.auth.default", return_value=(mock.sentinel.credentials, None))
@mock.patch("grpc.aio.secure_channel")
def test_create_channel_max_metadata_size(grpc_secure_channel, auth_default):
options = [("grpc.max_send_message_length", -1)]
grpc_helpers_async.create_channel("example.com:443", options=options)
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]
Comment on lines +735 to +739

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Update the test to verify that grpc.max_metadata_size is correctly configured both when custom options are provided and when they are omitted.

Suggested change
def test_create_channel_max_metadata_size(grpc_secure_channel, auth_default):
options = [("grpc.max_send_message_length", -1)]
grpc_helpers_async.create_channel("example.com:443", options=options)
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]
def test_create_channel_max_metadata_size(grpc_secure_channel, auth_default):
options = [("grpc.max_send_message_length", -1)]
grpc_helpers_async.create_channel("example.com:443", options=options)
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]
grpc_secure_channel.reset_mock()
grpc_helpers_async.create_channel("example.com:443")
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]



@pytest.mark.asyncio
async def test_fake_stream_unary_call():
fake_call = grpc_helpers_async.FakeStreamUnaryCall()
Expand Down
9 changes: 9 additions & 0 deletions packages/google-api-core/tests/unit/test_grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,3 +925,12 @@ def test_subscribe_unsubscribe(self):
def test_close(self):
channel = grpc_helpers.ChannelStub()
assert channel.close() is None


@mock.patch("google.auth.default", return_value=(mock.sentinel.credentials, None))
@mock.patch("grpc.secure_channel")
def test_create_channel_max_metadata_size(grpc_secure_channel, auth_default):
options = [("grpc.max_send_message_length", -1)]
grpc_helpers.create_channel("example.com:443", options=options)
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]
Comment on lines +932 to +936

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Update the test to verify that grpc.max_metadata_size is correctly configured both when custom options are provided and when they are omitted.

Suggested change
def test_create_channel_max_metadata_size(grpc_secure_channel, auth_default):
options = [("grpc.max_send_message_length", -1)]
grpc_helpers.create_channel("example.com:443", options=options)
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]
def test_create_channel_max_metadata_size(grpc_secure_channel, auth_default):
options = [("grpc.max_send_message_length", -1)]
grpc_helpers.create_channel("example.com:443", options=options)
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]
grpc_secure_channel.reset_mock()
grpc_helpers.create_channel("example.com:443")
_, kwargs = grpc_secure_channel.call_args
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]

Loading