tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1310701 - /tomcat/trunk/java/org/apache/catalina/websocket/WebSocketServlet.java
Date Sat, 07 Apr 2012 08:45:08 GMT
Author: markt
Date: Sat Apr  7 08:45:08 2012
New Revision: 1310701

URL: http://svn.apache.org/viewvc?rev=1310701&view=rev
Log:
Fix potential bottleneck on accepting new WebSocket connections

Modified:
    tomcat/trunk/java/org/apache/catalina/websocket/WebSocketServlet.java

Modified: tomcat/trunk/java/org/apache/catalina/websocket/WebSocketServlet.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/websocket/WebSocketServlet.java?rev=1310701&r1=1310700&r2=1310701&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/websocket/WebSocketServlet.java (original)
+++ tomcat/trunk/java/org/apache/catalina/websocket/WebSocketServlet.java Sat Apr  7 08:45:08
2012
@@ -23,6 +23,8 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
@@ -45,19 +47,8 @@ public abstract class WebSocketServlet e
             "258EAFA5-E914-47DA-95CA-C5AB0DC85B11".getBytes(
                     B2CConverter.ISO_8859_1);
 
-    private MessageDigest sha1Helper;
-
-
-    @Override
-    public void init() throws ServletException {
-        super.init();
-
-        try {
-            sha1Helper = MessageDigest.getInstance("SHA1");
-        } catch (NoSuchAlgorithmException e) {
-            throw new ServletException(e);
-        }
-    }
+    private Queue<MessageDigest> sha1Helpers =
+            new ConcurrentLinkedQueue<MessageDigest>();
 
 
     @Override
@@ -167,12 +158,24 @@ public abstract class WebSocketServlet e
     }
 
 
-    private String getWebSocketAccept(String key) {
-        synchronized (sha1Helper) {
-            sha1Helper.reset();
-            sha1Helper.update(key.getBytes(B2CConverter.ISO_8859_1));
-            return Base64.encode(sha1Helper.digest(WS_ACCEPT));
+    private String getWebSocketAccept(String key) throws ServletException {
+
+        MessageDigest sha1Helper = sha1Helpers.poll();
+        if (sha1Helper == null) {
+            try {
+                sha1Helper = MessageDigest.getInstance("SHA1");
+            } catch (NoSuchAlgorithmException e) {
+                throw new ServletException(e);
+            }
         }
+
+        sha1Helper.reset();
+        sha1Helper.update(key.getBytes(B2CConverter.ISO_8859_1));
+        String result = Base64.encode(sha1Helper.digest(WS_ACCEPT));
+
+        sha1Helpers.add(sha1Helper);
+
+        return result;
     }
 
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message