This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
Support for indirect stream events #25
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
91b188e
Abstract feature request code into new class
jkodumal 8746374
Support for indirect patch events
jkodumal 57b1b6e
Increase the maximum cache object size
jkodumal dbe4cf8
Change variable name to better reflect intent
jkodumal ac3fcb2
Support for indirect put events
jkodumal a3dbb7b
Fix potential NPEs in building Users
jkodumal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
src/main/java/com/launchdarkly/client/FeatureRequestor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package com.launchdarkly.client; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.reflect.TypeToken; | ||
import org.apache.http.HttpStatus; | ||
import org.apache.http.client.cache.CacheResponseStatus; | ||
import org.apache.http.client.cache.HttpCacheContext; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.cache.CacheConfig; | ||
import org.apache.http.impl.client.cache.CachingHttpClients; | ||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | ||
import org.apache.http.util.EntityUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.util.Map; | ||
|
||
class FeatureRequestor { | ||
|
||
private final String apiKey; | ||
private final LDConfig config; | ||
private final CloseableHttpClient client; | ||
private static final Logger logger = LoggerFactory.getLogger(FeatureRequestor.class); | ||
|
||
FeatureRequestor(String apiKey, LDConfig config) { | ||
this.apiKey = apiKey; | ||
this.config = config; | ||
this.client = createClient(); | ||
} | ||
|
||
protected CloseableHttpClient createClient() { | ||
CloseableHttpClient client; | ||
PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(); | ||
manager.setMaxTotal(100); | ||
manager.setDefaultMaxPerRoute(20); | ||
|
||
CacheConfig cacheConfig = CacheConfig.custom() | ||
.setMaxCacheEntries(1000) | ||
.setMaxObjectSize(131072) | ||
.setSharedCache(false) | ||
.build(); | ||
|
||
RequestConfig requestConfig = RequestConfig.custom() | ||
.setConnectTimeout(config.connectTimeout) | ||
.setSocketTimeout(config.socketTimeout) | ||
.setProxy(config.proxyHost) | ||
.build(); | ||
client = CachingHttpClients.custom() | ||
.setCacheConfig(cacheConfig) | ||
.setConnectionManager(manager) | ||
.setDefaultRequestConfig(requestConfig) | ||
.build(); | ||
return client; | ||
} | ||
|
||
Map<String, FeatureRep<?>> makeAllRequest(boolean latest) throws IOException { | ||
Gson gson = new Gson(); | ||
HttpCacheContext context = HttpCacheContext.create(); | ||
|
||
String resource = latest ? "/api/eval/latest-features" : "/api/eval/features"; | ||
|
||
HttpGet request = config.getRequest(apiKey, resource); | ||
|
||
CloseableHttpResponse response = null; | ||
try { | ||
response = client.execute(request, context); | ||
|
||
logCacheResponse(context.getCacheResponseStatus()); | ||
|
||
handleResponseStatus(response.getStatusLine().getStatusCode(), null); | ||
|
||
Type type = new TypeToken<Map<String, FeatureRep<?>>>() {}.getType(); | ||
|
||
Map<String, FeatureRep<?>> result = gson.fromJson(EntityUtils.toString(response.getEntity()), type); | ||
return result; | ||
} | ||
finally { | ||
try { | ||
if (response != null) response.close(); | ||
} catch (IOException e) { | ||
} | ||
} | ||
} | ||
|
||
void logCacheResponse(CacheResponseStatus status) { | ||
switch (status) { | ||
case CACHE_HIT: | ||
logger.debug("A response was generated from the cache with " + | ||
"no requests sent upstream"); | ||
break; | ||
case CACHE_MODULE_RESPONSE: | ||
logger.debug("The response was generated directly by the " + | ||
"caching module"); | ||
break; | ||
case CACHE_MISS: | ||
logger.debug("The response came from an upstream server"); | ||
break; | ||
case VALIDATED: | ||
logger.debug("The response was generated from the cache " + | ||
"after validating the entry with the origin server"); | ||
break; | ||
} | ||
} | ||
|
||
void handleResponseStatus(int status, String featureKey) throws IOException { | ||
|
||
if (status != HttpStatus.SC_OK) { | ||
if (status == HttpStatus.SC_UNAUTHORIZED) { | ||
logger.error("Invalid API key"); | ||
} else if (status == HttpStatus.SC_NOT_FOUND) { | ||
if (featureKey != null) { | ||
logger.error("Unknown feature key: " + featureKey); | ||
} | ||
else { | ||
logger.error("Resource not found"); | ||
} | ||
} else { | ||
logger.error("Unexpected status code: " + status); | ||
} | ||
throw new IOException("Failed to fetch flag"); | ||
} | ||
|
||
} | ||
|
||
<T> FeatureRep<T> makeRequest(String featureKey, boolean latest) throws IOException { | ||
Gson gson = new Gson(); | ||
HttpCacheContext context = HttpCacheContext.create(); | ||
|
||
String resource = latest ? "/api/eval/latest-features/" : "/api/eval/features/"; | ||
|
||
HttpGet request = config.getRequest(apiKey,resource + featureKey); | ||
|
||
CloseableHttpResponse response = null; | ||
try { | ||
response = client.execute(request, context); | ||
|
||
logCacheResponse(context.getCacheResponseStatus()); | ||
|
||
handleResponseStatus(response.getStatusLine().getStatusCode(), featureKey); | ||
|
||
Type type = new TypeToken<FeatureRep<T>>() {}.getType(); | ||
|
||
FeatureRep<T> result = gson.fromJson(EntityUtils.toString(response.getEntity()), type); | ||
return result; | ||
} | ||
finally { | ||
try { | ||
if (response != null) response.close(); | ||
} catch (IOException e) { | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated bug fixes