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

Commit

Permalink
Merge pull request #179 from launchdarkly/eb/ch62493/component-config-1
Browse files Browse the repository at this point in the history
(4.x - #1) add component-scoped configuration for polling and streaming
  • Loading branch information
eli-darkly authored Jan 23, 2020
2 parents 38f48d7 + a8c4c02 commit a8d6a06
Show file tree
Hide file tree
Showing 33 changed files with 693 additions and 251 deletions.
257 changes: 206 additions & 51 deletions src/main/java/com/launchdarkly/client/Components.java

Large diffs are not rendered by default.

20 changes: 14 additions & 6 deletions src/main/java/com/launchdarkly/client/DefaultFeatureRequestor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -21,7 +22,10 @@
import okhttp3.Request;
import okhttp3.Response;

class DefaultFeatureRequestor implements FeatureRequestor {
/**
* Implementation of getting flag data via a polling request. Used by both streaming and polling components.
*/
final class DefaultFeatureRequestor implements FeatureRequestor {
private static final Logger logger = LoggerFactory.getLogger(DefaultFeatureRequestor.class);
private static final String GET_LATEST_FLAGS_PATH = "/sdk/latest-flags";
private static final String GET_LATEST_SEGMENTS_PATH = "/sdk/latest-segments";
Expand All @@ -30,18 +34,22 @@ class DefaultFeatureRequestor implements FeatureRequestor {

private final String sdkKey;
private final LDConfig config;
private final URI baseUri;
private final OkHttpClient httpClient;
private final boolean useCache;

DefaultFeatureRequestor(String sdkKey, LDConfig config) {
DefaultFeatureRequestor(String sdkKey, LDConfig config, URI baseUri, boolean useCache) {
this.sdkKey = sdkKey;
this.config = config;
this.config = config; // this is no longer the source of truth for baseURI, but it can still affect HTTP behavior
this.baseUri = baseUri;
this.useCache = useCache;

OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
configureHttpClientBuilder(config, httpBuilder);

// HTTP caching is used only for FeatureRequestor. However, when streaming is enabled, HTTP GETs
// made by FeatureRequester will always guarantee a new flag state, so we disable the cache.
if (!config.stream) {
if (useCache) {
File cacheDir = Files.createTempDir();
Cache cache = new Cache(cacheDir, MAX_HTTP_CACHE_SIZE_BYTES);
httpBuilder.cache(cache);
Expand Down Expand Up @@ -78,7 +86,7 @@ public AllData getAllData() throws IOException, HttpErrorException {

private String get(String path) throws IOException, HttpErrorException {
Request request = getRequestBuilder(sdkKey)
.url(config.baseURI.resolve(path).toURL())
.url(baseUri.resolve(path).toURL())
.get()
.build();

Expand All @@ -92,7 +100,7 @@ private String get(String path) throws IOException, HttpErrorException {
}
logger.debug("Get flag(s) response: " + response.toString() + " with body: " + body);
logger.debug("Network response: " + response.networkResponse());
if(!config.stream) {
if (useCache) {
logger.debug("Cache hit count: " + httpClient.cache().hitCount() + " Cache network Count: " + httpClient.cache().networkCount());
logger.debug("Cache response: " + response.cacheResponse());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Rather than creating intermediate objects to represent this schema, we use the Gson streaming
* output API to construct JSON directly.
*/
class EventOutputFormatter {
final class EventOutputFormatter {
private final LDConfig config;

EventOutputFormatter(LDConfig config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public enum StaleValuesPolicy {
/**
* Used internally for backward compatibility.
* @return the equivalent enum value
* @since 4.11.0
* @since 4.12.0
*/
public PersistentDataStoreBuilder.StaleValuesPolicy toNewEnum() {
switch (this) {
Expand All @@ -113,7 +113,7 @@ public PersistentDataStoreBuilder.StaleValuesPolicy toNewEnum() {
* Used internally for backward compatibility.
* @param policy the enum value in the new API
* @return the equivalent enum value
* @since 4.11.0
* @since 4.12.0
*/
public static StaleValuesPolicy fromNewEnum(PersistentDataStoreBuilder.StaleValuesPolicy policy) {
switch (policy) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.launchdarkly.client;

@SuppressWarnings("serial")
class HttpErrorException extends Exception {
final class HttpErrorException extends Exception {
private final int status;

public HttpErrorException(int status) {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/launchdarkly/client/LDClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.launchdarkly.client;

import com.google.gson.JsonElement;
import com.launchdarkly.client.Components.NullUpdateProcessor;
import com.launchdarkly.client.value.LDValue;

import org.apache.commons.codec.binary.Hex;
Expand Down Expand Up @@ -30,7 +31,9 @@
* a single {@code LDClient} for the lifetime of their application.
*/
public final class LDClient implements LDClientInterface {
private static final Logger logger = LoggerFactory.getLogger(LDClient.class);
// Package-private so other classes can log under the top-level logger's tag
static final Logger logger = LoggerFactory.getLogger(LDClient.class);

private static final String HMAC_ALGORITHM = "HmacSHA256";
static final String CLIENT_VERSION = getClientVersion();

Expand Down Expand Up @@ -83,12 +86,13 @@ public LDClient(String sdkKey, LDConfig config) {
Components.defaultEventProcessor() : config.eventProcessorFactory;
this.eventProcessor = epFactory.createEventProcessor(sdkKey, config);

@SuppressWarnings("deprecation") // defaultUpdateProcessor will be replaced by streamingDataSource once the deprecated config.stream is removed
UpdateProcessorFactory upFactory = config.dataSourceFactory == null ?
Components.defaultDataSource() : config.dataSourceFactory;
Components.defaultUpdateProcessor() : config.dataSourceFactory;
this.updateProcessor = upFactory.createUpdateProcessor(sdkKey, config, featureStore);
Future<Void> startFuture = updateProcessor.start();
if (config.startWaitMillis > 0L) {
if (!config.offline && !config.useLdd) {
if (!(updateProcessor instanceof NullUpdateProcessor)) {
logger.info("Waiting up to " + config.startWaitMillis + " milliseconds for LaunchDarkly client to start...");
}
try {
Expand Down
Loading

0 comments on commit a8d6a06

Please sign in to comment.