Skip to content

Commit 937bcec

Browse files
committedMar 24, 2020
减少缓存时间
1 parent 7b8b332 commit 937bcec

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed
 

‎proxy/src/main/java/com/jhl/cache/ConnectionStatsCache.java

+16-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.google.common.cache.Cache;
44
import com.google.common.cache.CacheBuilder;
55
import com.jhl.pojo.AccountConnectionStat;
6-
import com.jhl.service.ProxyAccountService;
76
import com.jhl.task.GlobalConnectionStatTask;
87
import com.jhl.task.service.TaskService;
98
import com.jhl.utils.SynchronizedInternerUtils;
@@ -22,7 +21,7 @@
2221
public class ConnectionStatsCache {
2322

2423
private static final Cache<Object, AccountConnectionStat> ACCOUNT_CONNECTION_COUNT_STATS = CacheBuilder.newBuilder()
25-
.expireAfterAccess(ProxyAccountService.ACCOUNT_EXPIRE_TIME, TimeUnit.MINUTES).build();
24+
.expireAfterAccess(5, TimeUnit.MINUTES).build();
2625

2726
public final static long _1HOUR_MS = 3600000;
2827

@@ -107,20 +106,20 @@ public static boolean isFull(String accountId, int maxConnections) {
107106
return false;
108107
}
109108

110-
public static boolean canReport(String accountId) {
111-
synchronized (SynchronizedInternerUtils.getInterner().intern(accountId)){
112-
AccountConnectionStat connectionCounter = ACCOUNT_CONNECTION_COUNT_STATS.getIfPresent(accountId);
113-
if (connectionCounter != null) {
114-
long interruptionTime = connectionCounter.getInterruptionTime();
115-
//操作一个小时后,可以继续执行上报
116-
//interruptionTime =0 ok
117-
// interruptionTime !=0 ok
118-
if ((System.currentTimeMillis() - interruptionTime) > _1HOUR_MS) {
119-
connectionCounter.setInterruptionTime(System.currentTimeMillis());
120-
return true;
109+
public static boolean canReport(String accountId) {
110+
synchronized (SynchronizedInternerUtils.getInterner().intern(accountId)) {
111+
AccountConnectionStat connectionCounter = ACCOUNT_CONNECTION_COUNT_STATS.getIfPresent(accountId);
112+
if (connectionCounter != null) {
113+
long interruptionTime = connectionCounter.getInterruptionTime();
114+
//操作一个小时后,可以继续执行上报
115+
//interruptionTime =0 ok
116+
// interruptionTime !=0 ok
117+
if ((System.currentTimeMillis() - interruptionTime) > _1HOUR_MS) {
118+
connectionCounter.setInterruptionTime(System.currentTimeMillis());
119+
return true;
120+
}
121121
}
122-
}
123-
return false;
122+
return false;
124123
}
125124
}
126125

@@ -136,7 +135,7 @@ public static void updateGlobalConnectionStat(String accountNo, int count, long
136135
connectionCounter.updateRemoteConnectionNum(count);
137136
}
138137
//全局控制
139-
if (interruptionTime>0) connectionCounter.setInterruptionTime(interruptionTime);
138+
if (interruptionTime > 0) connectionCounter.setInterruptionTime(interruptionTime);
140139
}
141140

142141
/**
@@ -152,9 +151,9 @@ public static void updateGlobalConnectionStat(String accountNo, int count, long
152151
*/
153152
public static void reportConnectionNum(String accountNo, String proxyIp) {
154153
AccountConnectionStat connectionCounter = ACCOUNT_CONNECTION_COUNT_STATS.getIfPresent(accountNo);
155-
int internalConnectionCount = connectionCounter.getByServer();
156154

157155
if (System.currentTimeMillis() - connectionCounter.getLastReportTime() > _30S) {
156+
int internalConnectionCount = connectionCounter.getByServer();
158157
GlobalConnectionStatTask globalConnectionStatTask =
159158
new GlobalConnectionStatTask(accountNo, proxyIp, internalConnectionCount);
160159
TaskService.addTask(globalConnectionStatTask);

‎proxy/src/main/java/com/jhl/proxy/Dispatcher.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class Dispatcher extends ChannelInboundHandlerAdapter {
3636

3737
private static final String HOST = "HOST";
3838
private static final Long MAX_INTERVAL_REPORT_TIME_MS = 1000 * 60 * 5L;
39-
39+
private static final Long _1MINUTES=60_000L;
4040
/**
4141
* proxy端配置数据
4242
*/
@@ -71,7 +71,13 @@ public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
7171
} else {
7272

7373
try {
74+
75+
if (proxyAccountService.interrupted(accountNo, host, version))
76+
throw new ReleaseDirectMemoryException("【当前版本已经更新】抛出异常。统一内存释放");
77+
7478
writeToOutBoundChannel(msg, ctx);
79+
80+
ConnectionStatsCache.reportConnectionNum(accountNo,proxyIp);
7581
segmentReportingFlowStat();
7682
} catch (Exception e) {
7783
if (!(e instanceof ReleaseDirectMemoryException)) {
@@ -379,9 +385,7 @@ private String getAccountId() {
379385

380386

381387
private void writeToOutBoundChannel(Object msg, final ChannelHandlerContext ctx) throws ReleaseDirectMemoryException {
382-
if (proxyAccountService.interrupted(accountNo, host, version)) {
383-
throw new ReleaseDirectMemoryException("【当前版本已经更新】抛出异常。统一内存释放");
384-
}
388+
385389
outboundChannel.writeAndFlush(msg).addListener((ChannelFutureListener) future -> {
386390
release((ByteBuf) msg);
387391
if (future.isSuccess()) {
@@ -392,6 +396,7 @@ private void writeToOutBoundChannel(Object msg, final ChannelHandlerContext ctx)
392396
});
393397
}
394398

399+
395400
private void reportConnectionLimit() {
396401

397402
if (ConnectionStatsCache.canReport(accountNo)) {
@@ -412,8 +417,8 @@ private void segmentReportingFlowStat() {
412417
if (ConnectionStatsCache.getByHost(accountNo,host) < 1) return;
413418
TrafficCounter trafficCounter = TrafficControllerCache.getGlobalTrafficShapingHandler(getAccountId()).trafficCounter();
414419
if (System.currentTimeMillis() - trafficCounter.lastCumulativeTime() >= MAX_INTERVAL_REPORT_TIME_MS) {
415-
synchronized (SynchronizedInternerUtils.getInterner().intern(accountNo + ":reportStat")) {
416420

421+
synchronized (SynchronizedInternerUtils.getInterner().intern(accountNo + ":reportStat")) {
417422
if (System.currentTimeMillis() - trafficCounter.lastCumulativeTime() >= MAX_INTERVAL_REPORT_TIME_MS) {
418423
long writtenBytes = trafficCounter.cumulativeWrittenBytes();
419424
long readBytes = trafficCounter.cumulativeReadBytes();

0 commit comments

Comments
 (0)
Please sign in to comment.