use of com.palantir.dialogue.core.DialogueChannel in project dialogue by palantir.
the class ReloadingClientFactory method getStickyChannels.
@Override
public StickyChannelFactory getStickyChannels(String serviceName) {
Refreshable<List<Channel>> perHostChannels = perHost(serviceName).getPerHostChannels();
Refreshable<Supplier<Channel>> bestSupplier = perHostChannels.map(singleHostChannels -> {
if (singleHostChannels.isEmpty()) {
EmptyInternalDialogueChannel alwaysThrowing = new EmptyInternalDialogueChannel(() -> new SafeIllegalStateException("Service not configured", SafeArg.of("serviceName", serviceName)));
return () -> alwaysThrowing;
}
if (singleHostChannels.size() == 1) {
return () -> singleHostChannels.get(0);
}
return StickyEndpointChannels.builder().channels(singleHostChannels.stream().map(c -> (DialogueChannel) c).collect(Collectors.toList())).channelName(ChannelNames.reloading(serviceName, params)).taggedMetricRegistry(params.taggedMetrics()).build();
});
return new StickyChannelFactory() {
@Override
public Channel getStickyChannel() {
return bestSupplier.get().get();
}
@Override
public <T> T getCurrentBest(Class<T> clientInterface) {
return Reflection.callStaticFactoryMethod(clientInterface, getStickyChannel(), params.runtime());
}
@Override
public String toString() {
return "StickyChannelFactory{" + bestSupplier.get() + '}';
}
};
}
use of com.palantir.dialogue.core.DialogueChannel in project dialogue by palantir.
the class ReloadingClientFactory method getInternalDialogueChannel.
private Refreshable<InternalDialogueChannel> getInternalDialogueChannel(String serviceName) {
Preconditions.checkNotNull(serviceName, "serviceName");
String channelName = ChannelNames.reloading(serviceName, params);
return params.scb().map(block -> {
Preconditions.checkNotNull(block, "Refreshable must not provide a null ServicesConfigBlock");
if (!block.services().containsKey(serviceName)) {
return new EmptyInternalDialogueChannel(() -> new SafeIllegalStateException("Service not configured (config block not present)", SafeArg.of("serviceName", serviceName), SafeArg.of("available", block.services().keySet())));
}
if (block.services().get(serviceName).uris().isEmpty()) {
return new EmptyInternalDialogueChannel(() -> {
Map<String, PartialServiceConfiguration> servicesWithUris = Maps.filterValues(block.services(), c -> !c.uris().isEmpty());
return new SafeIllegalStateException("Service not configured (no URIs)", SafeArg.of("serviceName", serviceName), SafeArg.of("available", servicesWithUris.keySet()));
});
}
ServiceConfiguration serviceConf = ServiceConfigurationFactory.of(block).get(serviceName);
DialogueChannel dialogueChannel = cache.getNonReloadingChannel(params, serviceConf, channelName, OptionalInt.empty());
return new InternalDialogueChannelFromDialogueChannel(dialogueChannel);
});
}
use of com.palantir.dialogue.core.DialogueChannel in project dialogue by palantir.
the class ReloadingClientFactory method perHost.
@Override
public PerHostClientFactory perHost(String serviceName) {
Preconditions.checkNotNull(serviceName, "serviceName");
String channelName = ChannelNames.reloading(serviceName, params);
Refreshable<List<DialogueChannel>> perHostDialogueChannels = params.scb().map(block -> {
if (!block.services().containsKey(serviceName)) {
return ImmutableList.of();
}
ServiceConfiguration serviceConfiguration = ServiceConfigurationFactory.of(block).get(serviceName);
ImmutableList.Builder<DialogueChannel> list = ImmutableList.builder();
for (int i = 0; i < serviceConfiguration.uris().size(); i++) {
ServiceConfiguration singleUriServiceConf = ServiceConfiguration.builder().from(serviceConfiguration).uris(ImmutableList.of(serviceConfiguration.uris().get(i))).build();
// subtle gotcha here is that every single one of these has the same channelName,
// which means metrics like the QueuedChannel counter will end up being the sum of all of them.
list.add(cache.getNonReloadingChannel(params, singleUriServiceConf, channelName, OptionalInt.of(i)));
}
return list.build();
});
return new PerHostClientFactory() {
@Override
public Refreshable<List<Channel>> getPerHostChannels() {
return perHostDialogueChannels.map(ImmutableList::copyOf);
}
@Override
public <T> Refreshable<List<T>> getPerHost(Class<T> clientInterface) {
return getPerHostChannels().map(channels -> channels.stream().map(chan -> Reflection.callStaticFactoryMethod(clientInterface, chan, params.runtime())).collect(ImmutableList.toImmutableList()));
}
@Override
public String toString() {
return "PerHostClientFactory{serviceName=" + serviceName + ", channels=" + perHostDialogueChannels.get() + '}';
}
};
}
Aggregations