11package tech .smartboot .redisun ;
22
3- import org .smartboot .socket .transport .WriteBuffer ;
43import tech .smartboot .redisun .resp .RESP ;
54
6- import java .io .IOException ;
75import java .util .concurrent .CompletableFuture ;
86import java .util .concurrent .ConcurrentLinkedQueue ;
9- import java .util .concurrent .Semaphore ;
107
118/**
129 * Redis会话管理类
@@ -39,14 +36,14 @@ final class RedisSession {
3936
4037 private int offerCount = 0 ;
4138 private int pollCount = 0 ;
42- private final WriteBuffer writeBuffer ;
4339
44- public RedisSession ( WriteBuffer writeBuffer ) {
45- this . writeBuffer = writeBuffer ;
40+ public int incrOfferCount ( ) {
41+ return ++ offerCount ;
4642 }
4743
48- private final ConcurrentLinkedQueue <Tuple > commandQueue = new ConcurrentLinkedQueue <>();
49- private final Semaphore semaphore = new Semaphore (1 );
44+ public int getOfferCount () {
45+ return offerCount ;
46+ }
5047
5148 /**
5249 * 获取正在解码的响应对象
@@ -71,41 +68,12 @@ public CompletableFuture<RESP> poll() {
7168 return pipeline .poll ();
7269 }
7370
74- public void writeCommand (CompletableFuture <RESP > future , Command command ) throws IOException {
75- offerCount ++;
76- if (semaphore .tryAcquire ()) {
77- pipeline .offer (future );
78- command .writeTo (writeBuffer );
79- Tuple tuple ;
80- while ((tuple = commandQueue .poll ()) != null ) {
81- pipeline .offer (tuple .future );
82- tuple .command .writeTo (writeBuffer );
83- }
84- writeBuffer .flush ();
85- semaphore .release ();
86- flush ();
87- } else {
88- commandQueue .offer (new Tuple (future , command ));
89- }
71+ public void offer (CompletableFuture <RESP > future ) {
72+ pipeline .offer (future );
9073 }
9174
92- public void flush () {
93- if (commandQueue .isEmpty () || !semaphore .tryAcquire ()) {
94- return ;
95- }
96-
97- try {
98- Tuple tuple ;
99- while ((tuple = commandQueue .poll ()) != null ) {
100- pipeline .offer (tuple .future );
101- tuple .command .writeTo (writeBuffer );
102- }
103- } catch (Throwable e ) {
104- throw new RedisunException (e );
105- } finally {
106- semaphore .release ();
107- }
108-
75+ public int getPollCount () {
76+ return pollCount ;
10977 }
11078
11179 int load () {
@@ -114,13 +82,4 @@ int load() {
11482 return size >= 0 ? size : -size ;
11583 }
11684
117- private static class Tuple {
118- private final CompletableFuture <RESP > future ;
119- private final Command command ;
120-
121- public Tuple (CompletableFuture <RESP > future , Command command ) {
122- this .future = future ;
123- this .command = command ;
124- }
125- }
12685}
0 commit comments