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

Exclude attributes from Endpoint.toString() #6061

Merged
merged 1 commit into from
Jan 14, 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
29 changes: 9 additions & 20 deletions core/src/main/java/com/linecorp/armeria/client/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ enum Type {
private final int weight;
private final List<Endpoint> endpoints;
private final String authority;
private final String strVal;

@Nullable
private final Attributes attributes;
Expand Down Expand Up @@ -264,8 +263,6 @@ private Endpoint(Type type, String host, @Nullable String ipAddr, int port, int

// Pre-generate the authority.
authority = generateAuthority(type, host, port);
// Pre-generate toString() value.
strVal = generateToString(type, authority, ipAddr, weight, attributes);
this.attributes = attributes;
}

Expand All @@ -291,22 +288,6 @@ private static String generateAuthority(Type type, String host, int port) {
}
}

private static String generateToString(Type type, String authority, @Nullable String ipAddr,
int weight, @Nullable Attributes attributes) {
try (TemporaryThreadLocals tempThreadLocals = TemporaryThreadLocals.acquire()) {
final StringBuilder buf = tempThreadLocals.stringBuilder();
buf.append("Endpoint{").append(authority);
if (type == Type.HOSTNAME_AND_IP) {
buf.append(", ipAddr=").append(ipAddr);
}
buf.append(", weight=").append(weight);
if (attributes != null) {
buf.append(", attributes=").append(attributes);
}
return buf.append('}').toString();
}
}

@Override
public List<Endpoint> endpoints() {
return endpoints;
Expand Down Expand Up @@ -981,6 +962,14 @@ public int compareTo(Endpoint that) {

@Override
public String toString() {
return strVal;
try (TemporaryThreadLocals tempThreadLocals = TemporaryThreadLocals.acquire()) {
final StringBuilder buf = tempThreadLocals.stringBuilder();
buf.append("Endpoint{").append(authority);
if (type == Type.HOSTNAME_AND_IP) {
buf.append(", ipAddr=").append(ipAddr);
}
return buf.append(", weight=").append(weight)
.append('}').toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,9 @@ void testToString() {

// attributes
final Endpoint endpointWithAttr = Endpoint.of("127.0.0.1").withAttr(AttributeKey.valueOf("test"), 1);
// toString() should not include the attributes.
assertThat(endpointWithAttr.toString())
.isEqualTo("Endpoint{127.0.0.1, weight=1000, attributes=[test=1]}");
.isEqualTo("Endpoint{127.0.0.1, weight=1000}");
}

@Test
Expand Down
Loading