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

Commit 0639564

Browse files
committed
Fix resource leak in Redis feature store, switch to try-with-resources, and bump language level to 7
1 parent b53a884 commit 0639564

File tree

2 files changed

+13
-59
lines changed

2 files changed

+13
-59
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ repositories {
1313
allprojects {
1414
group = 'com.launchdarkly'
1515
version = "0.20.0-SNAPSHOT"
16-
sourceCompatibility = 1.6
17-
targetCompatibility = 1.6
16+
sourceCompatibility = 1.7
17+
targetCompatibility = 1.7
1818
}
1919

2020
dependencies {

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

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.net.URI;
1616
import java.util.HashMap;
1717
import java.util.Map;
18+
import java.util.concurrent.ExecutorService;
19+
import java.util.concurrent.Executors;
1820
import java.util.concurrent.TimeUnit;
1921

2022
/**
@@ -130,10 +132,8 @@ public FeatureRep<?> get(String key) {
130132
*/
131133
@Override
132134
public Map<String, FeatureRep<?>> all() {
133-
Jedis jedis = null;
134-
try {
135-
jedis = getJedis();
136-
Map<String,String> featuresJson = getJedis().hgetAll(featuresKey());
135+
try (Jedis jedis = pool.getResource()) {
136+
Map<String,String> featuresJson = jedis.hgetAll(featuresKey());
137137
Map<String, FeatureRep<?>> result = new HashMap<String, FeatureRep<?>>();
138138
Gson gson = new Gson();
139139

@@ -144,10 +144,6 @@ public Map<String, FeatureRep<?>> all() {
144144
result.put(entry.getKey(), rep);
145145
}
146146
return result;
147-
} finally {
148-
if (jedis != null) {
149-
jedis.close();
150-
}
151147
}
152148

153149
}
@@ -159,10 +155,7 @@ public Map<String, FeatureRep<?>> all() {
159155
*/
160156
@Override
161157
public void init(Map<String, FeatureRep<?>> features) {
162-
Jedis jedis = null;
163-
164-
try {
165-
jedis = getJedis();
158+
try (Jedis jedis = pool.getResource()) {
166159
Gson gson = new Gson();
167160
Transaction t = jedis.multi();
168161

@@ -173,10 +166,6 @@ public void init(Map<String, FeatureRep<?>> features) {
173166
}
174167

175168
t.exec();
176-
} finally {
177-
if (jedis != null) {
178-
jedis.close();
179-
}
180169
}
181170
}
182171

@@ -191,9 +180,7 @@ public void init(Map<String, FeatureRep<?>> features) {
191180
*/
192181
@Override
193182
public void delete(String key, int version) {
194-
Jedis jedis = null;
195-
try {
196-
jedis = getJedis();
183+
try (Jedis jedis = pool.getResource()) {
197184
Gson gson = new Gson();
198185
jedis.watch(featuresKey());
199186

@@ -212,13 +199,6 @@ public void delete(String key, int version) {
212199
cache.invalidate(key);
213200
}
214201
}
215-
finally {
216-
if (jedis != null) {
217-
jedis.unwatch();
218-
jedis.close();
219-
}
220-
}
221-
222202
}
223203

224204
/**
@@ -230,9 +210,7 @@ public void delete(String key, int version) {
230210
*/
231211
@Override
232212
public void upsert(String key, FeatureRep<?> feature) {
233-
Jedis jedis = null;
234-
try {
235-
jedis = getJedis();
213+
try (Jedis jedis = pool.getResource()) {
236214
Gson gson = new Gson();
237215
jedis.watch(featuresKey());
238216

@@ -248,12 +226,6 @@ public void upsert(String key, FeatureRep<?> feature) {
248226
cache.invalidate(key);
249227
}
250228
}
251-
finally {
252-
if (jedis != null) {
253-
jedis.unwatch();
254-
jedis.close();
255-
}
256-
}
257229
}
258230

259231
/**
@@ -290,24 +262,15 @@ private String featuresKey() {
290262
}
291263

292264
private Boolean getInit() {
293-
Jedis jedis = null;
294-
295-
try {
296-
jedis = getJedis();
265+
try (Jedis jedis = pool.getResource()) {
297266
return jedis.exists(featuresKey());
298-
} finally {
299-
if (jedis != null) {
300-
jedis.close();
301-
}
302267
}
303268
}
304269

305270
private FeatureRep<?> getRedis(String key) {
306-
Jedis jedis = null;
307-
try {
308-
jedis = getJedis();
271+
try (Jedis jedis = pool.getResource()){
309272
Gson gson = new Gson();
310-
String featureJson = getJedis().hget(featuresKey(), key);
273+
String featureJson = jedis.hget(featuresKey(), key);
311274

312275
if (featureJson == null) {
313276
return null;
@@ -317,21 +280,12 @@ private FeatureRep<?> getRedis(String key) {
317280
FeatureRep<?> f = gson.fromJson(featureJson, type);
318281

319282
return f.deleted ? null : f;
320-
} finally {
321-
if (jedis != null) {
322-
jedis.close();
323-
}
324283
}
325284
}
326285

327-
private final Jedis getJedis() {
328-
return pool.getResource();
329-
}
330-
331286
private final JedisPoolConfig getPoolConfig() {
332287
JedisPoolConfig config = new JedisPoolConfig();
333-
config.setMaxTotal(256);
334-
config.setBlockWhenExhausted(false);
335288
return config;
336289
}
290+
337291
}

0 commit comments

Comments
 (0)