[WIP] fix(api_core): configure default max_metadata_size in gRPC helpers#17663
[WIP] fix(api_core): configure default max_metadata_size in gRPC helpers#17663ohmayr wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request configures a default grpc.max_metadata_size of 32768 when creating secure gRPC channels in both synchronous and asynchronous helpers, and adds corresponding tests. The reviewer noted that the default limit is currently only applied if custom options are already present in kwargs. To ensure the limit is always enforced, the reviewer suggested initializing options as an empty list when omitted, and provided updated test cases to verify both scenarios.
| 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 |
There was a problem hiding this comment.
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.
| 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 |
| 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 |
There was a problem hiding this comment.
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.
| 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 |
| 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"] |
There was a problem hiding this comment.
Update the test to verify that grpc.max_metadata_size is correctly configured both when custom options are provided and when they are omitted.
| 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"] |
| 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"] |
There was a problem hiding this comment.
Update the test to verify that grpc.max_metadata_size is correctly configured both when custom options are provided and when they are omitted.
| 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"] |
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
Fixes #<issue_number_goes_here> 🦕