@Override public CompletableFuture<Void> releaseAsync(StatefulConnection<?, ?> connection) { return connection.closeAsync().exceptionally(LettuceFutureUtils.ignoreErrors()) .thenCompose(it -> redisClient.shutdownAsync()); } }
/** * Release the {@link StatefulConnection connection}. Closes connection {@link StatefulConnection#close()} by default. * Implementations may choose whether they override this method and return the connection to a pool. * * @param connection must not be {@literal null}. */ default void release(StatefulConnection<?, ?> connection) { LettuceFutureUtils.join(releaseAsync(connection)); }
@Override public CompletableFuture<Void> releaseAsync(StatefulConnection<?, ?> connection) { GenericObjectPool<StatefulConnection<?, ?>> blockingPool = poolRef.remove(connection); if (blockingPool != null) { log.warn("Releasing asynchronously a connection that was obtained from a non-blocking pool"); blockingPool.returnObject(connection); return CompletableFuture.completedFuture(null); } AsyncPool<StatefulConnection<?, ?>> pool = asyncPoolRef.remove(connection); if (pool == null) { return LettuceFutureUtils.failed(new PoolException("Returned connection " + connection + " was either previously returned or does not belong to this connection provider")); } return pool.release(connection); }
@Override public <T extends StatefulConnection<?, ?>> CompletableFuture<T> getConnectionAsync(Class<T> connectionType) { if (!initialized) { // partitions have to be initialized before asynchronous usage. // Needs to happen only once. Initialize eagerly if // blocking is not an options. synchronized (monitor) { if (!initialized) { client.getPartitions(); initialized = true; } } } if (connectionType.equals(StatefulRedisPubSubConnection.class)) { return client.connectPubSubAsync(codec) // .thenApply(connectionType::cast); } if (StatefulRedisClusterConnection.class.isAssignableFrom(connectionType) || connectionType.equals(StatefulConnection.class)) { return client.connectAsync(codec) // .thenApply(connection -> { readFrom.ifPresent(connection::setReadFrom); return connectionType.cast(connection); }); } return LettuceFutureUtils .failed(new UnsupportedOperationException("Connection type " + connectionType + " not supported!")); }
/** * Request a connection given {@code connectionType}. Providing a connection type allows specialization to provide a * more specific connection type. * * @param connectionType must not be {@literal null}. * @return the requested connection. Must be {@link #release(StatefulConnection) released} if the connection is no * longer in use. */ default <T extends StatefulConnection<?, ?>> T getConnection(Class<T> connectionType) { return LettuceFutureUtils.join(getConnectionAsync(connectionType)); }
@SuppressWarnings({ "null", "unchecked", "rawtypes" }) @Override public <T extends StatefulConnection<?, ?>> CompletionStage<T> getConnectionAsync(Class<T> connectionType, RedisURI redisURI) { if (connectionType.equals(StatefulRedisSentinelConnection.class)) { return client.connectSentinelAsync(StringCodec.UTF8, redisURI).thenApply(connectionType::cast); } if (connectionType.equals(StatefulRedisPubSubConnection.class)) { return client.connectPubSubAsync(codec, redisURI).thenApply(connectionType::cast); } if (StatefulConnection.class.isAssignableFrom(connectionType)) { return readFrom.map(it -> this.masterReplicaConnectionAsync(redisURI, it)) // .orElseGet(() -> (CompletionStage) client.connectAsync(codec, redisURI)) // .thenApply(connectionType::cast); } return LettuceFutureUtils .failed(new UnsupportedOperationException("Connection type " + connectionType + " not supported!")); }
.allOf(futures.stream().map(it -> it.exceptionally(LettuceFutureUtils.ignoreErrors())) .toArray(CompletableFuture[]::new)) // .thenCompose(ignored -> { .map(it -> it.exceptionally(LettuceFutureUtils.ignoreErrors())).toArray(CompletableFuture[]::new);
/** * Request a connection given {@code connectionType} for a specific {@link RedisURI}. Providing a connection type * allows specialization to provide a more specific connection type. * * @param connectionType must not be {@literal null}. * @param redisURI must not be {@literal null}. * @return the requested connection. */ default <T extends StatefulConnection<?, ?>> T getConnection(Class<T> connectionType, RedisURI redisURI) { return LettuceFutureUtils.join(getConnectionAsync(connectionType, redisURI)); }
/** * Close connection (blocking call). */ void close() { if (state.compareAndSet(State.INITIAL, CLOSING) || state.compareAndSet(State.CONNECTION_REQUESTED, CLOSING)) { StatefulConnection<ByteBuffer, ByteBuffer> connection = this.connection; this.connection = null; if (connection != null) { LettuceFutureUtils.join(connectionProvider.releaseAsync(connection)); } state.set(State.CLOSED); } }