Skip to content

Commit

Permalink
Work on threading model, plus more threading checks and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
purplefox committed Jun 20, 2014
1 parent 17d05d0 commit 8bf0bfa
Show file tree
Hide file tree
Showing 31 changed files with 355 additions and 382 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ ScratchTest.java
test-results
test-tmp
*.class
ScratchPad.java
3 changes: 3 additions & 0 deletions vertx-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
<io.netty.leakDetectionLevel>PARANOID</io.netty.leakDetectionLevel>
</systemPropertyVariables>
<argLine>-server -Xms128m -Xmx1024m -XX:NewRatio=2</argLine>
<excludes>
<exclude>**/ScratchPad.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.vertx.java.core.datagram.impl;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import org.vertx.java.core.AsyncResult;
Expand All @@ -42,17 +41,7 @@ final class DatagramChannelFutureListener<T> implements ChannelFutureListener {

@Override
public void operationComplete(final ChannelFuture future) throws Exception {
Channel ch = future.channel();
if (context.isOnCorrectWorker(ch.eventLoop())) {
try {
vertx.setContext(context);
notifyHandler(future);
} catch (Throwable t) {
context.reportException(t);
}
} else {
context.execute(() -> notifyHandler(future));
}
context.execute(() -> notifyHandler(future), true);
}

private void notifyHandler(ChannelFuture future) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,7 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
@SuppressWarnings("unchecked")
@Override
protected void channelRead(final DatagramSocketImpl server, final ContextImpl context, ChannelHandlerContext chctx, final Object msg) throws Exception {
if (context.isOnCorrectWorker(chctx.channel().eventLoop())) {
try {
vertx.setContext(context);
server.handleMessage((org.vertx.java.core.datagram.DatagramPacket) msg);
} catch (Throwable t) {
context.reportException(t);
}
} else {
context.execute(() -> {
try {
server.handleMessage((org.vertx.java.core.datagram.DatagramPacket) msg);
} catch (Throwable t) {
context.reportException(t);
}
});
}
context.execute(() -> server.handleMessage((org.vertx.java.core.datagram.DatagramPacket) msg), true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,6 @@ private static NioDatagramChannel createChannel(org.vertx.java.core.datagram.Int


private void notifyException(final Handler<AsyncResult<DatagramSocket>> handler, final Throwable cause) {
if (context.isOnCorrectWorker(channel().eventLoop())) {
try {
vertx.setContext(context);
handler.handle(new FutureResultImpl<>(cause));
} catch (Throwable t) {
context.reportException(t);
}
} else {
context.execute(() -> handler.handle(new FutureResultImpl<>(cause)));
}
context.execute(() -> handler.handle(new FutureResultImpl<>(cause)), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public DnsClient reverseLookup(String address, final Handler<AsyncResult<String>
} catch (final UnknownHostException e) {
// Should never happen as we work with ip addresses as input
// anyway just in case notify the handler
actualCtx.execute(() -> handler.handle(new FutureResultImpl<>(e)));
actualCtx.execute(() -> handler.handle(new FutureResultImpl<>(e)), false);
}
return this;
}
Expand Down Expand Up @@ -275,26 +275,13 @@ private void setResult(final FutureResultImpl r, EventLoop loop, final Object re
if (r.complete()) {
return;
}
if (actualCtx.isOnCorrectWorker(loop)) {
try {
vertx.setContext(actualCtx);
if (result instanceof Throwable) {
r.setFailure((Throwable) result);
} else {
r.setResult(result);
}
} catch (Throwable t) {
actualCtx.reportException(t);
actualCtx.execute(() -> {
if (result instanceof Throwable) {
r.setFailure((Throwable) result);
} else {
r.setResult(result);
}
} else {
actualCtx.execute(() -> {
if (result instanceof Throwable) {
r.setFailure((Throwable) result);
} else {
r.setResult(result);
}
});
}
}, true);
}

private static final class HandlerAdapter<T> implements Handler<AsyncResult<List<T>>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ private <T> void doReceive(BaseMessage<T> msg, HandlerHolder<T> holder) {
unregisterHandler(msg.address, holder.handler);
}
}
});
}, false);
}

private void checkStarted() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,14 @@ public void completed(Integer bytesWritten, Object attachment) {
context.execute(() -> {
writesOutstanding -= buff.limit();
handler.handle(new FutureResultImpl<Void>().setResult(null));
});
}, false);
}
}

public void failed(Throwable exc, Object attachment) {
if (exc instanceof Exception) {
Exception e = (Exception) exc;
context.execute(() -> handler.handle(new FutureResultImpl<Void>().setResult(null)));
context.execute(() -> handler.handle(new FutureResultImpl<Void>().setResult(null)), false);
} else {
log.error("Error occurred", exc);
}
Expand All @@ -369,7 +369,7 @@ private void done() {
buff.flip();
writeBuff.setBytes(offset, buff);
result.setResult(writeBuff).setHandler(handler);
});
}, false);
}

public void completed(Integer bytesRead, Object attachment) {
Expand All @@ -388,11 +388,7 @@ public void completed(Integer bytesRead, Object attachment) {
}

public void failed(Throwable t, Object attachment) {
context.execute(new Runnable() {
public void run() {
result.setFailure(t).setHandler(handler);
}
});
context.execute(() -> result.setFailure(t).setHandler(handler), false);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import javax.net.ssl.SSLPeerUnverifiedException;
import javax.security.cert.X509Certificate;
import java.net.URI;

/**
* Represents a server-side HTTP request.<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,18 @@ public HandshakeInboundHandler(final Handler<WebSocket> wsConnect) {
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
context.execute(ctx.channel().eventLoop(), () -> {
context.execute(() -> {
// if still handshaking this means we not got any response back from the server and so need to notify the client
// about it as otherwise the client would never been notified.
if (handshaking) {
handleException(new WebSocketHandshakeException("Connection closed while handshake in process"));
}
});
}, true);
}

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
context.execute(ctx.channel().eventLoop(), () -> {
context.execute(() -> {
if (handshaker != null && handshaking) {
if (msg instanceof HttpResponse) {
HttpResponse resp = (HttpResponse) msg;
Expand Down Expand Up @@ -197,7 +197,7 @@ public void channelRead(final ChannelHandlerContext ctx, final Object msg) throw
} else {
buffered.add(msg);
}
});
}, true);
}

private void handleException(WebSocketHandshakeException e) {
Expand Down
Loading

0 comments on commit 8bf0bfa

Please sign in to comment.