Skip to content

Commit 95a615e

Browse files
committed
improved servers performance
1 parent 58c26b1 commit 95a615e

2 files changed

Lines changed: 25 additions & 40 deletions

File tree

‎src/org/java_websocket/WebSocket.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel socket
160160
* When socket related I/O errors occur.
161161
* @throws InterruptedException
162162
*/
163-
/*package public*/void handleRead() throws IOException {
163+
/*package public*/synchronized void handleRead() throws IOException {
164164
if( !socketBuffer.hasRemaining() ) {
165165
socketBuffer.rewind();
166166
socketBuffer.limit( socketBuffer.capacity() );

‎src/org/java_websocket/WebSocketServer.java‎

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java.util.Iterator;
1515
import java.util.List;
1616
import java.util.Set;
17-
import java.util.concurrent.Callable;
17+
import java.util.concurrent.ArrayBlockingQueue;
1818
import java.util.concurrent.ExecutorService;
1919
import java.util.concurrent.Executors;
2020
import java.util.concurrent.ThreadFactory;
@@ -32,7 +32,7 @@
3232
*/
3333
public abstract class WebSocketServer extends WebSocketAdapter implements Runnable {
3434

35-
public static int DECODERS = 1;
35+
public static int DECODERS = Runtime.getRuntime().availableProcessors();
3636

3737
/**
3838
* Holds the list of active WebSocket connections. "Active" means WebSocket
@@ -61,8 +61,7 @@ public abstract class WebSocketServer extends WebSocketAdapter implements Runnab
6161

6262
private ExecutorService decoders;
6363

64-
private Set<WebSocket> active_websocktes = new HashSet<WebSocket>();
65-
// private Set<WebSocket> write_demands = new HashSet<WebSocket>();
64+
private ArrayBlockingQueue<WebSocket> queue;
6665

6766
// CONSTRUCTORS ////////////////////////////////////////////////////////////
6867
/**
@@ -107,7 +106,11 @@ public WebSocketServer( InetSocketAddress address , int decodercount , List<Draf
107106
else
108107
this.drafts = drafts;
109108
setAddress( address );
109+
queue = new ArrayBlockingQueue<WebSocket>( decodercount );
110110
this.decoders = Executors.newFixedThreadPool( decodercount, new WebsocketThreadFactory() );
111+
for( int i = 0 ; i < decodercount ; i++ ) {
112+
decoders.submit( new WebsocketWorker() );
113+
}
111114
}
112115

113116
/**
@@ -191,7 +194,6 @@ public void run() {
191194
server = ServerSocketChannel.open();
192195
server.configureBlocking( false );
193196
server.socket().bind( address );
194-
// InetAddress.getLocalHost()
195197
selector = Selector.open();
196198
server.register( selector, server.validOps() );
197199
} catch ( IOException ex ) {
@@ -223,9 +225,8 @@ public void run() {
223225
i.remove();
224226
} else if( key.isReadable() ) {
225227
conn = (WebSocket) key.attachment();
226-
if( asyncQueueRead( conn ) ) {
227-
i.remove();
228-
}
228+
queue.put( conn );
229+
i.remove();
229230
}
230231
}
231232
} catch ( CancelledKeyException e ) {
@@ -234,6 +235,8 @@ public void run() {
234235
if( key != null )
235236
key.cancel();
236237
handleIOException( conn, ex );
238+
} catch ( InterruptedException e ) {
239+
return;
237240
}
238241
}
239242
} catch ( RuntimeException e ) {
@@ -338,43 +341,25 @@ public final void onWriteDemand( WebSocket conn ) {
338341
public void onMessage( WebSocket conn, ByteBuffer message ) {
339342
};
340343

341-
private boolean asyncQueueRead( WebSocket ws ) {
342-
synchronized ( active_websocktes ) {
343-
if( active_websocktes.contains( ws ) ) {
344-
System.out.println( "queue failed" );
345-
return false;
346-
}
347-
active_websocktes.add( ws );// will add ws only if it is not already added
348-
decoders.submit( new WebsocketReadTask( ws ) );
349-
return true;
350-
}
351-
}
352-
353-
private class WebsocketReadTask implements Callable<Boolean> {
354-
355-
private WebSocket ws;
356-
357-
private WebsocketReadTask( WebSocket ws ) {
358-
this.ws = ws;
359-
}
344+
private class WebsocketWorker implements Runnable {
360345

361346
@Override
362-
public Boolean call() throws Exception {
347+
public void run() {
348+
WebSocket ws = null;
363349
try {
364-
ws.handleRead();
365-
ws.flush();
366-
System.out.println( "Assync Complete" );
367-
} catch ( IOException e ) {
368-
handleIOException( ws, e );
350+
while ( true ) {
351+
try {
352+
ws = queue.take();
353+
ws.handleRead();
354+
ws.flush();
355+
} catch ( IOException e ) {
356+
handleIOException( ws, e );
357+
}
358+
}
369359
} catch ( RuntimeException e ) {
370360
handleFatal( ws, e );
371-
} finally {
372-
synchronized ( active_websocktes ) {
373-
active_websocktes.remove( ws );
374-
}
375-
selector.wakeup();
361+
} catch ( InterruptedException e ) {
376362
}
377-
return true;
378363
}
379364
}
380365

0 commit comments

Comments
 (0)