/**
* counter for nr of connections handled by an endpoint
*/
- protected volatile CounterLatch connectionCounterLatch = null;
+ private volatile CounterLatch connectionCounterLatch = null;
/**
* Socket properties
protected abstract Log getLog();
public abstract boolean getUseSendfile();
+
+ protected CounterLatch initializeConnectionLatch() {
+ if (connectionCounterLatch==null) {
+ connectionCounterLatch = new CounterLatch(0,getMaxConnections());
+ }
+ return connectionCounterLatch;
+ }
+
+ protected void releaseConnectionLatch() {
+ CounterLatch latch = connectionCounterLatch;
+ if (latch!=null) latch.releaseAll();
+ connectionCounterLatch = null;
+ }
+
+ protected void awaitConnection() throws InterruptedException {
+ CounterLatch latch = connectionCounterLatch;
+ if (latch!=null) latch.await();
+ }
+
+ protected long countUpConnection() {
+ CounterLatch latch = connectionCounterLatch;
+ if (latch!=null) return latch.countUp();
+ else return -1;
+ }
+
+ protected long countDownConnection() {
+ CounterLatch latch = connectionCounterLatch;
+ if (latch!=null) {
+ long result = latch.countDown();
+ if (result<0) {
+ getLog().warn("Incorrect connection count, multiple socket.close called on the same socket." );
+ }
+ return result;
+ } else return -1;
+ }
+
+
// -------------------- SSL related properties --------------------
import org.apache.tomcat.jni.Socket;
import org.apache.tomcat.jni.Status;
import org.apache.tomcat.util.ExceptionUtils;
+import org.apache.tomcat.util.threads.CounterLatch;
/**
if (getExecutor() == null) {
createExecutor();
}
+
+ initializeConnectionLatch();
// Start poller threads
pollers = new Poller[pollerThreadCount];
*/
@Override
public void stopInternal() {
+ releaseConnectionLatch();
if (!paused) {
pause();
}
// parent pool or acceptor socket.
// In any case disable double free which would cause JVM core.
Socket.destroy(socket);
+ countDownConnection();
}
}
break;
}
try {
+ //if we have reached max connections, wait
+ awaitConnection();
// Accept the next incoming connection from the server socket
long socket = Socket.accept(serverSock);
+ //increment socket count
+ countUpConnection();
/*
* In the case of a deferred accept unlockAccept needs to
* send data. This data will be rubbish, so destroy the
}
try {
//if we have reached max connections, wait
- connectionCounterLatch.await();
+ awaitConnection();
// Accept the next incoming connection from the server socket
Socket socket = serverSocketFactory.acceptSocket(serverSocket);
// Ignore
}
} else {
- connectionCounterLatch.countUp();
+ countUpConnection();
}
} else {
// Close socket right away
if (log.isTraceEnabled()) {
log.trace("Closing socket:"+socket);
}
- connectionCounterLatch.countDown();
+ countDownConnection();
try {
socket.getSocket().close();
} catch (IOException e) {
createExecutor();
}
- connectionCounterLatch = new CounterLatch(0,getMaxConnections());
+ initializeConnectionLatch();
// Start acceptor threads
for (int i = 0; i < acceptorThreadCount; i++) {
@Override
public void stopInternal() {
- connectionCounterLatch.releaseAll();
- connectionCounterLatch = null;
+ releaseConnectionLatch();
if (!paused) {
pause();
}
if (!running) {
running = true;
paused = false;
-
- connectionCounterLatch = new CounterLatch(0, getMaxConnections());
+
// Create worker collection
if ( getExecutor() == null ) {
createExecutor();
}
+ initializeConnectionLatch();
+
// Start poller threads
pollers = new Poller[getPollerThreadCount()];
for (int i=0; i<pollers.length; i++) {
*/
@Override
public void stopInternal() {
- connectionCounterLatch.releaseAll();
- connectionCounterLatch = null;
+ releaseConnectionLatch();
if (!paused) {
pause();
}
}
try {
//if we have reached max connections, wait
- connectionCounterLatch.await();
+ awaitConnection();
// Accept the next incoming connection from the server socket
SocketChannel socket = serverSock.accept();
// Hand this socket off to an appropriate processor
log.debug("", ix);
}
} else {
- connectionCounterLatch.countUp();
+ countUpConnection();
}
}
} catch (SocketTimeoutException sx) {
try {if (ka!=null && ka.getSendfileData()!=null && ka.getSendfileData().fchannel!=null && ka.getSendfileData().fchannel.isOpen()) ka.getSendfileData().fchannel.close();}catch (Exception ignore){}
if (ka!=null) {
ka.reset();
- if (connectionCounterLatch.countDown()<0) {
- log.warn("Incorrect connection count, multiple cancel called on the same key?" );
- }
+ countDownConnection();
}
} catch (Throwable e) {
ExceptionUtils.handleThrowable(e);