The PipedInputStream/OutputStream combo has a serious performance problem in it. Namely, the writer doesn't wake up the reader when it writes new data, and instead it relies on the wait(1000) call on the reader side to notice new data.

This means it can have up to 1 sec latency for new data to be picked up. We should replace all the use of PipedIn/OutStream by something else that works.

In this instance, I tweaked the code so that it doesn't go through a pipe. This assumes that the servlet container allows a different thread to drain InputStream.



git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@18883 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2009-06-13 21:22:12 +00:00
1 parent 3e5d76c30a
commit d0d54a69a6
2 files changed
+67 -9

No files matched your search

@@ -28,14 +28,11 @@ import hudson.remoting.PingThread;
import hudson.remoting.Channel.Mode;
import hudson.util.ChunkedOutputStream;
import hudson.util.ChunkedInputStream;
import org.apache.commons.io.IOUtils;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
@@ -49,11 +46,13 @@ import java.util.logging.Logger;
abstract class FullDuplexHttpChannel {
private Channel channel;
private final PipedOutputStream pipe = new PipedOutputStream();
private InputStream upload;
private final UUID uuid;
private final boolean restricted;
private boolean completed;
public FullDuplexHttpChannel(UUID uuid, boolean restricted) throws IOException {
this.uuid = uuid;
this.restricted = restricted;
@@ -65,7 +64,7 @@ abstract class FullDuplexHttpChannel {
* <p>
* If this connection is lost, we'll abort the channel.
*/
public void download(StaplerRequest req, StaplerResponse rsp) throws InterruptedException, IOException {
public synchronized void download(StaplerRequest req, StaplerResponse rsp) throws InterruptedException, IOException {
rsp.setStatus(HttpServletResponse.SC_OK);
// server->client channel.
@@ -74,8 +73,16 @@ abstract class FullDuplexHttpChannel {
OutputStream out = rsp.getOutputStream();
if (DIY_CHUNKING) out = new ChunkedOutputStream(out);
// send something out so that the client will see the HTTP headers
out.write("Starting HTTP duplex channel".getBytes());
out.flush();
// wait until we have the other channel
while(upload==null)
wait();
channel = new Channel("HTTP full-duplex channel " + uuid,
Computer.threadPoolForRemoting, Mode.BINARY, new PipedInputStream(pipe), out, null, restricted);
Computer.threadPoolForRemoting, Mode.BINARY, upload, out, null, restricted);
// so that we can detect dead clients, periodically send something
PingThread ping = new PingThread(channel) {
@@ -84,7 +91,7 @@ abstract class FullDuplexHttpChannel {
LOGGER.info("Duplex-HTTP session " + uuid + " is terminated");
// this will cause the channel to abort and subsequently clean up
try {
pipe.close();
upload.close();
} catch (IOException e) {
// this can never happen
throw new AssertionError(e);
@@ -95,6 +102,10 @@ abstract class FullDuplexHttpChannel {
main(channel);
channel.join();
ping.interrupt();
// publish that we are done
completed=true;
notify();
}
protected abstract void main(Channel channel) throws IOException, InterruptedException;
@@ -102,11 +113,18 @@ abstract class FullDuplexHttpChannel {
/**
* This is where we receive inputs from the client.
*/
public void upload(StaplerRequest req, StaplerResponse rsp) throws InterruptedException, IOException {
public synchronized void upload(StaplerRequest req, StaplerResponse rsp) throws InterruptedException, IOException {
rsp.setStatus(HttpServletResponse.SC_OK);
InputStream in = req.getInputStream();
if(DIY_CHUNKING) in = new ChunkedInputStream(in);
IOUtils.copy(in,pipe);
// publish the upload channel
upload = in;
notify();
// wait until we are done
while (!completed)
wait();
}
public Channel getChannel() {