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

Address feedback for connection pool #274

Merged
merged 2 commits into from
Mar 15, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.micronaut.configuration.lettuce;

import io.micronaut.core.annotation.NonNull;

import java.util.Optional;

/**
Expand All @@ -34,47 +36,31 @@ public abstract class AbstractRedisConnectionPoolConfiguration {
* @return The minimum idle connections count.
* @see io.micronaut.configuration.lettuce.NamedRedisServersConfiguration
*/
public Optional<Integer> getMinIdle() {
if (minIdle != null) {
return Optional.of(minIdle);
} else {
return Optional.empty();
}
public @NonNull Optional<Integer> getMinIdle() {
return Optional.ofNullable(minIdle);
}

/**
* @return The maximum idle connections count.
* @see io.micronaut.configuration.lettuce.NamedRedisServersConfiguration
*/
public Optional<Integer> getMaxIdle() {
if (maxIdle != null) {
return Optional.of(maxIdle);
} else {
return Optional.empty();
}
public @NonNull Optional<Integer> getMaxIdle() {
return Optional.ofNullable(maxIdle);
}

/**
* @return The maximum total connections count.
* @see io.micronaut.configuration.lettuce.NamedRedisServersConfiguration
*/
public Optional<Integer> getMaxTotal() {
if (maxTotal != null) {
return Optional.of(maxTotal);
} else {
return Optional.empty();
}
public @NonNull Optional<Integer> getMaxTotal() {
return Optional.ofNullable(maxTotal);
}

/**
* @return The maximum total connections count.
* @see io.micronaut.configuration.lettuce.NamedRedisServersConfiguration
*/
public Optional<Boolean> getEnabled() {
if (enabled != null) {
return Optional.of(enabled);
} else {
return Optional.empty();
}
public @NonNull Optional<Boolean> getEnabled() {
return Optional.ofNullable(enabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* Allows configuration of redis connection pool.
*
* @author Graeme Rocher, Illia Kovalov
* @since 1.3
* @since 5.3.0
*/
@ConfigurationProperties(RedisSetting.REDIS_POOL)
@Requires(classes = SyncCache.class, property = RedisSetting.REDIS_POOL + ".enabled", defaultValue = StringUtils.FALSE, notEquals = StringUtils.FALSE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.micronaut.context.BeanLocator;
import io.micronaut.context.exceptions.ConfigurationException;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.inject.qualifiers.Qualifiers;

import java.util.Optional;
Expand All @@ -46,7 +47,7 @@ public class RedisConnectionUtil {
* @return The connection
* @throws ConfigurationException If the connection cannot be found
*/
public static AbstractRedisClient findClient(BeanLocator beanLocator, Optional<String> serverName, String errorMessage) {
public static @NonNull AbstractRedisClient findClient(BeanLocator beanLocator, Optional<String> serverName, String errorMessage) {
Optional<? extends AbstractRedisClient> clusterConn = findRedisClusterClient(beanLocator, serverName);
if (clusterConn.isPresent()) {
return clusterConn.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@

/**
* An abstract class implementing SyncCache for the redis.
* Author: Graeme Rocher, Kovalov Illia
*
* @author Graeme Rocher, Kovalov Illia
* @param <C> – The native cache implementation
* @since 5.3.0
*/
public abstract class AbstractRedisCache<C> implements SyncCache<C>, AutoCloseable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,21 @@

/**
* Default redis connection pool factory.
* Author: Kovalov Illia
*
* @author Kovalov Illia
* @since 5.3.0
*/
@Factory
public final class RedisAsyncConnectionPoolFactory {
private final BeanLocator beanLocator;
private final DefaultRedisCacheConfiguration defaultRedisCacheConfiguration;
private final DefaultRedisConnectionPoolConfiguration defaultRedisConnectionPoolConfiguration;

public RedisAsyncConnectionPoolFactory(
@Singleton
public AsyncPool<StatefulConnection<byte[], byte[]>> getAsyncPool(
DefaultRedisCacheConfiguration defaultRedisCacheConfiguration,
BeanLocator beanLocator,
DefaultRedisConnectionPoolConfiguration defaultRedisConnectionPoolConfiguration
) {
this.beanLocator = beanLocator;
this.defaultRedisCacheConfiguration = defaultRedisCacheConfiguration;
this.defaultRedisConnectionPoolConfiguration = defaultRedisConnectionPoolConfiguration;
}

@Singleton
public AsyncPool<StatefulConnection<byte[], byte[]>> getAsyncPool() {
Optional<String> server = defaultRedisCacheConfiguration.getServer();
AbstractRedisClient client = RedisConnectionUtil.findClient(this.beanLocator, server, "No Redis server configured to allow caching");
AbstractRedisClient client = RedisConnectionUtil.findClient(beanLocator, server, "No Redis server configured to allow caching");
BoundedPoolConfig asyncConfig = defaultRedisConnectionPoolConfiguration.getBoundedPoolConfig();
CompletionStage<BoundedAsyncPool<StatefulConnection<byte[], byte[]>>> stage = AsyncConnectionPoolSupport.createBoundedObjectPoolAsync(() -> {
if (client instanceof RedisClusterClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.micronaut.context.BeanLocator;
import io.micronaut.context.annotation.EachBean;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.StringUtils;
Expand Down Expand Up @@ -160,6 +161,38 @@ protected <T> void putValue(byte[] serializedKey, T value) {
value);
}

@Override
@NonNull
@SuppressWarnings("java:S1185") // This is here for binary compatibility
public <T> Optional<T> get(Object key, Argument<T> requiredType) {
return super.get(key, requiredType);
}

@Override
@SuppressWarnings("java:S1185") // This is here for binary compatibility
public void put(Object key, Object value) {
super.put(key, value);
}

@Override
@NonNull
@SuppressWarnings("java:S1185") // This is here for binary compatibility
public <T> Optional<T> putIfAbsent(Object key, T value) {
return super.putIfAbsent(key, value);
}

@Override
@SuppressWarnings("java:S1185") // This is here for binary compatibility
protected String getKeysPattern() {
return super.getKeysPattern();
}

@Override
@SuppressWarnings("java:S1185") // This is here for binary compatibility
protected byte[] serializeKey(Object key) {
return super.serializeKey(key);
}

@PreDestroy
@Override
public void close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* An implementation of {@link SyncCache} for Lettuce / Redis using connection pooling.
*
* @author Graeme Rocher, Kovalov Illia
* @since 1.0
* @since 5.3.0
*/
@EachBean(RedisCacheConfiguration.class)
@Requires(classes = SyncCache.class, property = RedisSetting.REDIS_POOL + ".enabled", defaultValue = StringUtils.FALSE, notEquals = StringUtils.FALSE)
Expand Down