diff --git a/packages/google-api-core/google/api_core/grpc_helpers.py b/packages/google-api-core/google/api_core/grpc_helpers.py index 30ba19c54f1a..9dcd92c1ca4b 100644 --- a/packages/google-api-core/google/api_core/grpc_helpers.py +++ b/packages/google-api-core/google/api_core/grpc_helpers.py @@ -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 + return grpc.secure_channel( target, composite_credentials, compression=compression, **kwargs ) diff --git a/packages/google-api-core/google/api_core/grpc_helpers_async.py b/packages/google-api-core/google/api_core/grpc_helpers_async.py index 780788e4b565..7ea91b307924 100644 --- a/packages/google-api-core/google/api_core/grpc_helpers_async.py +++ b/packages/google-api-core/google/api_core/grpc_helpers_async.py @@ -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 + return aio.secure_channel( target, composite_credentials, compression=compression, **kwargs ) diff --git a/packages/google-api-core/tests/asyncio/test_grpc_helpers_async.py b/packages/google-api-core/tests/asyncio/test_grpc_helpers_async.py index 43700f22bc83..65f3693ccbc2 100644 --- a/packages/google-api-core/tests/asyncio/test_grpc_helpers_async.py +++ b/packages/google-api-core/tests/asyncio/test_grpc_helpers_async.py @@ -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"] + + @pytest.mark.asyncio async def test_fake_stream_unary_call(): fake_call = grpc_helpers_async.FakeStreamUnaryCall() diff --git a/packages/google-api-core/tests/unit/test_grpc_helpers.py b/packages/google-api-core/tests/unit/test_grpc_helpers.py index e05d3969951f..edf865dac685 100644 --- a/packages/google-api-core/tests/unit/test_grpc_helpers.py +++ b/packages/google-api-core/tests/unit/test_grpc_helpers.py @@ -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"]