private void recycleSocket(final AsyncSocket socket, AsyncHttpRequest request) { if (socket == null) return; URI uri = request.getUri(); int port = getSchemePort(uri); String lookup = computeLookup(uri, port, request); // nothing here will block... synchronized (this) { HashSet<AsyncSocket> sockets = mSockets.get(lookup); if (sockets == null) { sockets = new HashSet<AsyncSocket>(); mSockets.put(lookup, sockets); } final HashSet<AsyncSocket> ss = sockets; sockets.add(socket); socket.setClosedCallback( new CompletedCallback() { @Override public void onCompleted(Exception ex) { synchronized (AsyncSocketMiddleware.this) { ss.remove(socket); } socket.setClosedCallback(null); } }); } }
String computeLookup(URI uri, int port, AsyncHttpRequest request) { String proxy; if (proxyHost != null) proxy = proxyHost + ":" + proxyPort; else proxy = ""; if (request.proxyHost != null) proxy = request.getProxyHost() + ":" + request.proxyPort; return uri.getScheme() + "//" + uri.getHost() + ":" + port + "?proxy=" + proxy; }