Skip to content

Commit 391d3cd

Browse files
authored
support passing detect context to help build netty handlers (#12460)
1 parent 2031a4c commit 391d3cd

10 files changed

Lines changed: 91 additions & 32 deletions

File tree

dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/QosDetector.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ public QosDetector(FrameworkModel frameworkModel) {
3737
@Override
3838
public Result detect(ChannelBuffer in) {
3939
if(!QosEnableFlag) {
40-
return Result.UNRECOGNIZED;
40+
return Result.unrecognized();
4141
}
4242
Result h1Res = qosHTTP1Detector.detect(in);
43-
if(h1Res.equals(Result.RECOGNIZED)) {
43+
if(h1Res.equals(Result.recognized())) {
4444
return h1Res;
4545
}
4646
Result telRes = telnetDetector.detect(in);
47-
if(telRes.equals(Result.RECOGNIZED)) {
47+
if(telRes.equals(Result.recognized())) {
4848
return telRes;
4949
}
50-
if(h1Res.equals(Result.NEED_MORE_DATA) || telRes.equals(Result.NEED_MORE_DATA)) {
51-
return Result.NEED_MORE_DATA;
50+
if(h1Res.equals(Result.needMoreData()) || telRes.equals(Result.needMoreData())) {
51+
return Result.needMoreData();
5252
}
53-
return Result.UNRECOGNIZED;
53+
return Result.unrecognized();
5454
}
5555

5656
}

dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/QosHTTP1Detector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ private static boolean isHttp(int magic) {
2727
@Override
2828
public Result detect(ChannelBuffer in) {
2929
if (in.readableBytes() < 2) {
30-
return Result.NEED_MORE_DATA;
30+
return Result.needMoreData();
3131
}
3232
final int magic = in.getByte(in.readerIndex());
3333
// h2 starts with "PR"
3434
if (isHttp(magic) && in.getByte(in.readerIndex()+1) != 'R' ){
35-
return Result.RECOGNIZED;
35+
return Result.recognized();
3636
}
37-
return Result.UNRECOGNIZED;
37+
return Result.unrecognized();
3838
}
3939
}

dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/pu/TelnetDetector.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,20 @@ public TelnetDetector(FrameworkModel frameworkModel) {
4343
@Override
4444
public Result detect(ChannelBuffer in) {
4545
if (in.readableBytes() >= MaxSize) {
46-
return Result.UNRECOGNIZED;
46+
return Result.unrecognized();
4747
}
4848
Result resCommand = commandDetect(in);
49-
if (resCommand.equals(Result.RECOGNIZED)) {
49+
if (resCommand.equals(Result.recognized())) {
5050
return resCommand;
5151
}
5252
Result resAyt = telnetAytDetect(in);
53-
if (resAyt.equals(Result.RECOGNIZED)) {
53+
if (resAyt.equals(Result.recognized())) {
5454
return resAyt;
5555
}
56-
if (resAyt.equals(Result.UNRECOGNIZED) && resCommand.equals(Result.UNRECOGNIZED)) {
57-
return Result.UNRECOGNIZED;
56+
if (resAyt.equals(Result.unrecognized()) && resCommand.equals(Result.unrecognized())) {
57+
return Result.unrecognized();
5858
}
59-
return Result.NEED_MORE_DATA;
59+
return Result.needMoreData();
6060
}
6161

6262
private Result commandDetect(ChannelBuffer in) {
@@ -75,26 +75,26 @@ private Result commandDetect(ChannelBuffer in) {
7575
s = s.trim();
7676
CommandContext commandContext = TelnetCommandDecoder.decode(s);
7777
if (frameworkModel.getExtensionLoader(BaseCommand.class).hasExtension(commandContext.getCommandName())) {
78-
return Result.RECOGNIZED;
78+
return Result.recognized();
7979
}
80-
return Result.UNRECOGNIZED;
80+
return Result.unrecognized();
8181
}
8282

8383
private Result telnetAytDetect(ChannelBuffer in) {
8484
// detect if remote channel send a telnet ayt command to server
8585
int prefaceLen = AytPreface.readableBytes();
8686
int bytesRead = min(in.readableBytes(), prefaceLen);
8787
if (bytesRead == 0 || !ChannelBuffers.prefixEquals(in, AytPreface, bytesRead)) {
88-
return Result.UNRECOGNIZED;
88+
return Result.unrecognized();
8989
}
9090
if (bytesRead == prefaceLen) {
9191
// we need to consume preface because it's not a qos command
9292
// consume and remember to mark, pu server handler reset reader index
9393
in.readBytes(AytPreface.readableBytes());
9494
in.markReaderIndex();
95-
return Result.RECOGNIZED;
95+
return Result.recognized();
9696
}
97-
return Result.NEED_MORE_DATA;
97+
return Result.needMoreData();
9898
}
9999

100100
}

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/ProtocolDetector.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,59 @@
1919

2020
import org.apache.dubbo.remoting.buffer.ChannelBuffer;
2121

22+
import java.util.HashMap;
23+
import java.util.Map;
24+
2225

2326
/**
2427
* Determine incoming bytes belong to the specific protocol.
25-
*
2628
*/
2729
public interface ProtocolDetector {
2830

2931
Result detect(ChannelBuffer in);
3032

31-
enum Result {
33+
class Result {
34+
35+
private final Flag flag;
36+
37+
private final Map<String, String> detectContext = new HashMap<>(4);
38+
39+
private Result(Flag flag) {
40+
this.flag = flag;
41+
}
42+
43+
public void setAttribute(String key, String value) {
44+
this.detectContext.put(key, value);
45+
}
46+
47+
public String getAttribute(String key) {
48+
return this.detectContext.get(key);
49+
}
50+
51+
public void removeAttribute(String key) {
52+
this.detectContext.remove(key);
53+
}
54+
55+
public Flag flag() {
56+
return flag;
57+
}
58+
59+
public static Result recognized(){
60+
return new Result(Flag.RECOGNIZED);
61+
}
62+
63+
64+
public static Result unrecognized(){
65+
return new Result(Flag.UNRECOGNIZED);
66+
}
67+
68+
69+
public static Result needMoreData(){
70+
return new Result(Flag.NEED_MORE_DATA);
71+
}
72+
}
73+
74+
enum Flag {
3275
RECOGNIZED, UNRECOGNIZED, NEED_MORE_DATA
3376
}
3477
}

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/pu/ChannelOperator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
package org.apache.dubbo.remoting.api.pu;
1818

1919
import org.apache.dubbo.remoting.ChannelHandler;
20+
import org.apache.dubbo.remoting.api.ProtocolDetector;
2021

2122
import java.util.List;
2223

2324
public interface ChannelOperator {
2425
void configChannelHandler(List<ChannelHandler> handlerList);
26+
27+
ProtocolDetector.Result detectResult();
2528
}

dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyConfigOperator.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.dubbo.remoting.Codec;
2525
import org.apache.dubbo.remoting.Codec2;
2626
import org.apache.dubbo.remoting.Constants;
27+
import org.apache.dubbo.remoting.api.ProtocolDetector;
2728
import org.apache.dubbo.remoting.api.pu.ChannelHandlerPretender;
2829
import org.apache.dubbo.remoting.api.pu.ChannelOperator;
2930
import org.apache.dubbo.remoting.api.pu.DefaultCodec;
@@ -36,6 +37,8 @@ public class NettyConfigOperator implements ChannelOperator {
3637
private final Channel channel;
3738
private ChannelHandler handler;
3839

40+
private ProtocolDetector.Result detectResult;
41+
3942
public NettyConfigOperator(NettyChannel channel, ChannelHandler handler) {
4043
this.channel = channel;
4144
this.handler = handler;
@@ -91,6 +94,15 @@ public void configChannelHandler(List<ChannelHandler> handlerList) {
9194
}
9295
}
9396

97+
public void setDetectResult(ProtocolDetector.Result detectResult) {
98+
this.detectResult = detectResult;
99+
}
100+
101+
@Override
102+
public ProtocolDetector.Result detectResult() {
103+
return detectResult;
104+
}
105+
94106
private boolean isClientSide(Channel channel) {
95107
return channel.getUrl().getSide("").equalsIgnoreCase(CommonConstants.CONSUMER);
96108
}

dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServerHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
107107
ChannelBuffer buf = new NettyBackedChannelBuffer(in);
108108
final ProtocolDetector.Result result = protocol.detector().detect(buf);
109109
in.resetReaderIndex();
110-
switch (result) {
110+
switch (result.flag()) {
111111
case UNRECOGNIZED:
112112
continue;
113113
case RECOGNIZED:
@@ -117,6 +117,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
117117
URL localURL = this.urlMapper.getOrDefault(protocolName, url);
118118
channel.setUrl(localURL);
119119
NettyConfigOperator operator = new NettyConfigOperator(channel, localHandler);
120+
operator.setDetectResult(result);
120121
protocol.configServerProtocolHandler(url, operator);
121122
ctx.pipeline().remove(this);
122123
case NEED_MORE_DATA:

dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/pu/DubboDetector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ public Result detect(ChannelBuffer in) {
3636
int bytesRead = min(in.readableBytes(), prefaceLen);
3737

3838
if (bytesRead ==0 || !ChannelBuffers.prefixEquals(in, Preface, bytesRead)) {
39-
return Result.UNRECOGNIZED;
39+
return Result.unrecognized();
4040
}
4141
if (bytesRead == prefaceLen) {
42-
return Result.RECOGNIZED;
42+
return Result.recognized();
4343
}
4444

45-
return Result.NEED_MORE_DATA;
45+
return Result.needMoreData();
4646
}
4747
}

dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/Http2ProtocolDetector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public Result detect(ChannelBuffer in) {
3636

3737
// If the input so far doesn't match the preface, break the connection.
3838
if (bytesRead == 0 || !ChannelBuffers.prefixEquals(in, clientPrefaceString, bytesRead)) {
39-
return Result.UNRECOGNIZED;
39+
return Result.unrecognized();
4040
}
4141
if (bytesRead == prefaceLen) {
42-
return Result.RECOGNIZED;
42+
return Result.recognized();
4343
}
44-
return Result.NEED_MORE_DATA;
44+
return Result.needMoreData();
4545
}
4646
}

dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/Http2ProtocolDetectorTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ void testDetect() {
4242
ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
4343
ChannelBuffer in = new ByteBufferBackedChannelBuffer(byteBuf.nioBuffer());
4444
ProtocolDetector.Result result = detector.detect(in);
45-
Assertions.assertEquals(result, ProtocolDetector.Result.UNRECOGNIZED);
45+
Assertions.assertEquals(result.flag(), ProtocolDetector.Result.unrecognized().flag());
4646

4747
byteBuf.writeBytes(connectionPrefaceBuf);
4848
result = detector.detect(new ByteBufferBackedChannelBuffer(byteBuf.nioBuffer()));
49-
Assertions.assertEquals(result, ProtocolDetector.Result.RECOGNIZED);
49+
Assertions.assertEquals(result.flag(), ProtocolDetector.Result.recognized().flag());
5050

5151
byteBuf.clear();
5252
byteBuf.writeBytes(connectionPrefaceBuf, 0, 1);
5353
result = detector.detect(new ByteBufferBackedChannelBuffer(byteBuf.nioBuffer()));
54-
Assertions.assertEquals(result, ProtocolDetector.Result.NEED_MORE_DATA);
54+
Assertions.assertEquals(result.flag(), ProtocolDetector.Result.needMoreData().flag());
5555

5656
}
5757
}

0 commit comments

Comments
 (0)