vishesh92 commented on code in PR #11221:
URL: https://github.com/apache/cloudstack/pull/11221#discussion_r2212477489


##########
services/console-proxy/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyNoVncClient.java:
##########
@@ -109,39 +112,54 @@ public void run() {
                     connectClientToVNCServer(tunnelUrl, tunnelSession, 
websocketUrl);
                     authenticateToVNCServer(clientSourceIp);
 
-                    int readBytes;
-                    byte[] b;
+                    // Track consecutive iterations with no data and sleep 
accordingly. Only used for NIO socket connections.
+                    int consecutiveZeroReads = 0;
+                    int sleepTime = 1;
                     while (connectionAlive) {
                         logger.trace("Connection with client [{}] [IP: {}] is 
alive.", clientId, clientSourceIp);
                         if (client.isVncOverWebSocketConnection()) {
                             if (client.isVncOverWebSocketConnectionOpen()) {
                                 updateFrontEndActivityTime();
                             }
                             connectionAlive = session.isOpen();
+                            sleepTime = 1;
                         } else if (client.isVncOverNioSocket()) {
-                            byte[] bytesArr;
-                            int nextBytes = client.getNextBytes();
-                            bytesArr = new byte[nextBytes];
-                            client.readBytes(bytesArr, nextBytes);
-                            logger.trace("Read [{}] bytes from client [{}].", 
nextBytes, clientId);
-                            if (nextBytes > 0) {
-                                
session.getRemote().sendBytes(ByteBuffer.wrap(bytesArr));
+                            ByteBuffer buffer = getOrCreateReadBuffer();
+                            int bytesRead = 
client.readAvailableDataIntoBuffer(buffer, buffer.remaining());
+
+                            if (bytesRead > 0) {
                                 updateFrontEndActivityTime();
+                                consecutiveZeroReads = 0; // Reset counter on 
successful read
+
+                                sleepTime = 0; // Still no sleep to catch any 
remaining data quickly
                             } else {
                                 connectionAlive = session.isOpen();
+                                consecutiveZeroReads++;
+                                // Use adaptive sleep time to prevent 
excessive busy waiting
+                                sleepTime = Math.min(consecutiveZeroReads, 
10); // Cap at 10ms max
+                            }
+
+                            final boolean bufferHasData = buffer.position() > 
0;
+                            if (bufferHasData && (bytesRead == 0 || 
buffer.remaining() <= flushThreshold)) {
+                                buffer.flip();
+                                logger.trace("Flushing buffer with [{}] bytes 
for client [{}]", buffer.remaining(), clientId);
+                                session.getRemote().sendBytes(buffer);
+                                buffer.compact();
                             }
                         } else {
-                            b = new byte[100];
-                            readBytes = client.read(b);
+                            byte[] b = new byte[100];
+                            int readBytes = client.read(b);
                             logger.trace("Read [{}] bytes from client [{}].", 
readBytes, clientId);
                             if (readBytes == -1 || (readBytes > 0 && 
!sendReadBytesToNoVNC(b, readBytes))) {
                                 connectionAlive = false;
                             }
                         }

Review Comment:
   ```suggestion
                               int readBytes = client.read(b);
                               logger.trace("Read [{}] bytes from client 
[{}].", readBytes, clientId);
                               if (readBytes == -1 || (readBytes > 0 && 
!sendReadBytesToNoVNC(b, readBytes))) {
                                   connectionAlive = false;
                               }
                               sleepTime = 1;
                           }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to