Skip to content

Commit 38a24e6

Browse files
authored
Merge pull request #30 from yuetang95/master
Improve ClientMode determination logic
2 parents ac3c39c + 9f9ddd5 commit 38a24e6

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

src/main/java/net/spy/memcached/ClientMode.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
* The modes in which the client can operate.
1010
*/
1111
public enum ClientMode {
12-
12+
/**
13+
* Default client mode, we will perform mode discovery based on the endpoint dns name.
14+
*/
15+
Unset,
1316
/**
1417
* In Static Client mode, the set of endpoints specified during initialization is used throughout the lifetime of the client object.
1518
*/

src/main/java/net/spy/memcached/ConnectionFactory.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ MemcachedNode createMemcachedNode(SocketAddress sa, SocketChannel c,
117117
* @return the clientMode.
118118
*/
119119
ClientMode getClientMode();
120-
120+
121+
/**
122+
* Set the client mode in client. Should only be used in the constructor when determine the client mode.
123+
*/
124+
void setClientMode(ClientMode clientMode);
125+
121126
/**
122127
* The interval used for periodic polling of configuration.
123128
* @return the interval in milliseconds

src/main/java/net/spy/memcached/DefaultConnectionFactory.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public class DefaultConnectionFactory extends SpyObject implements
7373
/**
7474
* Default client mode.
7575
*/
76-
public static final ClientMode DEFAULT_CLIENT_MODE = ClientMode.Dynamic;
76+
public static final ClientMode DEFAULT_CLIENT_MODE = ClientMode.Unset;
7777

7878
/**
7979
* Default failure mode.
@@ -132,7 +132,7 @@ public class DefaultConnectionFactory extends SpyObject implements
132132
*/
133133
public static final long DEFAULT_AUTH_WAIT_TIME = 1000;
134134

135-
private final ClientMode clientMode;
135+
private ClientMode clientMode;
136136
protected final int opQueueLen;
137137
private final int readBufSize;
138138
private final HashAlgorithm hashAlg;
@@ -205,7 +205,12 @@ public DefaultConnectionFactory() {
205205
public ClientMode getClientMode(){
206206
return clientMode;
207207
}
208-
208+
209+
@Override
210+
public void setClientMode(ClientMode clientMode) {
211+
this.clientMode = clientMode;
212+
}
213+
209214
public long getDynamicModePollingInterval(){
210215
return ConfigurationPoller.DEFAULT_POLL_INTERVAL;
211216
}

src/main/java/net/spy/memcached/MemcachedClient.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ public MemcachedClient(List<InetSocketAddress> addrs) throws IOException {
226226
}
227227

228228
public MemcachedClient(ConnectionFactory cf, List<InetSocketAddress> addrs) throws IOException{
229-
this(cf, addrs, false);
229+
this(cf, addrs, cf != null && cf.getClientMode() == ClientMode.Unset);
230230
}
231-
231+
232232
/**
233233
* Get a memcache client over the specified memcached locations.
234234
*
@@ -255,13 +255,11 @@ private MemcachedClient(ConnectionFactory cf, List<InetSocketAddress> addrs, boo
255255
String hostName = addrs.get(0).getHostName();
256256
//All config endpoints has ".cfg." subdomain in the DNS name.
257257
if(hostName != null && hostName.contains(".cfg.")){
258-
cf = new DefaultConnectionFactory(ClientMode.Dynamic);
258+
cf = updateClientMode(cf, ClientMode.Dynamic);
259259
}
260260
}
261261
//Fallback to static mode
262-
if(cf == null){
263-
cf = new DefaultConnectionFactory(ClientMode.Static);
264-
}
262+
cf = updateClientMode(cf, ClientMode.Static);
265263
}
266264

267265
if (cf == null) {
@@ -276,7 +274,6 @@ private MemcachedClient(ConnectionFactory cf, List<InetSocketAddress> addrs, boo
276274
throw new IllegalArgumentException("Only one configuration endpoint is valid with dynamic client mode.");
277275
}
278276

279-
280277
connFactory = cf;
281278
clientMode = cf.getClientMode();
282279
tcService = new TranscodeService(cf.isDaemon());
@@ -297,7 +294,16 @@ private MemcachedClient(ConnectionFactory cf, List<InetSocketAddress> addrs, boo
297294
addObserver(this);
298295
}
299296
}
300-
297+
298+
private ConnectionFactory updateClientMode(ConnectionFactory f, ClientMode mode) {
299+
if (f == null) {
300+
f = new DefaultConnectionFactory(mode);
301+
} else {
302+
f.setClientMode(mode);
303+
}
304+
return f;
305+
}
306+
301307
/**
302308
* Establish a connection to the configuration endpoint and get the list of cache node endpoints. Then initialize the
303309
* memcached client with the cache node endpoints list.

0 commit comments

Comments
 (0)