Skip to content

Commit 5e388ab

Browse files
committed
refactor(session):优化负载计算逻辑
- 移除 AtomicInteger 依赖 - 使用两个整型变量分别记录 offer 和 poll 操作次数 - 调整负载值计算方式为差值绝对值 - 注释掉调试打印语句
1 parent bd0e375 commit 5e388ab

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/main/java/tech/smartboot/redisun/RedisSession.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import java.util.concurrent.CompletableFuture;
66
import java.util.concurrent.ConcurrentLinkedQueue;
7-
import java.util.concurrent.atomic.AtomicInteger;
87

98
/**
109
* Redis会话管理类
@@ -35,7 +34,8 @@ final class RedisSession {
3534
*/
3635
private RESP decodingResponse;
3736

38-
private final AtomicInteger load = new AtomicInteger();
37+
private int offerCount = 0;
38+
private int pollCount = 0;
3939

4040
/**
4141
* 感知变化量,无需保障原子性
@@ -69,17 +69,19 @@ void setDecodingResponse(RESP decodingResponse) {
6969
}
7070

7171
public CompletableFuture<RESP> poll() {
72-
load.decrementAndGet();
72+
pollCount++;
7373
return pipeline.poll();
7474
}
7575

7676
public void offer(CompletableFuture<RESP> future) {
77-
load.incrementAndGet();
77+
offerCount++;
7878
pipeline.offer(future);
7979
}
8080

8181
int load() {
82-
return load.get();
82+
int size = offerCount - pollCount;
83+
// System.out.println("load: " + size);
84+
return size >= 0 ? size : -size;
8385
}
8486

8587
}

0 commit comments

Comments
 (0)