Skip to content
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

DefaultHostAndPort: equals and hashCode are not consistent #2974

Merged
merged 1 commit into from
Jun 17, 2024

Conversation

idelpivnitskiy
Copy link
Member

Motivation:

If two instances of DefaultHostAndPort are created with hostname in different case, they pass equals check but don't have a consistent hashCode. As a result, users can't query a Map entry with keys in different case.
Side-effect of that is a multi-address http client that may have multiple instances of a single-address client for the same hostname in different cases, preventing it from reusing connections.

Modifications:

  • Implement DefaultHostAndPort.hashCode() in a consistent way with InetSocketAddressHolder where hostName hash is computed in case-insensitive mode.
  • Re-generate DefaultHostAndPort.equals(...) implementation to allow a check by reference.
  • Enhance DefaultMultiAddressUrlHttpClientBuilderTest to demonstrate that the problem went away.

Result:

  1. DefaultHostAndPort can be used as a case-insensitive key for maps, matching definition of the host name in URL.
  2. Multi-address client correctly recognizes the same host name in case-insensitive way.

Motivation:

If two instances of `DefaultHostAndPort` are created with hostname in
different case, they pass `equals` check but don't have a consistent
`hashCode`. As a result, users can't query a `Map` entry with keys in
different case.
Side-effect of that is a multi-address http client that may have
multiple instances of a single-address client for the same hostname in
different cases, preventing it from reusing connections.

Modifications:
- Implement `DefaultHostAndPort.hashCode()` in a consistent way with
`InetSocketAddressHolder` where `hostName` hash is computed in
case-insensitive mode.
- Re-generate `DefaultHostAndPort.equals(...)` implementation to allow a
check by reference.
- Enhance `DefaultMultiAddressUrlHttpClientBuilderTest` to demonstrate
that the problem went away.

Result:

1. `DefaultHostAndPort` can be used as a case-insensitive key for maps,
matching definition of the host name in URL.
2. Multi-address client correctly recognizes the same host name in
case-insensitive way.
@idelpivnitskiy idelpivnitskiy self-assigned this Jun 17, 2024
@idelpivnitskiy idelpivnitskiy merged commit 7b68059 into apple:main Jun 17, 2024
11 checks passed
@idelpivnitskiy idelpivnitskiy deleted the hostAndPort branch June 17, 2024 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants