Skip to content

Commit

Permalink
Allow client config of WebSocket per message deflate (micronaut-proje…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyg484 authored Jan 23, 2024
1 parent b76111b commit d07ddbb
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,51 @@ public class DefaultHttpClientConfiguration extends HttpClientConfiguration {
*/
public static final String PREFIX = "micronaut.http.client";
private final DefaultConnectionPoolConfiguration connectionPoolConfiguration;
private final DefaultWebSocketCompressionConfiguration webSocketCompressionConfiguration;

/**
* Default constructor.
*/
public DefaultHttpClientConfiguration() {
this.connectionPoolConfiguration = new DefaultConnectionPoolConfiguration();
this.webSocketCompressionConfiguration = new DefaultWebSocketCompressionConfiguration();
}

/**
* @param connectionPoolConfiguration The connection pool configuration
* @param applicationConfiguration The application configuration
* @deprecated Use {@link DefaultHttpClientConfiguration(DefaultConnectionPoolConfiguration, DefaultWebSocketCompressionConfiguration, ApplicationConfiguration)} instead.
*/
@Inject
@Deprecated(since = "4.3.0")
public DefaultHttpClientConfiguration(DefaultConnectionPoolConfiguration connectionPoolConfiguration, ApplicationConfiguration applicationConfiguration) {
this(connectionPoolConfiguration, new DefaultWebSocketCompressionConfiguration(), applicationConfiguration);
}


/**
* @param connectionPoolConfiguration The connection pool configuration
* @param webSocketCompressionConfiguration The WebSocket compression configuration
* @param applicationConfiguration The application configuration
*/
@Inject
public DefaultHttpClientConfiguration(DefaultConnectionPoolConfiguration connectionPoolConfiguration,
DefaultWebSocketCompressionConfiguration webSocketCompressionConfiguration,
ApplicationConfiguration applicationConfiguration) {
super(applicationConfiguration);
this.connectionPoolConfiguration = connectionPoolConfiguration;
this.webSocketCompressionConfiguration = webSocketCompressionConfiguration;
}

@Override
public ConnectionPoolConfiguration getConnectionPoolConfiguration() {
return connectionPoolConfiguration;
}

@Override
public WebSocketCompressionConfiguration getWebSocketCompressionConfiguration() {
return webSocketCompressionConfiguration;
}

/**
* Uses the default SSL configuration.
*
Expand All @@ -82,4 +104,13 @@ public void setClientSslConfiguration(@Nullable ClientSslConfiguration sslConfig
@Primary
public static class DefaultConnectionPoolConfiguration extends ConnectionPoolConfiguration {
}

/**
* The default WebSocket compression configuration.
*/
@ConfigurationProperties(WebSocketCompressionConfiguration.PREFIX)
@BootstrapContextCompatible
@Primary
public static class DefaultWebSocketCompressionConfiguration extends WebSocketCompressionConfiguration {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public abstract class HttpClientConfiguration {
/**
* The default value.
*/
@SuppressWarnings("WeakerAccess")
public static final boolean DEFAULT_EXCEPTION_ON_ERROR_STATUS = true;

/**
Expand Down Expand Up @@ -302,6 +301,17 @@ public void setSslConfiguration(SslConfiguration sslConfiguration) {
this.sslConfiguration = sslConfiguration;
}

/**
* Obtains the WebSocket compression configuration.
*
* @return The WebSocket compression configuration.
* @since 4.3.0
*/
@Nullable
public WebSocketCompressionConfiguration getWebSocketCompressionConfiguration() {
return null;
}

/**
* @return Whether redirects should be followed
*/
Expand Down Expand Up @@ -934,4 +944,42 @@ public void setMaxConcurrentHttp2Connections(int maxConcurrentHttp2Connections)
}
}

/**
* Configuration for WebSocket client compression extensions.
*/
public static class WebSocketCompressionConfiguration implements Toggleable {

/**
* The prefix to use for configuration.
*/
public static final String PREFIX = "ws.compression";

/**
* The default enable value.
*/
@SuppressWarnings("WeakerAccess")
public static final boolean DEFAULT_ENABLED = true;

private boolean enabled = DEFAULT_ENABLED;

/**
* Whether deflate compression is enabled for client WebSocket connections.
*
* @return True if the per message deflate extension is enabled.
*/
@Override
public boolean isEnabled() {
return enabled;
}

/**
* Sets whether the per message deflate extension is enabled for WebSocket connections.
* Default value ({@link io.micronaut.http.client.HttpClientConfiguration.WebSocketCompressionConfiguration#DEFAULT_ENABLED}).
*
* @param enabled True is it is enabled.
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class ServiceHttpClientConfiguration extends HttpClientConfiguration impl

private final String serviceId;
private final ServiceConnectionPoolConfiguration connectionPoolConfiguration;
private final ServiceWebSocketCompressionConfiguration webSocketCompressionConfiguration;
private List<URI> urls = Collections.emptyList();
private String healthCheckUri = DEFAULT_HEALTHCHECKURI;
private boolean healthCheck = DEFAULT_HEALTHCHECK;
Expand Down Expand Up @@ -97,6 +98,7 @@ public ServiceHttpClientConfiguration(
} else {
this.connectionPoolConfiguration = new ServiceConnectionPoolConfiguration();
}
this.webSocketCompressionConfiguration = new ServiceWebSocketCompressionConfiguration();
}

/**
Expand All @@ -106,11 +108,31 @@ public ServiceHttpClientConfiguration(
* @param connectionPoolConfiguration The connection pool configuration
* @param sslConfiguration The SSL configuration
* @param defaultHttpClientConfiguration The default HTTP client configuration
* @deprecated Use {@link ServiceHttpClientConfiguration(String, ServiceConnectionPoolConfiguration, ServiceWebSocketCompressionConfiguration, ServiceSslClientConfiguration, HttpClientConfiguration)} instead.
*/
@Deprecated(since = "4.3.0")
public ServiceHttpClientConfiguration(
@Parameter String serviceId,
@Nullable ServiceConnectionPoolConfiguration connectionPoolConfiguration,
@Nullable ServiceSslClientConfiguration sslConfiguration,
HttpClientConfiguration defaultHttpClientConfiguration) {
this(serviceId, connectionPoolConfiguration, new ServiceWebSocketCompressionConfiguration(), sslConfiguration, defaultHttpClientConfiguration);
}

/**
* Creates a new client configuration for the given service ID.
*
* @param serviceId The service id
* @param connectionPoolConfiguration The connection pool configuration
* @param webSocketCompressionConfiguration The WebSocket compression configuration
* @param sslConfiguration The SSL configuration
* @param defaultHttpClientConfiguration The default HTTP client configuration
*/
@Inject
public ServiceHttpClientConfiguration(
@Parameter String serviceId,
@Nullable ServiceConnectionPoolConfiguration connectionPoolConfiguration,
@Nullable ServiceWebSocketCompressionConfiguration webSocketCompressionConfiguration,
@Nullable ServiceSslClientConfiguration sslConfiguration,
HttpClientConfiguration defaultHttpClientConfiguration) {
super(defaultHttpClientConfiguration);
Expand All @@ -123,6 +145,11 @@ public ServiceHttpClientConfiguration(
} else {
this.connectionPoolConfiguration = new ServiceConnectionPoolConfiguration();
}
if (webSocketCompressionConfiguration != null) {
this.webSocketCompressionConfiguration = webSocketCompressionConfiguration;
} else {
this.webSocketCompressionConfiguration = new ServiceWebSocketCompressionConfiguration();
}
}

/**
Expand Down Expand Up @@ -248,13 +275,25 @@ public ConnectionPoolConfiguration getConnectionPoolConfiguration() {
return connectionPoolConfiguration;
}

@Override
public WebSocketCompressionConfiguration getWebSocketCompressionConfiguration() {
return webSocketCompressionConfiguration;
}

/**
* The default connection pool configuration.
*/
@ConfigurationProperties(ConnectionPoolConfiguration.PREFIX)
public static class ServiceConnectionPoolConfiguration extends ConnectionPoolConfiguration {
}

/**
* The default WebSocket compression configuration.
*/
@ConfigurationProperties(WebSocketCompressionConfiguration.PREFIX)
public static class ServiceWebSocketCompressionConfiguration extends WebSocketCompressionConfiguration {
}

/**
* The default connection pool configuration.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,9 @@ protected void initChannel(@NonNull Channel ch) {
}

try {
ch.pipeline().addLast(WebSocketClientCompressionHandler.INSTANCE);
if (configuration.getWebSocketCompressionConfiguration() != null && configuration.getWebSocketCompressionConfiguration().isEnabled()) {
ch.pipeline().addLast(WebSocketClientCompressionHandler.INSTANCE);
}
ch.pipeline().addLast(ChannelPipelineCustomizer.HANDLER_MICRONAUT_WEBSOCKET_CLIENT, handler);
clientCustomizer.specializeForChannel(ch, NettyClientCustomizer.ChannelRole.CONNECTION).onInitialPipelineBuilt();
if (initial.tryEmitEmpty().isSuccess()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class DefaultNettyHttpClientRegistry implements AutoCloseable,
*/
public DefaultNettyHttpClientRegistry(
HttpClientConfiguration defaultHttpClientConfiguration,
HttpClientFilterResolver httpClientFilterResolver,
HttpClientFilterResolver<ClientFilterResolutionContext> httpClientFilterResolver,
LoadBalancerResolver loadBalancerResolver,
ClientSslBuilder nettyClientSslBuilder,
ThreadFactory threadFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.micronaut.http.client
import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Requires
import io.micronaut.core.io.socket.SocketUtils
import io.micronaut.http.client.DefaultHttpClientConfiguration.DefaultWebSocketCompressionConfiguration
import io.micronaut.http.client.annotation.Client
import io.micronaut.http.client.netty.DefaultHttpClient
import io.micronaut.runtime.ApplicationConfiguration
Expand Down Expand Up @@ -54,17 +55,27 @@ class ClientSpecificLoggerSpec extends Specification {

private final DefaultHttpClientConfiguration.DefaultConnectionPoolConfiguration connectionPoolConfiguration

private final DefaultHttpClientConfiguration.DefaultWebSocketCompressionConfiguration webSocketCompressionConfiguration

@Inject
ClientTwoHttpConfiguration(ApplicationConfiguration applicationConfiguration, DefaultHttpClientConfiguration.DefaultConnectionPoolConfiguration connectionPoolConfiguration) {
ClientTwoHttpConfiguration(ApplicationConfiguration applicationConfiguration,
DefaultHttpClientConfiguration.DefaultConnectionPoolConfiguration connectionPoolConfiguration,
DefaultWebSocketCompressionConfiguration webSocketCompressionConfiguration) {
super(applicationConfiguration)
this.connectionPoolConfiguration = connectionPoolConfiguration
this.webSocketCompressionConfiguration = webSocketCompressionConfiguration
}

@Override
ConnectionPoolConfiguration getConnectionPoolConfiguration() {
return this.connectionPoolConfiguration
}

@Override
WebSocketCompressionConfiguration getWebSocketCompressionConfiguration() {
return this.webSocketCompressionConfiguration
}

@Override
Optional<String> getLoggerName() {
return Optional.of("${ClientSpecificLoggerSpec.class}.client.two".toString())
Expand Down
Loading

0 comments on commit d07ddbb

Please sign in to comment.