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

Reduce overhead of REST Client config handling #42100

Closed
wants to merge 1 commit into from
Closed
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import jakarta.enterprise.inject.CreationException;

Expand Down Expand Up @@ -327,11 +328,21 @@ public RestClientConfig getClientConfig(String configKey) {
if (configKey == null) {
return RestClientConfig.EMPTY;
}
return configs.computeIfAbsent(configKey, RestClientConfig::load);
return configs.computeIfAbsent(configKey, new Function<>() {
@Override
public RestClientConfig apply(String configKey) {
return RestClientConfig.load(configKey);
}
});
}

public RestClientConfig getClientConfig(Class<?> clientInterface) {
return configs.computeIfAbsent(clientInterface.getName(), name -> RestClientConfig.load(clientInterface));
return configs.computeIfAbsent(clientInterface.getName(), new Function<>() {
@Override
public RestClientConfig apply(String interfaceName) {
return RestClientConfig.load(clientInterface);
}
});
}

public void putClientConfig(String configKey, RestClientConfig clientConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.quarkus.restclient.config.RestClientConfig;
import io.quarkus.restclient.config.RestClientsConfig;
import io.quarkus.test.QuarkusUnitTest;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.common.AbstractConfigSource;

public class RestClientConfigNotationTest {
Expand All @@ -41,6 +42,7 @@ public class RestClientConfigNotationTest {
})
public void testInterfaceConfiguration(final String urlPropertyName) {
TestConfigSource.urlPropertyName = urlPropertyName;
refreshPropertyNames();

RestClientsConfig configRoot = new RestClientsConfig();
RestClientConfig clientConfig = configRoot.getClientConfig(EchoClient.class);
Expand All @@ -55,12 +57,19 @@ public void testInterfaceConfiguration(final String urlPropertyName) {
})
public void testConfigKeyConfiguration(final String urlPropertyName) {
TestConfigSource.urlPropertyName = urlPropertyName;
refreshPropertyNames();

RestClientsConfig configRoot = new RestClientsConfig();
RestClientConfig clientConfig = configRoot.getClientConfig("echo-client");

verifyConfig(clientConfig, urlPropertyName);
}

private void refreshPropertyNames() {
SmallRyeConfig config = (SmallRyeConfig) ConfigProvider.getConfig();
config.getLatestPropertyNames();
}

private void verifyConfig(final RestClientConfig clientConfig, final String urlPropertyName) {
ConfigSource configSource = Iterators.find(ConfigProvider.getConfig().getConfigSources().iterator(),
c -> c.getName().equals(TestConfigSource.SOURCE_NAME));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Map<String, String> getProperties() {

@Override
public Set<String> getPropertyNames() {
return Collections.emptySet();
return Set.of("quarkus.rest-client.\"io.quarkus.restclient.configuration.EchoClient\".url");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.net.URL;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
Expand All @@ -23,11 +24,13 @@
import jakarta.ws.rs.core.Configuration;
import jakarta.ws.rs.ext.ParamConverterProvider;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.rest.client.RestClientDefinitionException;
import org.eclipse.microprofile.rest.client.ext.QueryParamStyle;
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
import org.eclipse.microprofile.rest.client.spi.RestClientListener;
import org.jboss.resteasy.reactive.client.TlsConfig;
import org.jboss.resteasy.reactive.client.api.ClientLogger;
import org.jboss.resteasy.reactive.client.api.InvalidRestClientDefinitionException;
Expand Down Expand Up @@ -407,23 +410,37 @@ public <T> T build(Class<T> aClass) throws IllegalStateException, RestClientDefi
throw new IllegalStateException("No URL specified. Cannot build a rest client without URL");
}

RestClientListeners.get().forEach(listener -> listener.onNewClient(aClass, this));
Collection<RestClientListener> listeners = RestClientListeners.get();
for (RestClientListener listener : listeners) {
listener.onNewClient(aClass, this);
}

AnnotationRegisteredProviders annotationRegisteredProviders = arcContainer
.instance(AnnotationRegisteredProviders.class).get();
for (Map.Entry<Class<?>, Integer> mapper : annotationRegisteredProviders.getProviders(aClass).entrySet()) {
for (var mapper : annotationRegisteredProviders.getProviders(aClass).entrySet()) {
register(mapper.getKey(), mapper.getValue());
}

Object defaultMapperDisabled = getConfiguration().getProperty(DEFAULT_MAPPER_DISABLED);
Boolean globallyDisabledMapper = ConfigProvider.getConfig()
Config mpConfig = ConfigProvider.getConfig();
Boolean globallyDisabledMapper = mpConfig
.getOptionalValue(DEFAULT_MAPPER_DISABLED, Boolean.class).orElse(false);
if (!globallyDisabledMapper && !(defaultMapperDisabled instanceof Boolean && (Boolean) defaultMapperDisabled)) {
exceptionMappers.add(new DefaultMicroprofileRestClientExceptionMapper());
}

exceptionMappers.sort(Comparator.comparingInt(ResponseExceptionMapper::getPriority));
redirectHandlers.sort(Comparator.comparingInt(RedirectHandler::getPriority));
exceptionMappers.sort(new Comparator<>() {
@Override
public int compare(ResponseExceptionMapper<?> o1, ResponseExceptionMapper<?> o2) {
return Integer.compare(o1.getPriority(), o2.getPriority());
}
});
redirectHandlers.sort(new Comparator<>() {
@Override
public int compare(RedirectHandler o1, RedirectHandler o2) {
return Integer.compare(o1.getPriority(), o2.getPriority());
}
});
clientBuilder.register(new MicroProfileRestClientResponseFilter(exceptionMappers));
clientBuilder.followRedirects(followRedirects);

Expand Down Expand Up @@ -456,7 +473,7 @@ public <T> T build(Class<T> aClass) throws IllegalStateException, RestClientDefi

Boolean effectiveTrustAll = trustAll;
if (effectiveTrustAll == null) {
effectiveTrustAll = ConfigProvider.getConfig().getOptionalValue(TLS_TRUST_ALL, Boolean.class)
effectiveTrustAll = mpConfig.getOptionalValue(TLS_TRUST_ALL, Boolean.class)
.orElse(false);
}

Expand Down Expand Up @@ -493,7 +510,7 @@ public <T> T build(Class<T> aClass) throws IllegalStateException, RestClientDefi
clientBuilder.alpn(restClientsConfig.alpn.get());
}

Boolean enableCompression = ConfigProvider.getConfig()
Boolean enableCompression = mpConfig
.getOptionalValue(ENABLE_COMPRESSION, Boolean.class).orElse(false);
if (enableCompression) {
clientBuilder.enableCompression();
Expand Down
Loading