Permalink
Browse files

Stop selecting for writable when there is no data

Also, explicitly check and throw exceptions for disconnections.

Change-Id: I4a3014d313f8f36ac35c1f5940ddd4418ff6b9bf
Reviewed-on: http://review.couchbase.org/13179
Reviewed-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Trond Norbye <trond.norbye@gmail.com>
  • Loading branch information...
1 parent 5dbe5cd commit ee53d20011b7a441ba6ab796f22d304a1e84cc1f @mnunberg mnunberg committed with trondn Feb 13, 2012
Showing with 18 additions and 10 deletions.
  1. +18 −10 src/main/java/org/couchbase/mock/memcached/MemcachedServer.java
@@ -224,29 +224,37 @@ public void run() {
if (client != null) {
try {
- if (key.isReadable()) {
- SocketChannel channel = (SocketChannel) key.channel();
+ int ioevents = SelectionKey.OP_READ;
+ SocketChannel channel = (SocketChannel) key.channel();
+
+ if (key.isReadable()) {
if (channel.read(client.getInputBuffer()) == -1) {
channel.close();
+ throw new ClosedChannelException();
} else {
client.step();
- if (client.hasOutput()) {
- channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, client);
- } else {
- channel.register(selector, SelectionKey.OP_READ, client);
- }
}
- } else if (key.isWritable()) {
- SocketChannel channel = (SocketChannel) key.channel();
+ }
+ if (key.isWritable()) {
ByteBuffer buf;
while ((buf = client.getOutputBuffer()) != null) {
- channel.write(buf);
+ if (channel.write(buf) == -1) {
+ channel.close();
+ throw new ClosedChannelException();
+ }
}
}
+
+ if (client.hasOutput()) {
+ ioevents |= SelectionKey.OP_WRITE;
+ }
+
+ channel.register(selector, ioevents, client);
} catch (ClosedChannelException exp) {
// just ditch this client..
}
+
} else {
if (key.isAcceptable()) {
SocketChannel cc = server.accept();

0 comments on commit ee53d20

Please sign in to comment.