fix: LwHttp - retry when address lookup failed

- use 5sec read timeout
- use tcp_nodelay
This commit is contained in:
Hannes Janetzek 2014-06-02 10:38:06 +02:00
parent a26aa9de15
commit 19701b7563

View File

@ -22,8 +22,8 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL; import java.net.URL;
import java.net.UnknownHostException;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
@ -60,10 +60,10 @@ public class LwHttp implements HttpEngine {
private OutputStream mCommandStream; private OutputStream mCommandStream;
private Buffer mResponseStream; private Buffer mResponseStream;
private long mLastRequest = 0; private long mLastRequest = 0;
private SocketAddress mSockAddr; private InetSocketAddress mSockAddr;
/** Server requested to close the connection */ /** Server requested to close the connection */
private boolean mMustClose; private boolean mMustCloseConnection;
private final byte[] REQUEST_GET_START; private final byte[] REQUEST_GET_START;
private final byte[] REQUEST_GET_END; private final byte[] REQUEST_GET_END;
@ -298,7 +298,7 @@ public class LwHttp implements HttpEngine {
} else if (check(HEADER_ENCODING_GZIP, buf, pos, end)) { } else if (check(HEADER_ENCODING_GZIP, buf, pos, end)) {
gzip = true; gzip = true;
} else if (check(HEADER_CONNECTION_CLOSE, buf, pos, end)) { } else if (check(HEADER_CONNECTION_CLOSE, buf, pos, end)) {
mMustClose = true; mMustCloseConnection = true;
} }
} }
@ -367,36 +367,41 @@ public class LwHttp implements HttpEngine {
log.debug("request: {}", new String(mRequestBuffer, 0, len)); log.debug("request: {}", new String(mRequestBuffer, 0, len));
try { try {
writeRequest(mRequestBuffer, len); writeRequest(len);
} catch (IOException e) { } catch (IOException e) {
log.debug("recreate connection"); log.debug("recreate connection");
close(); close();
lwHttpConnect(); lwHttpConnect();
writeRequest(mRequestBuffer, len); writeRequest(len);
} }
} }
private void writeRequest(byte[] request, int length) throws IOException { private void writeRequest(int length) throws IOException {
mCommandStream.write(request, 0, length); mCommandStream.write(mRequestBuffer, 0, length);
mCommandStream.flush(); //mCommandStream.flush();
} }
private boolean lwHttpConnect() throws IOException { private void lwHttpConnect() throws IOException {
if (mSockAddr == null) if (mSockAddr == null || mSockAddr.isUnresolved()) {
mSockAddr = new InetSocketAddress(mHost, mPort); mSockAddr = new InetSocketAddress(mHost, mPort);
if (mSockAddr.isUnresolved())
throw new UnknownHostException(mHost);
}
try { try {
mSocket = new Socket(); mSocket = new Socket();
mSocket.connect(mSockAddr, 30000);
mSocket.setTcpNoDelay(true); mSocket.setTcpNoDelay(true);
mSocket.setSoTimeout(5000);
mSocket.connect(mSockAddr, 10000);
mCommandStream = mSocket.getOutputStream(); mCommandStream = mSocket.getOutputStream();
mResponseStream = new Buffer(mSocket.getInputStream()); mResponseStream = new Buffer(mSocket.getInputStream());
mMustCloseConnection = false;
} catch (IOException e) { } catch (IOException e) {
close(); close();
throw e; throw e;
} }
mMustClose = false;
return true;
} }
@Override @Override
@ -427,6 +432,7 @@ public class LwHttp implements HttpEngine {
mResponseStream.setCache(null); mResponseStream.setCache(null);
if (!mResponseStream.finishedReading()) { if (!mResponseStream.finishedReading()) {
if (dbg)
log.debug("invalid buffer position"); log.debug("invalid buffer position");
close(); close();
return true; return true;
@ -437,7 +443,7 @@ public class LwHttp implements HttpEngine {
return false; return false;
} }
if (mMustClose) { if (mMustCloseConnection) {
close(); close();
return true; return true;
} }