Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
Closed
24 changes: 14 additions & 10 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ apply plugin: 'signing'
apply plugin: 'idea'
apply plugin: 'com.github.johnrengelman.shadow'

configurations.all {
// check for updates every build
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

repositories {
mavenCentral()
mavenLocal()

// Before LaunchDarkly release artifacts get synced to Maven Central they are here along with snapshots:
maven {
url "https://oss.sonatype.org/content/groups/public/"
}
}

allprojects {
group = 'com.launchdarkly'
version = "0.20.0"
version = "1.0.0-SNAPSHOT"
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
Expand All @@ -24,9 +34,9 @@ dependencies {
compile "com.google.code.gson:gson:2.2.4"
compile "com.google.guava:guava:19.0"
compile "org.slf4j:slf4j-api:1.7.7"
compile "com.launchdarkly:okhttp-eventsource:0.1.0"
compile group: "com.launchdarkly", name: "okhttp-eventsource", version: "0.1.3-SNAPSHOT", changing: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do exactly? Never encountered this before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From here: https://discuss.gradle.org/t/how-to-get-gradle-to-download-newer-snapshots-to-gradle-cache-when-using-an-ivy-repository/7344

In short it goes with the addition on lines 8-12. We're instructing gradle to always go look in a repo for the latest version of this dep. When this is pinned to a stable 1.0.0 it won't be needed, but is very handy for local development and potentially build/deploy systems that that might have older snapshots cached.

I am not a gradle expert, so this might not be the best way to do it.

compile "redis.clients:jedis:2.8.0"
testCompile "org.easymock:easymock:3.3"
testCompile "org.easymock:easymock:3.4"
testCompile 'junit:junit:[4.10,)'
testRuntime "org.slf4j:slf4j-simple:1.7.7"
}
Expand All @@ -50,7 +60,7 @@ buildscript {
}
dependencies {
classpath 'org.ajoberstar:gradle-git:0.12.0'
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
}
}

Expand Down Expand Up @@ -125,12 +135,6 @@ uploadArchives {
description 'Official LaunchDarkly SDK for Java'
url 'https://github.com/launchdarkly/java-client'

scm {
connection 'scm:svn:http://foo.googlecode.com/svn/trunk/'
developerConnection 'scm:svn:https://foo.googlecode.com/svn/trunk/'
url 'http://foo.googlecode.com/svn/trunk/'
}

licenses {
license {
name 'The Apache License, Version 2.0'
Expand Down
37 changes: 19 additions & 18 deletions src/main/java/com/launchdarkly/client/EventProcessor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.launchdarkly.client;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.gson.Gson;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand All @@ -14,22 +15,34 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

class EventProcessor implements Closeable {
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory());
ThreadFactory threadFactory = new ThreadFactoryBuilder()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an extra linefeed there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This thread factory doesn't actually need to be a class member. The only reason it's assigned here is that we're not instantiating the scheduler in the ctor. I would suggest moving the initialization there so we don't unnecessarily keep a reference to the thread factory.

.setDaemon(true)
.setNameFormat("LaunchDarkly-EventProcessor-%d")
.build();
private final ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor(threadFactory);
private final Random random = new Random();
private final BlockingQueue<Event> queue;
private final String apiKey;
private final LDConfig config;
private final Consumer consumer;

EventProcessor(String apiKey, LDConfig config) {
this.apiKey = apiKey;
this.queue = new ArrayBlockingQueue<>(config.capacity);
this.consumer = new Consumer(config);
this.config = config;
this.scheduler.scheduleAtFixedRate(consumer, 0, config.flushInterval, TimeUnit.SECONDS);
}

boolean sendEvent(Event e) {
if (config.samplingInterval > 0 && random.nextInt(config.samplingInterval) != 0) {
return true;
}
return queue.offer(e);
}

Expand All @@ -43,18 +56,8 @@ public void flush() {
this.consumer.flush();
}

static class DaemonThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
return thread;
}
}

class Consumer implements Runnable {
private final Logger logger = LoggerFactory.getLogger(Consumer.class);


private final CloseableHttpClient client;
private final LDConfig config;

Expand All @@ -78,6 +81,7 @@ public void flush() {
}

private void postEvents(List<Event> events) {
logger.debug("Posting " + events.size() + " event(s)..");
CloseableHttpResponse response = null;
Gson gson = new Gson();
String json = gson.toJson(events);
Expand All @@ -95,24 +99,21 @@ private void postEvents(List<Event> events) {
if (status >= 300) {
if (status == HttpStatus.SC_UNAUTHORIZED) {
logger.error("Invalid API key");
}
else {
} else {
logger.error("Unexpected status code: " + status);
}
}
else {
logger.debug("Successfully processed events");
} else {
logger.debug("Successfully posted " + events.size() + " event(s).");
}
} catch (IOException e) {
logger.error("Unhandled exception in LaunchDarkly client", e);
logger.error("Unhandled exception in LaunchDarkly client attempting to connect to URI: " + config.eventsURI, e);
} finally {
try {
if (response != null) response.close();
} catch (IOException e) {
logger.error("Unhandled exception in LaunchDarkly client", e);
}
}

}
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/launchdarkly/client/FeatureRequestor.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Map<String, FeatureRep<?>> makeAllRequest(boolean latest) throws IOException {

CloseableHttpResponse response = null;
try {
logger.debug("Making request: " + request);
response = client.execute(request, context);

logCacheResponse(context.getCacheResponseStatus());
Expand All @@ -76,7 +77,10 @@ Map<String, FeatureRep<?>> makeAllRequest(boolean latest) throws IOException {

Type type = new TypeToken<Map<String, FeatureRep<?>>>() {}.getType();

Map<String, FeatureRep<?>> result = gson.fromJson(EntityUtils.toString(response.getEntity()), type);
String json = EntityUtils.toString(response.getEntity());
logger.debug("Got response: " + response.toString());
logger.debug("Got Response body: " + json);
Map<String, FeatureRep<?>> result = gson.fromJson(json, type);
return result;
}
finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
*/
public class InMemoryFeatureStore implements FeatureStore {

final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
final Map<String, FeatureRep<?>> features = new HashMap<>();
volatile boolean initialized = false;
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private final Map<String, FeatureRep<?>> features = new HashMap<>();
private volatile boolean initialized = false;


/**
Expand Down
Loading