Skip to content

Commit 61174d1

Browse files
committed
bugfix: use Socket connect to deal w/ slow initial timeout
1 parent 7e725e8 commit 61174d1

File tree

1 file changed

+6
-120
lines changed

1 file changed

+6
-120
lines changed

src/com/danga/MemCached/SockIOPool.java

Lines changed: 6 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public class SockIOPool {
144144
private long maxBusyTime = 1000 * 60 * 5; // max idle time for avail sockets
145145
private long maintSleep = 1000 * 5; // maintenance thread sleep time
146146
private int socketTO = 1000 * 10; // default timeout of socket reads
147-
private int socketConnectTO = 0; // default timeout of socket connections
147+
private int socketConnectTO = 1000 * 3; // default timeout of socket connections
148148
private boolean failover = true; // default to failover in event of cache server dead
149149
private boolean nagle = true; // enable/disable Nagle's algorithm
150150
private int hashingAlg = NATIVE_HASH; // default to using the native hash as it is the fastest
@@ -1197,9 +1197,7 @@ public SockIO( SockIOPool pool, String host, int port, int timeout, int connectT
11971197

11981198
this.pool = pool;
11991199

1200-
sock = ( connectTimeout > 0 )
1201-
? getSocket( host, port, connectTimeout )
1202-
: new Socket( host,port );
1200+
sock = getSocket( host, port, connectTimeout );
12031201

12041202
if (timeout >= 0)
12051203
sock.setSoTimeout( timeout );
@@ -1232,9 +1230,7 @@ public SockIO( SockIOPool pool, String host, int timeout, int connectTimeout, bo
12321230
String[] ip = host.split(":");
12331231

12341232
// get socket: default is to use non-blocking connect
1235-
sock = ( connectTimeout > 0 )
1236-
? getSocket( ip[ 0 ], Integer.parseInt( ip[ 1 ] ), connectTimeout )
1237-
: new Socket( ip[ 0 ], Integer.parseInt( ip[ 1 ] ) );
1233+
sock = getSocket( ip[ 0 ], Integer.parseInt( ip[ 1 ] ), connectTimeout );
12381234

12391235
if ( timeout >= 0 )
12401236
sock.setSoTimeout( timeout );
@@ -1261,40 +1257,9 @@ public SockIO( SockIOPool pool, String host, int timeout, int connectTimeout, bo
12611257
* @throws IOException if errors connecting or if connection times out
12621258
*/
12631259
protected static Socket getSocket( String host, int port, int timeout ) throws IOException {
1264-
1265-
// Create a new thread which will attempt to connect to host:port, and start it running
1266-
ConnectThread thread = new ConnectThread( host, port );
1267-
thread.start();
1268-
1269-
Socket socket = null;
1270-
int timer = 0;
1271-
int sleep = 25;
1272-
1273-
while ( timer < timeout ) {
1274-
1275-
// if the thread has a connected socket
1276-
// then return it
1277-
if ( thread.isConnected() )
1278-
return thread.getSocket();
1279-
1280-
// if the thread had an error
1281-
// then throw a new IOException
1282-
if ( thread.isError() )
1283-
throw new IOException();
1284-
1285-
try {
1286-
// sleep for short time before polling again
1287-
Thread.sleep( sleep );
1288-
}
1289-
catch ( InterruptedException ie ) { }
1290-
1291-
// Increment timer
1292-
timer += sleep;
1293-
}
1294-
1295-
// made it through loop without getting connection
1296-
// the connection thread will timeout on its own at OS timeout
1297-
throw new IOException( "Could not connect for " + timeout + " milliseconds" );
1260+
Socket sock = new Socket();
1261+
sock.connect( new InetSocketAddress( host, port ), timeout );
1262+
return sock;
12981263
}
12991264

13001265
/**
@@ -1529,83 +1494,4 @@ public String toString() {
15291494
return (sock == null) ? "" : sock.toString();
15301495
}
15311496
}
1532-
1533-
/**
1534-
* Thread to attempt connection.
1535-
* This will be polled by the main thread. We run the risk of filling up w/
1536-
* threads attempting connections if network is down. However, the falling off
1537-
* mech in the main code should limit this.
1538-
*
1539-
* @author greg whalin <[email protected]>
1540-
* @version 1.2
1541-
*/
1542-
static class ConnectThread extends Thread {
1543-
1544-
// logger
1545-
private static Logger log =
1546-
Logger.getLogger(ConnectThread.class.getName());
1547-
1548-
private Socket socket;
1549-
private String host;
1550-
private int port;
1551-
boolean error;
1552-
1553-
/**
1554-
* Constructor
1555-
*
1556-
* @param host
1557-
* @param port
1558-
*/
1559-
public ConnectThread(String host, int port) {
1560-
this.host = host;
1561-
this.port = port;
1562-
this.socket = null;
1563-
this.error = false;
1564-
this.setDaemon(true);
1565-
}
1566-
1567-
/**
1568-
* start thread running.
1569-
* This attempts to establish a connection.
1570-
*/
1571-
public void run() {
1572-
try {
1573-
socket = new Socket(host, port);
1574-
}
1575-
catch (IOException ioe) {
1576-
error = true;
1577-
}
1578-
1579-
log.debug("socket creation thread leaving for host: " + host);
1580-
}
1581-
1582-
/**
1583-
* Is the new socket connected yet
1584-
*
1585-
* @return
1586-
*/
1587-
public boolean isConnected() {
1588-
return (socket != null && socket.isConnected())
1589-
? true
1590-
: false;
1591-
}
1592-
1593-
/**
1594-
* Did we have an exception while connecting?
1595-
*
1596-
* @return
1597-
*/
1598-
public boolean isError() {
1599-
return error;
1600-
}
1601-
1602-
/**
1603-
* Return the socket.
1604-
*
1605-
* @return
1606-
*/
1607-
public Socket getSocket() {
1608-
return socket;
1609-
}
1610-
}
16111497
}

0 commit comments

Comments
 (0)