Skip to content

Commit

Permalink
make explicit the accidental non-pooling behaviour of NGSessionPool
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoroka authored and denji committed Jul 21, 2015
1 parent d37065f commit c6e5364
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@
*/
public class NGServer implements Runnable {

/**
* Default size for thread pool
*/
public static final int DEFAULT_SESSIONPOOLSIZE = 10;

/**
* The address on which to listen, or null to listen on all local addresses
*/
Expand Down Expand Up @@ -129,11 +124,10 @@ public class NGServer implements Runnable {
* @param addr the address at which to listen, or
* <code>null</code> to bind to all local addresses
* @param port the port on which to listen.
* @param sessionPoolSize the max number of idle sessions allowed by the
* pool
*/
public NGServer(InetAddress addr, int port, int sessionPoolSize, int timeoutMillis) {
init(addr, port, sessionPoolSize, timeoutMillis);
public NGServer(InetAddress addr, int port, int timeoutMillis) {
init(addr, port, timeoutMillis);
}

/**
Expand All @@ -148,7 +142,7 @@ public NGServer(InetAddress addr, int port, int sessionPoolSize, int timeoutMill
* @param port the port on which to listen.
*/
public NGServer(InetAddress addr, int port) {
init(addr, port, DEFAULT_SESSIONPOOLSIZE, NGConstants.HEARTBEAT_TIMEOUT_MILLIS);
init(addr, port, NGConstants.HEARTBEAT_TIMEOUT_MILLIS);
}

/**
Expand All @@ -159,26 +153,25 @@ public NGServer(InetAddress addr, int port) {
* <code>NGServer</code> and start it.
*/
public NGServer() {
init(null, NGConstants.DEFAULT_PORT, DEFAULT_SESSIONPOOLSIZE, NGConstants.HEARTBEAT_TIMEOUT_MILLIS);
init(null, NGConstants.DEFAULT_PORT, NGConstants.HEARTBEAT_TIMEOUT_MILLIS);
}

/**
* Sets up the NGServer internals
*
* @param addr the InetAddress to bind to
* @param port the port on which to listen
* @param sessionPoolSize the max number of idle sessions allowed by the
* pool
*/
private void init(InetAddress addr, int port, int sessionPoolSize, int timeoutMillis) {
private void init(InetAddress addr, int port, int timeoutMillis) {
this.addr = addr;
this.port = port;

this.aliasManager = new AliasManager();
allNailStats = new java.util.HashMap();
// allow a maximum of 10 idle threads. probably too high a number
// and definitely should be configurable in the future
sessionPool = new NGSessionPool(this, sessionPoolSize);
sessionPool = new NGSessionPool(this);
this.heartbeatTimeoutMillis = timeoutMillis;
}

Expand Down Expand Up @@ -325,8 +318,6 @@ public void shutdown(boolean exitVM) {
} catch (Throwable toDiscard) {
}

sessionPool.shutdown();

Class[] argTypes = new Class[1];
argTypes[0] = NGServer.class;
Object[] argValues = new Object[1];
Expand Down Expand Up @@ -495,7 +486,7 @@ public static void main(String[] args) throws NumberFormatException, UnknownHost
}
}

NGServer server = new NGServer(serverAddress, port, DEFAULT_SESSIONPOOLSIZE, timeoutMillis);
NGServer server = new NGServer(serverAddress, port, timeoutMillis);
Thread t = new Thread(server);
t.setName("NGServer(" + serverAddress + ", " + port + ")");
t.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,17 @@
package com.martiansoftware.nailgun;

/**
* Provides NGSession pooling functionality. One parameter, "maxIdle",
* governs its behavior by setting the maximum number of idle NGSession
* threads it will allow. It creates a pool of size maxIdle - 1, because
* one NGSession is kept "on deck" by the NGServer in order to eke out
* a little extra responsiveness.
* Provides NGSession creation functionality.
*
* @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
*/
class NGSessionPool {

/**
* number of sessions to store in the pool
*/
int poolSize = 0;

/**
* the pool itself
*/
NGSession[] pool = null;

/**
* The number of sessions currently in the pool
*/
int poolEntries = 0;

/**
* reference to server we're working for
*/
NGServer server = null;

/**
* have we been shut down?
*/
boolean done = false;

/**
* synchronization object
*/
Expand All @@ -63,14 +39,9 @@ class NGSessionPool {
* Creates a new NGSessionRunner operating for the specified server, with
* the specified number of threads
* @param server the server to work for
* @param poolsize the maximum number of idle threads to allow
*/
NGSessionPool(NGServer server, int poolsize) {
NGSessionPool(NGServer server) {
this.server = server;
this.poolSize = Math.min(0, poolsize);

pool = new NGSession[poolSize];
poolEntries = 0;
}

/**
Expand All @@ -80,13 +51,8 @@ class NGSessionPool {
NGSession take() {
NGSession result;
synchronized(lock) {
if (poolEntries == 0) {
result = new NGSession(this, server);
result.start();
} else {
--poolEntries;
result = pool[poolEntries];
}
result = new NGSession(this, server);
result.start();
}
return (result);
}
Expand All @@ -97,28 +63,7 @@ NGSession take() {
* @param session the NGSession to return to the pool
*/
void give(NGSession session) {
boolean shutdown = false;
synchronized(lock) {
if (done || poolEntries == poolSize) {
shutdown = true;
} else {
pool[poolEntries] = session;
++poolEntries;
}
}
if (shutdown) session.shutdown();
}

/**
* Shuts down the pool. Running nails are allowed to finish.
*/
void shutdown() {
done = true;
synchronized(lock) {
while (poolEntries > 0) {
take().shutdown();
}
}
session.shutdown();
}

}

0 comments on commit c6e5364

Please sign in to comment.