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

Allow to prepare the pool #3072

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -55,6 +55,7 @@
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Asmir Mustafic
* @since 2.0
* @see #getConnection(Class)
*/
Expand Down Expand Up @@ -90,8 +91,18 @@ class LettucePoolingConnectionProvider implements LettuceConnectionProvider, Red
public <T extends StatefulConnection<?, ?>> T getConnection(Class<T> connectionType) {

GenericObjectPool<StatefulConnection<?, ?>> pool = pools.computeIfAbsent(connectionType, poolType -> {
return ConnectionPoolSupport.createGenericObjectPool(() -> connectionProvider.getConnection(connectionType),
poolConfig, false);

GenericObjectPool<StatefulConnection<?, ?>> newPool = ConnectionPoolSupport
.createGenericObjectPool(() -> connectionProvider.getConnection(connectionType), poolConfig, false);

try {
newPool.preparePool();

} catch (Exception ex) {
throw new PoolException("Could not prepare the pool", ex);
}

return newPool;
});

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder;

/**
* Unit tests for {@link LettucePoolingConnectionProvider}.
*
* @author Mark Paluch
* @author Asmir Mustafic
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
Expand Down Expand Up @@ -70,4 +73,21 @@ void shouldDiscardTransactionOnReleaseOnActiveTransaction() {

verify(commandsMock).discard();
}

@Test
void shouldPrepareThePool() {

GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMinIdle(5);
poolConfig.setMaxIdle(8);
poolConfig.setMaxTotal(10);

LettucePoolingClientConfiguration config = new LettucePoolingClientConfigurationBuilder().poolConfig(poolConfig)
.build();

LettucePoolingConnectionProvider provider = new LettucePoolingConnectionProvider(connectionProviderMock, config);

provider.getConnection(StatefulRedisConnection.class);
verify(connectionProviderMock, times(5)).getConnection(any());
}
}