use of io.lettuce.core.api.StatefulConnection in project lettuce-core by lettuce-io.
the class ReactiveBackpressurePropagationUnitTests method writeCommandAndCancelInTheMiddle.
@Test
void writeCommandAndCancelInTheMiddle() throws Exception {
Command<String, String, List<String>> lrange = new Command<>(CommandType.LRANGE, new ValueListOutput<>(StringCodec.UTF8));
RedisPublisher<String, String, String> publisher = new RedisPublisher<>(lrange, statefulConnection, true, ImmediateEventExecutor.INSTANCE);
CountDownLatch pressureArrived = new CountDownLatch(1);
CountDownLatch buildPressure = new CountDownLatch(1);
CountDownLatch waitForPressureReduced = new CountDownLatch(2);
Disposable cancellation = Flux.from(publisher).limitRate(2).publishOn(Schedulers.single()).doOnNext(s -> {
try {
pressureArrived.countDown();
buildPressure.await();
} catch (InterruptedException e) {
}
waitForPressureReduced.countDown();
}).subscribe();
assertThat(embeddedChannel.config().isAutoRead()).isTrue();
// produce some back pressure
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.arrayHeader(4)));
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("one")));
pressureArrived.await();
assertThat(embeddedChannel.config().isAutoRead()).isTrue();
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("two")));
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("three")));
assertThat(embeddedChannel.config().isAutoRead()).isFalse();
cancellation.dispose();
assertThat(embeddedChannel.config().isAutoRead()).isTrue();
// allow processing
buildPressure.countDown();
// emit the last item
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("four")));
// done
assertThat(embeddedChannel.config().isAutoRead()).isTrue();
}
use of io.lettuce.core.api.StatefulConnection in project lettuce-core by lettuce-io.
the class ReactiveBackpressurePropagationUnitTests method writeCommand.
@Test
void writeCommand() throws Exception {
Command<String, String, List<String>> lrange = new Command<>(CommandType.LRANGE, new ValueListOutput<>(StringCodec.UTF8));
RedisPublisher<String, String, String> publisher = new RedisPublisher<>((Command) lrange, statefulConnection, true, ImmediateEventExecutor.INSTANCE);
CountDownLatch pressureArrived = new CountDownLatch(1);
CountDownLatch buildPressure = new CountDownLatch(1);
CountDownLatch waitForPressureReduced = new CountDownLatch(2);
CountDownLatch waitForWorkCompleted = new CountDownLatch(4);
Flux.from(publisher).limitRate(2).publishOn(Schedulers.single()).doOnNext(s -> {
try {
pressureArrived.countDown();
buildPressure.await();
} catch (InterruptedException e) {
}
waitForPressureReduced.countDown();
waitForWorkCompleted.countDown();
}).subscribe();
assertThat(embeddedChannel.config().isAutoRead()).isTrue();
// produce some back pressure
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.arrayHeader(4)));
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("one")));
pressureArrived.await();
assertThat(embeddedChannel.config().isAutoRead()).isTrue();
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("two")));
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("three")));
assertThat(embeddedChannel.config().isAutoRead()).isFalse();
// allow processing
buildPressure.countDown();
// wait until processing caught up
waitForPressureReduced.await();
assertThat(embeddedChannel.config().isAutoRead()).isTrue();
// emit the last item
embeddedChannel.writeInbound(Unpooled.wrappedBuffer(RESP.bulkString("four")));
// done
waitForWorkCompleted.await();
assertThat(embeddedChannel.config().isAutoRead()).isTrue();
}
use of io.lettuce.core.api.StatefulConnection in project lettuce-core by lettuce-io.
the class ConnectionDecoratingInvocationHandler method handleInvocation.
@Override
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
Method targetMethod = target.getClass().getMethod(method.getName(), method.getParameterTypes());
Method proxyMethod = proxy.getClass().getMethod(method.getName(), method.getParameterTypes());
Object result = targetMethod.invoke(target, args);
if (result instanceof StatefulConnection) {
Class[] interfaces;
if (result instanceof StatefulRedisClusterConnection && proxyMethod.getReturnType().isAssignableFrom(StatefulRedisClusterConnection.class)) {
interfaces = new Class[] { StatefulConnection.class, StatefulRedisClusterConnection.class };
} else if (result instanceof StatefulRedisSentinelConnection && proxyMethod.getReturnType().isAssignableFrom(StatefulRedisSentinelConnection.class)) {
interfaces = new Class[] { StatefulConnection.class, StatefulRedisSentinelConnection.class };
} else {
interfaces = new Class[] { StatefulConnection.class, StatefulRedisConnection.class };
}
return Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, new ConnectionDecoratingInvocationHandler(result));
}
return result;
}
use of io.lettuce.core.api.StatefulConnection in project lettuce-core by lettuce-io.
the class LettuceExtension method afterAll.
@Override
public void afterAll(ExtensionContext context) {
ExtensionContext.Store store = getStore(context);
CLOSE_AFTER_EACH.forEach(it -> {
StatefulConnection connection = store.get(it, StatefulConnection.class);
if (connection != null) {
connection.close();
store.remove(StatefulRedisConnection.class);
}
});
}
use of io.lettuce.core.api.StatefulConnection in project lcache by long172066912.
the class TestRedisCache2 method testPubSub.
@Test
public void testPubSub() {
for (int p = 0; p < 10; p++) {
int finalP = p;
new Thread(() -> {
BaseCacheExecutor baseCacheExecutor = null;
if (finalP % 2 == 0) {
baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.newCache("test"), new LettuceConnectSourceConfig());
} else {
baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.newCache("friend"));
}
int i = 0;
while (true) {
i++;
System.out.println("Lettuce发布消息:" + i);
try {
baseCacheExecutor.publish("test", "test" + i);
Thread.sleep(1000L);
} catch (Exception e) {
e.printStackTrace();
StatefulConnection connectResource = (StatefulConnection) baseCacheExecutor.getConnectResource();
if (!connectResource.isOpen()) {
System.out.println("关闭连接" + connectResource);
baseCacheExecutor.close();
}
baseCacheExecutor.returnConnectResource();
}
}
}).start();
}
new Thread(() -> {
BaseCacheExecutor baseCacheExecutor = null;
baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.newCache("test"), new LettuceConnectSourceConfig());
baseCacheExecutor.subscribe((message) -> {
System.out.println("Lettuce Local订阅消息:" + message);
}, "test");
}).start();
new Thread(() -> {
BaseCacheExecutor baseCacheExecutor = null;
baseCacheExecutor = CacheClientFactory.getCacheExecutor(CacheConfigModel.newCache("friend"));
baseCacheExecutor.subscribe((message) -> {
System.out.println("Lettuce Dev订阅消息:" + message);
}, "test");
}).start();
while (true) {
}
}
Aggregations