Prevent duplicate entries in channels map

Prevents duplicate entries from being added to the named channels map
in client properties.  

Resolves #67
This commit is contained in:
Andrey Litvitski
2024-12-19 19:33:25 +03:00
committed by GitHub
parent 729940d62c
commit 8628aa1c8a
3 changed files with 23 additions and 1 deletions

View File

@@ -86,7 +86,7 @@ public class GrpcClientAutoConfiguration {
@Override
public String getTarget(String authority) {
NamedChannel channel = this.channels.getChannel(authority);
return this.channels.getTarget(channel.getAddress());
return this.channels.getTarget(channel);
}
}

View File

@@ -83,6 +83,15 @@ public class GrpcClientProperties implements EnvironmentAware {
return address;
}
public String getTarget(NamedChannel channel) {
String address = channel.getAddress();
if (address.startsWith("static:") || address.startsWith("tcp:")) {
address = address.substring(address.indexOf(":") + 1).replaceFirst("/*", "");
}
address = this.environment.resolvePlaceholders(address);
return address;
}
public static class NamedChannel {
/**

View File

@@ -200,6 +200,19 @@ class GrpcClientPropertiesTests {
assertThat(properties.getChannels()).containsOnlyKeys("custom");
}
@Test
void withCustomChannelReturnsDuplicateEntryMap() {
Map<String, String> map = new HashMap<>();
map.put("spring.grpc.client.channels.custom.address", "static://my-server:8888");
GrpcClientProperties properties = bindProperties(map);
GrpcClientAutoConfiguration.NamedChannelVirtualTargets virtualTargets = new GrpcClientAutoConfiguration.NamedChannelVirtualTargets(
properties);
var address = virtualTargets.getTarget("custom");
assertThat(address).isEqualTo("my-server:8888");
assertThat(properties.getTarget("custom")).isEqualTo("my-server:8888");
assertThat(properties.getChannels()).containsOnlyKeys("custom");
}
}
}