-
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 6, 2024
1 parent
97e9741
commit 1710ed2
Showing
22 changed files
with
364 additions
and
34 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
7 changes: 7 additions & 0 deletions
7
core/src/main/java/com/ctrip/xpipe/netty/commands/DoAfterConnectedHandler.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,7 @@ | ||
package com.ctrip.xpipe.netty.commands; | ||
|
||
public interface DoAfterConnectedHandler { | ||
|
||
boolean shouldDo(); | ||
|
||
} |
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
28 changes: 28 additions & 0 deletions
28
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,28 @@ | ||
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; | ||
|
||
private DoAfterConnectedHandler doAfterConnectedHandler; | ||
|
||
public NettyRedisPoolClientFactory(int eventLoopThreads, String clientName, DoAfterConnectedHandler doAfterConnectedHandler) { | ||
super(eventLoopThreads); | ||
this.clientName = clientName; | ||
this.doAfterConnectedHandler = doAfterConnectedHandler; | ||
} | ||
|
||
@Override | ||
public PooledObject<NettyClient> makeObject(Endpoint key) throws Exception { | ||
ChannelFuture f = b.connect(key.getHost(), key.getPort()); | ||
NettyClient nettyClient = new RedisAsyncNettyClient(f, key, clientName, doAfterConnectedHandler); | ||
f.channel().attr(NettyClientHandler.KEY_CLIENT).set(nettyClient); | ||
return new DefaultPooledObject<NettyClient>(nettyClient); | ||
} | ||
|
||
} |
139 changes: 139 additions & 0 deletions
139
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,139 @@ | ||
package com.ctrip.xpipe.netty.commands; | ||
|
||
import com.ctrip.xpipe.api.codec.Codec; | ||
import com.ctrip.xpipe.api.endpoint.Endpoint; | ||
import com.ctrip.xpipe.api.monitor.Task; | ||
import com.ctrip.xpipe.api.monitor.TransactionMonitor; | ||
import com.ctrip.xpipe.utils.ChannelUtil; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelFuture; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class RedisAsyncNettyClient extends AsyncNettyClient{ | ||
|
||
protected Logger logger = LoggerFactory.getLogger(RedisAsyncNettyClient.class); | ||
|
||
private String clientName; | ||
|
||
private DoAfterConnectedHandler doAfterConnectedHandler; | ||
|
||
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, DoAfterConnectedHandler doAfterConnectedHandler) { | ||
super(future, endpoint); | ||
this.clientName = clientName; | ||
this.doAfterConnectedHandler = doAfterConnectedHandler; | ||
} | ||
|
||
@Override | ||
protected void doAfterConnected() { | ||
if (doAfterConnectedHandler != null && !doAfterConnectedHandler.shouldDo()) { | ||
return; | ||
} | ||
TransactionMonitor transaction = TransactionMonitor.DEFAULT; | ||
transaction.logTransactionSwallowException("netty.client.setName", desc.get(), new Task() { | ||
@Override | ||
public void go() throws Exception { | ||
String command = CLIENT_SET_NAME + clientName + "\r\n"; | ||
ByteBuf byteBuf = Unpooled.wrappedBuffer(command.getBytes(Codec.defaultCharset)); | ||
RedisAsyncNettyClient.super.sendRequest(byteBuf, new ByteBufReceiver() { | ||
@Override | ||
public RECEIVER_RESULT receive(Channel channel, ByteBuf byteBuf) { | ||
String result = doReceiveResponse(byteBuf); | ||
if (result == null) { | ||
logger.warn("[redisAsync][clientSetName][wont-result][{}] {}", desc, result); | ||
} | ||
if (EXPECT_RESP.equalsIgnoreCase(result)) { | ||
logger.info("[redisAsync][clientSetName][success][{}] {}", desc, result); | ||
} | ||
return RECEIVER_RESULT.SUCCESS; | ||
} | ||
|
||
@Override | ||
public void clientClosed(NettyClient nettyClient) { | ||
logger.warn("[redisAsync][clientSetName][wont-send][{}] {}", desc, nettyClient.channel()); | ||
} | ||
|
||
@Override | ||
public void clientClosed(NettyClient nettyClient, Throwable th) { | ||
logger.warn("[redisAsync][clientSetName][wont-send][{}] {}", desc, nettyClient.channel(), th); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Map getData() { | ||
Map<String, Object> transactionData = new HashMap<>(); | ||
transactionData.put("remoteAddress ", ChannelUtil.getSimpleIpport(channel.remoteAddress())); | ||
transactionData.put("clientName", clientName); | ||
transactionData.put("commandTimeoutMills", getAfterConnectCommandTimeoutMill()); | ||
return transactionData; | ||
} | ||
}); | ||
|
||
} | ||
|
||
@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
Oops, something went wrong.