Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/main/java/com/devcycle/sdk/server/local/DVCLocalClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public final class DVCLocalClient {
private final DVCOptions dvcOptions;

private static LocalBucketing localBucketing = new LocalBucketing();

private EnvironmentConfigManager configManager;

private final String serverKey;

Expand All @@ -24,8 +26,8 @@ public DVCLocalClient(String serverKey) {
this(serverKey, DVCOptions.builder().build());
}

public DVCLocalClient(String serverKey, DVCOptions dvcOptions ) {
new EnvironmentConfigManager(serverKey, dvcOptions);
public DVCLocalClient(String serverKey, DVCOptions dvcOptions) {
configManager = new EnvironmentConfigManager(serverKey, localBucketing, dvcOptions);
this.serverKey = serverKey;
this.dvcOptions = dvcOptions;
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Expand Down Expand Up @@ -65,7 +67,11 @@ public <T> Variable<T> variable(User user, String key, T defaultValue) {
if (defaultValue == null) {
throw new IllegalArgumentException("defaultValue cannot be null");
}


if (!configManager.isConfigInitialized()) {
System.out.println("Variable called before DVCClient has initialized, returning default value");
}

try {
String userString = OBJECT_MAPPER.writeValueAsString(user);

Expand All @@ -88,6 +94,13 @@ public <T> Variable<T> variable(User user, String key, T defaultValue) {
.reasonUsingDefaultValue("Variable not found")
.build();

// TODO queue events
// eventQueue.queueAggregateEvent(
// user,
// new Event(type: EventTypes.variableDefaulted, target: key),
// null
// );

return variable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ public final class EnvironmentConfigManager {
private String environmentKey;
private int pollingIntervalMS;

public EnvironmentConfigManager(String environmentKey, DVCOptions options) {
private LocalBucketing localBucketing;

public EnvironmentConfigManager(String environmentKey, LocalBucketing localBucketing, DVCOptions options) {
this.environmentKey = environmentKey;

configApiClient = new DVCApiClient(environmentKey, options).initialize();

int configPollingIntervalMs = options.getConfigPollingIntervalMs();
pollingIntervalMS = configPollingIntervalMs >= MIN_INTERVALS_MS ? configPollingIntervalMs
: DEFAULT_POLL_INTERVAL_MS;

this.localBucketing = localBucketing;

setupScheduler();
}
Expand All @@ -47,7 +51,7 @@ private void setupScheduler() {
public void run() {
try {
getConfig();
} catch (DVCException e) {
} catch (DVCException | JsonProcessingException e) {
e.printStackTrace();
}
}
Expand All @@ -56,14 +60,18 @@ public void run() {
scheduler.scheduleAtFixedRate(getConfigRunnable, 0, this.pollingIntervalMS, TimeUnit.MILLISECONDS);
}

private ProjectConfig getConfig() throws DVCException {
public boolean isConfigInitialized() {
return config != null;
}

private ProjectConfig getConfig() throws DVCException, JsonProcessingException {
Call<ProjectConfig> config = this.configApiClient.getConfig(this.environmentKey, this.configETag);

this.config = getConfigResponse(config);
return this.config;
}

private ProjectConfig getConfigResponse(Call<ProjectConfig> call) throws DVCException {
private ProjectConfig getConfigResponse(Call<ProjectConfig> call) throws DVCException, JsonProcessingException {
ErrorResponse errorResponse = ErrorResponse.builder().build();
Response<ProjectConfig> response;

Expand All @@ -78,6 +86,18 @@ private ProjectConfig getConfigResponse(Call<ProjectConfig> call) throws DVCExce
errorResponse.setMessage("Unknown error");

if (response.isSuccessful()) {
ProjectConfig config = response.body();
try {
ObjectMapper mapper = new ObjectMapper();
localBucketing.storeConfig(environmentKey, mapper.writeValueAsString(config));
} catch (JsonProcessingException e) {
if (this.config != null) {
System.out.printf("Unable to parse config with etag: %s. Using cache, etag %s%n", currentETag, this.configETag);
return this.config;
} else {
throw e;
}
}
String currentETag = response.headers().get("ETag");
this.configETag = currentETag;
return response.body();
Expand Down