Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 28c1cc5

Browse files
committed
Use a thread-safe Jedis pool
1 parent a54c1b0 commit 28c1cc5

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

src/main/java/com/launchdarkly/client/FeatureStore.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.launchdarkly.client;
22

3+
import java.io.Closeable;
34
import java.util.Map;
45

56
/**
@@ -13,7 +14,7 @@
1314
* of features based on update messages that may be received out-of-order.
1415
*
1516
*/
16-
public interface FeatureStore {
17+
public interface FeatureStore extends Closeable {
1718
/**
1819
*
1920
* Returns the {@link com.launchdarkly.client.FeatureRep} to which the specified key is mapped, or
@@ -71,4 +72,5 @@ public interface FeatureStore {
7172
* @return true if this store has been initialized
7273
*/
7374
boolean initialized();
75+
7476
}

src/main/java/com/launchdarkly/client/InMemoryFeatureStore.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.launchdarkly.client;
22

3+
import java.io.IOException;
34
import java.util.HashMap;
45
import java.util.Map;
56
import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -141,4 +142,14 @@ public void upsert(String key, FeatureRep<?> feature) {
141142
public boolean initialized() {
142143
return initialized;
143144
}
145+
146+
/**
147+
* Does nothing; this class does not have any resources to release
148+
* @throws IOException
149+
*/
150+
@Override
151+
public void close() throws IOException
152+
{
153+
return;
154+
}
144155
}

src/main/java/com/launchdarkly/client/RedisFeatureStore.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
import com.google.gson.reflect.TypeToken;
88
import org.apache.http.util.EntityUtils;
99
import redis.clients.jedis.Jedis;
10+
import redis.clients.jedis.JedisPool;
11+
import redis.clients.jedis.JedisPoolConfig;
1012
import redis.clients.jedis.Transaction;
1113

14+
import java.io.IOException;
1215
import java.lang.reflect.Type;
1316
import java.net.URI;
1417
import java.util.HashMap;
@@ -22,7 +25,7 @@
2225
*/
2326
public class RedisFeatureStore implements FeatureStore {
2427
private static final String DEFAULT_PREFIX = "launchdarkly";
25-
private final Jedis jedis;
28+
private final JedisPool pool;
2629
private LoadingCache<String, FeatureRep<?>> cache;
2730
private LoadingCache<String, Boolean> initCache;
2831
private String prefix;
@@ -36,7 +39,7 @@ public class RedisFeatureStore implements FeatureStore {
3639
* @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed
3740
*/
3841
public RedisFeatureStore(String host, int port, String prefix, long cacheTimeSecs) {
39-
jedis = new Jedis(host, port);
42+
pool = new JedisPool(new JedisPoolConfig(), host, port);
4043
setPrefix(prefix);
4144
createCache(cacheTimeSecs);
4245
createInitCache(cacheTimeSecs);
@@ -50,7 +53,7 @@ public RedisFeatureStore(String host, int port, String prefix, long cacheTimeSec
5053
* @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed
5154
*/
5255
public RedisFeatureStore(URI uri, String prefix, long cacheTimeSecs) {
53-
jedis = new Jedis(uri);
56+
pool = new JedisPool(new JedisPoolConfig(), uri);
5457
setPrefix(prefix);
5558
createCache(cacheTimeSecs);
5659
createInitCache(cacheTimeSecs);
@@ -61,7 +64,7 @@ public RedisFeatureStore(URI uri, String prefix, long cacheTimeSecs) {
6164
*
6265
*/
6366
public RedisFeatureStore() {
64-
jedis = new Jedis("localhost");
67+
pool = new JedisPool(new JedisPoolConfig(), "localhost");
6568
this.prefix = DEFAULT_PREFIX;
6669
}
6770

@@ -128,7 +131,7 @@ public FeatureRep<?> get(String key) {
128131
*/
129132
@Override
130133
public Map<String, FeatureRep<?>> all() {
131-
Map<String,String> featuresJson = jedis.hgetAll(featuresKey());
134+
Map<String,String> featuresJson = jedis().hgetAll(featuresKey());
132135
Map<String, FeatureRep<?>> result = new HashMap<String, FeatureRep<?>>();
133136
Gson gson = new Gson();
134137

@@ -149,6 +152,7 @@ public Map<String, FeatureRep<?>> all() {
149152
*/
150153
@Override
151154
public void init(Map<String, FeatureRep<?>> features) {
155+
Jedis jedis = jedis();
152156
Gson gson = new Gson();
153157
Transaction t = jedis.multi();
154158

@@ -172,6 +176,7 @@ public void init(Map<String, FeatureRep<?>> features) {
172176
*/
173177
@Override
174178
public void delete(String key, int version) {
179+
Jedis jedis = jedis();
175180
try {
176181
Gson gson = new Gson();
177182
jedis.watch(featuresKey());
@@ -206,6 +211,7 @@ public void delete(String key, int version) {
206211
*/
207212
@Override
208213
public void upsert(String key, FeatureRep<?> feature) {
214+
Jedis jedis = jedis();
209215
try {
210216
Gson gson = new Gson();
211217
jedis.watch(featuresKey());
@@ -245,18 +251,28 @@ public boolean initialized() {
245251
return getInit();
246252
}
247253

254+
/**
255+
* Releases all resources associated with the store. The store must no longer be used once closed.
256+
* @throws IOException
257+
*/
258+
public void close() throws IOException
259+
{
260+
pool.destroy();
261+
}
262+
263+
248264

249265
private String featuresKey() {
250266
return prefix + ":features";
251267
}
252268

253269
private Boolean getInit() {
254-
return jedis.exists(featuresKey());
270+
return jedis().exists(featuresKey());
255271
}
256272

257273
private FeatureRep<?> getRedis(String key) {
258274
Gson gson = new Gson();
259-
String featureJson = jedis.hget(featuresKey(), key);
275+
String featureJson = jedis().hget(featuresKey(), key);
260276

261277
if (featureJson == null) {
262278
return null;
@@ -267,4 +283,8 @@ private FeatureRep<?> getRedis(String key) {
267283

268284
return f.deleted ? null : f;
269285
}
286+
287+
private final Jedis jedis() {
288+
return pool.getResource();
289+
}
270290
}

src/main/java/com/launchdarkly/client/StreamProcessor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ public void close() throws IOException {
101101
if (es != null) {
102102
es.close();
103103
}
104+
if (store != null) {
105+
store.close();
106+
}
104107
}
105108

106109
boolean initialized() {

0 commit comments

Comments
 (0)