-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yifuzhou
committed
Nov 5, 2024
1 parent
97e9741
commit a4cd17e
Showing
13 changed files
with
218 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
core/src/main/java/com/ctrip/xpipe/netty/commands/NettyRedisPoolClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.ctrip.xpipe.netty.commands; | ||
|
||
import com.ctrip.xpipe.api.endpoint.Endpoint; | ||
import io.netty.channel.ChannelFuture; | ||
import org.apache.commons.pool2.PooledObject; | ||
import org.apache.commons.pool2.impl.DefaultPooledObject; | ||
|
||
public class NettyRedisPoolClientFactory extends NettyKeyedPoolClientFactory{ | ||
|
||
private String clientName; | ||
|
||
public NettyRedisPoolClientFactory(int eventLoopThreads, String clientName) { | ||
super(eventLoopThreads); | ||
this.clientName = clientName; | ||
} | ||
|
||
@Override | ||
public PooledObject<NettyClient> makeObject(Endpoint key) throws Exception { | ||
ChannelFuture f = b.connect(key.getHost(), key.getPort()); | ||
NettyClient nettyClient = new RedisAsyncNettyClient(f, key, clientName); | ||
f.channel().attr(NettyClientHandler.KEY_CLIENT).set(nettyClient); | ||
return new DefaultPooledObject<NettyClient>(nettyClient); | ||
} | ||
|
||
} |
117 changes: 117 additions & 0 deletions
117
core/src/main/java/com/ctrip/xpipe/netty/commands/RedisAsyncNettyClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.ctrip.xpipe.netty.commands; | ||
|
||
import com.ctrip.xpipe.api.codec.Codec; | ||
import com.ctrip.xpipe.api.endpoint.Endpoint; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.util.ReferenceCountUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
|
||
public class RedisAsyncNettyClient extends AsyncNettyClient{ | ||
|
||
protected Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
private String clientName; | ||
|
||
private static final String CLIENT_SET_NAME = "CLIENT SETNAME "; | ||
|
||
private static final String EXPECT_RESP = "OK"; | ||
|
||
private static final int DEFAULT_REDIS_COMMAND_TIME_OUT_MILLI = 660; | ||
|
||
public RedisAsyncNettyClient(ChannelFuture future, Endpoint endpoint, String clientName) { | ||
super(future, endpoint); | ||
this.clientName = clientName; | ||
} | ||
|
||
@Override | ||
protected void doAfterConnected() { | ||
String command = CLIENT_SET_NAME + clientName + "\r\n"; | ||
ByteBuf byteBuf = Unpooled.wrappedBuffer(command.getBytes(Codec.defaultCharset)); | ||
try { | ||
sendRequest(byteBuf, new ByteBufReceiver() { | ||
@Override | ||
public RECEIVER_RESULT receive(Channel channel, ByteBuf byteBuf) { | ||
String result = doReceiveResponse(byteBuf); | ||
if (result == null) { | ||
return RECEIVER_RESULT.CONTINUE; | ||
} | ||
if (EXPECT_RESP.equalsIgnoreCase(result)) { | ||
return RECEIVER_RESULT.SUCCESS; | ||
} | ||
return RECEIVER_RESULT.FAIL; | ||
} | ||
|
||
@Override | ||
public void clientClosed(NettyClient nettyClient) { | ||
logger.warn("[redisAsync][wont-send][{}] {}", desc, nettyClient.channel()); | ||
} | ||
|
||
@Override | ||
public void clientClosed(NettyClient nettyClient, Throwable th) { | ||
logger.warn("[redisAsync][wont-send][{}] {}", desc, nettyClient.channel(), th); | ||
} | ||
}); | ||
} finally { | ||
ReferenceCountUtil.release(byteBuf); | ||
} | ||
} | ||
|
||
@Override | ||
protected int getAfterConnectCommandTimeoutMill() { | ||
return DEFAULT_REDIS_COMMAND_TIME_OUT_MILLI; | ||
} | ||
|
||
public static String doReceiveResponse(ByteBuf byteBuf) { | ||
ByteArrayOutputStream baous = new ByteArrayOutputStream(); | ||
CRLF_STATE crlfState = CRLF_STATE.CONTENT; | ||
int readable = byteBuf.readableBytes(); | ||
for(int i=0; i < readable ;i++){ | ||
|
||
byte data = byteBuf.readByte(); | ||
baous.write(data); | ||
switch(data){ | ||
case '\r': | ||
crlfState = CRLF_STATE.CR; | ||
break; | ||
case '\n': | ||
if(crlfState == CRLF_STATE.CR){ | ||
crlfState = CRLF_STATE.CRLF; | ||
break; | ||
} | ||
default: | ||
crlfState = CRLF_STATE.CONTENT; | ||
break; | ||
} | ||
|
||
if(crlfState == CRLF_STATE.CRLF){ | ||
break; | ||
} | ||
} | ||
if (baous.toByteArray().length != 0 && crlfState == CRLF_STATE.CRLF) { | ||
String data = new String(baous.toByteArray(), Codec.defaultCharset); | ||
int beginIndex = 0; | ||
int endIndex = data.length(); | ||
if(data.charAt(0) == '+'){ | ||
beginIndex = 1; | ||
} | ||
if(data.charAt(endIndex - 2) == '\r' && data.charAt(endIndex - 1) == '\n'){ | ||
endIndex -= 2; | ||
} | ||
return data.substring(beginIndex, endIndex); | ||
} | ||
return null; | ||
} | ||
|
||
public enum CRLF_STATE{ | ||
CR, | ||
CRLF, | ||
CONTENT | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.