Skip to content

Commit 70956d4

Browse files
author
Nitesh Kant
committed
Adding a short-circuit interception sample.
1 parent 46892b0 commit 70956d4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

ripc-transport-netty4-examples/src/main/java/io/ripc/transport/netty4/tcp/TcpServerSample.java

+16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
1010

11+
import java.util.concurrent.atomic.AtomicLong;
12+
1113
import static java.nio.charset.Charset.defaultCharset;
1214
import static rx.RxReactiveStreams.*;
1315

@@ -22,9 +24,23 @@ public static TcpInterceptor<ByteBuf, ByteBuf, ByteBuf, ByteBuf> log() {
2224
};
2325
}
2426

27+
public static TcpInterceptor<ByteBuf, ByteBuf, ByteBuf, ByteBuf> shortCircuitAltConnection() {
28+
return handler -> {
29+
final AtomicLong connCounter = new AtomicLong();
30+
return input -> {
31+
if (connCounter.incrementAndGet() % 2 == 0) {
32+
logger.error("Short-circuiting further processing.");
33+
return input.write(Publishers.just(Unpooled.buffer().writeBytes("Go Away!!! \n".getBytes())));
34+
}
35+
return handler.handle(input);
36+
};
37+
};
38+
}
39+
2540
public static void main(String[] args) {
2641
Netty4TcpServer.<ByteBuf, ByteBuf>create(0)
2742
.intercept(log())
43+
.intercept(shortCircuitAltConnection())
2844
.start(connection ->
2945
toPublisher(toObservable(connection)
3046
.flatMap(byteBuf -> {

0 commit comments

Comments
 (0)