change connection layer to use millisecond timeouts

This commit is contained in:
shikhar
2013-03-24 17:27:36 -04:00
parent c781724028
commit 6656214803
7 changed files with 27 additions and 23 deletions

View File

@@ -35,13 +35,10 @@ public abstract class AbstractService
protected final String name; protected final String name;
/** Transport layer */ /** Transport layer */
protected final Transport trans; protected final Transport trans;
/** Timeout for blocking operations */
protected int timeout;
public AbstractService(String name, Transport trans) { public AbstractService(String name, Transport trans) {
this.name = name; this.name = name;
this.trans = trans; this.trans = trans;
timeout = trans.getTimeout();
} }
@Override @Override
@@ -77,12 +74,6 @@ public abstract class AbstractService
trans.reqService(this); trans.reqService(this);
} }
public int getTimeout() {
return this.timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
} }

View File

@@ -138,16 +138,16 @@ public interface Connection {
Transport getTransport(); Transport getTransport();
/** /**
* @return the {@code timeout} in seconds that this connection uses for blocking operations and recommends to any * @return the {@code timeout} in milliseconds that this connection uses for blocking operations and recommends to
* {@link Channel other} {@link ForwardedChannelOpener classes} that ask for it. * any {@link Channel other} {@link ForwardedChannelOpener classes} that ask for it.
*/ */
int getTimeout(); int getTimeoutMs();
/** /**
* Set the {@code timeout} this connection uses for blocking operations and recommends to any {@link Channel other} * Set the {@code timeout} this connection uses for blocking operations and recommends to any {@link Channel other}
* {@link ForwardedChannelOpener classes} that ask for it. * {@link ForwardedChannelOpener classes} that ask for it.
* *
* @param timeout timeout in seconds * @param timeout timeout in milliseconds
*/ */
void setTimeout(int timeout); void setTimeoutMs(int timeout);
} }

View File

@@ -54,6 +54,8 @@ public class ConnectionImpl
private long windowSize = 2048 * 1024; private long windowSize = 2048 * 1024;
private int maxPacketSize = 32 * 1024; private int maxPacketSize = 32 * 1024;
private volatile int timeoutMs;
/** /**
* Create with an associated {@link Transport}. * Create with an associated {@link Transport}.
* *
@@ -61,6 +63,7 @@ public class ConnectionImpl
*/ */
public ConnectionImpl(Transport trans) { public ConnectionImpl(Transport trans) {
super("ssh-connection", trans); super("ssh-connection", trans);
timeoutMs = trans.getTimeout() * 1000;
} }
@Override @Override
@@ -251,4 +254,14 @@ public class ConnectionImpl
channels.clear(); channels.clear();
} }
@Override
public void setTimeoutMs(int timeoutMs) {
this.timeoutMs = timeoutMs;
}
@Override
public int getTimeoutMs() {
return timeoutMs;
}
} }

View File

@@ -265,7 +265,7 @@ public abstract class AbstractChannel
if (!closeEvent.inError()) if (!closeEvent.inError())
throw e; throw e;
} }
closeEvent.await(conn.getTimeout(), TimeUnit.SECONDS); closeEvent.await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS);
} }
} finally { } finally {
openCloseLock.unlock(); openCloseLock.unlock();

View File

@@ -65,7 +65,7 @@ public abstract class AbstractDirectChannel
public void open() public void open()
throws ConnectionException, TransportException { throws ConnectionException, TransportException {
trans.write(buildOpenReq()); trans.write(buildOpenReq());
openEvent.await(conn.getTimeout(), TimeUnit.SECONDS); openEvent.await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS);
} }
private void gotOpenConfirmation(SSHPacket buf) private void gotOpenConfirmation(SSHPacket buf)

View File

@@ -91,7 +91,7 @@ public class SessionChannel
.putUInt32(width) .putUInt32(width)
.putUInt32(height) .putUInt32(height)
.putBytes(PTYMode.encode(modes)) .putBytes(PTYMode.encode(modes))
).await(conn.getTimeout(), TimeUnit.SECONDS); ).await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS);
} }
@Override @Override
@@ -119,7 +119,7 @@ public class SessionChannel
checkReuse(); checkReuse();
log.info("Will request to exec `{}`", command); log.info("Will request to exec `{}`", command);
sendChannelRequest("exec", true, new Buffer.PlainBuffer().putString(command)) sendChannelRequest("exec", true, new Buffer.PlainBuffer().putString(command))
.await(conn.getTimeout(), TimeUnit.SECONDS); .await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS);
usedUp = true; usedUp = true;
return this; return this;
} }
@@ -175,14 +175,14 @@ public class SessionChannel
.putString(authProto) .putString(authProto)
.putString(authCookie) .putString(authCookie)
.putUInt32(screen) .putUInt32(screen)
).await(conn.getTimeout(), TimeUnit.SECONDS); ).await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS);
} }
@Override @Override
public void setEnvVar(String name, String value) public void setEnvVar(String name, String value)
throws ConnectionException, TransportException { throws ConnectionException, TransportException {
sendChannelRequest("env", true, new Buffer.PlainBuffer().putString(name).putString(value)) sendChannelRequest("env", true, new Buffer.PlainBuffer().putString(name).putString(value))
.await(conn.getTimeout(), TimeUnit.SECONDS); .await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS);
} }
@Override @Override
@@ -195,7 +195,7 @@ public class SessionChannel
public Shell startShell() public Shell startShell()
throws ConnectionException, TransportException { throws ConnectionException, TransportException {
checkReuse(); checkReuse();
sendChannelRequest("shell", true, null).await(conn.getTimeout(), TimeUnit.SECONDS); sendChannelRequest("shell", true, null).await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS);
usedUp = true; usedUp = true;
return this; return this;
} }
@@ -206,7 +206,7 @@ public class SessionChannel
checkReuse(); checkReuse();
log.info("Will request `{}` subsystem", name); log.info("Will request `{}` subsystem", name);
sendChannelRequest("subsystem", true, new Buffer.PlainBuffer().putString(name)) sendChannelRequest("subsystem", true, new Buffer.PlainBuffer().putString(name))
.await(conn.getTimeout(), TimeUnit.SECONDS); .await(conn.getTimeoutMs(), TimeUnit.MILLISECONDS);
usedUp = true; usedUp = true;
return this; return this;
} }

View File

@@ -201,7 +201,7 @@ public class RemotePortForwarder
final byte[] specifics = new Buffer.PlainBuffer().putString(forward.address).putUInt32(forward.port) final byte[] specifics = new Buffer.PlainBuffer().putString(forward.address).putUInt32(forward.port)
.getCompactData(); .getCompactData();
return conn.sendGlobalRequest(reqName, true, specifics) return conn.sendGlobalRequest(reqName, true, specifics)
.retrieve(conn.getTimeout(), TimeUnit.SECONDS); .retrieve(conn.getTimeoutMs(), TimeUnit.MILLISECONDS);
} }
/** @return the active forwards. */ /** @return the active forwards. */