Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get rid of guava #23

Merged
merged 2 commits into from
Aug 21, 2017
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
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ allprojects {
dependencies {
compile "com.squareup.okhttp3:okhttp:3.8.1"
compile "org.slf4j:slf4j-api:1.7.22"
compile "com.google.guava:guava:19.0"
testRuntime "ch.qos.logback:logback-classic:1.1.9"
testCompile "org.mockito:mockito-core:1.10.19"
testCompile "junit:junit:4.11"
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/com/launchdarkly/eventsource/EventSource.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.launchdarkly.eventsource;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import okhttp3.*;
import okio.BufferedSource;
import okio.Okio;
Expand All @@ -21,12 +20,15 @@
import java.security.KeyStore;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import static com.launchdarkly.eventsource.ReadyState.*;
import static java.lang.String.format;

/**
* Client for <a href="https://www.w3.org/TR/2015/REC-eventsource-20150203/">Server-Sent Events</a>
Expand Down Expand Up @@ -60,19 +62,29 @@ public class EventSource implements ConnectionHandler, Closeable {
this.uri = builder.uri;
this.headers = addDefaultHeaders(builder.headers);
this.reconnectTimeMs = builder.reconnectTimeMs;
ThreadFactory eventsThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("okhttp-eventsource-events-[" + name + "]-%d")
.build();
ThreadFactory eventsThreadFactory = createThreadFactory("okhttp-eventsource-events");
this.eventExecutor = Executors.newSingleThreadExecutor(eventsThreadFactory);
ThreadFactory streamThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("okhttp-eventsource-stream-[" + name + "]-%d")
.build();
ThreadFactory streamThreadFactory = createThreadFactory("okhttp-eventsource-stream");
this.streamExecutor = Executors.newSingleThreadExecutor(streamThreadFactory);
this.handler = new AsyncEventHandler(this.eventExecutor, builder.handler);
this.readyState = new AtomicReference<>(RAW);
this.client = builder.clientBuilder.build();
}

private ThreadFactory createThreadFactory(final String type) {
final ThreadFactory backingThreadFactory =
Executors.defaultThreadFactory();
final AtomicLong count = new AtomicLong(0);
return new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = backingThreadFactory.newThread(runnable);
thread.setName(format(Locale.ROOT, "%s-[%s]-%d", type, name, count.getAndIncrement()));
return thread;
}
};
}

public void start() {
if (!readyState.compareAndSet(RAW, CONNECTING)) {
logger.info("Start method called on this already-started EventSource object. Doing nothing");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.launchdarkly.eventsource;

import com.google.common.annotations.VisibleForTesting;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
Expand Down Expand Up @@ -74,7 +72,6 @@ public Socket createSocket(InetAddress inetAddress, int i, InetAddress inetAddre
* @param s the socket
* @return
*/
@VisibleForTesting
static Socket setModernTlsVersionsOnSocket(Socket s) {
if (s != null && (s instanceof SSLSocket)) {
List<String> defaultEnabledProtocols = Arrays.asList(((SSLSocket) s).getSupportedProtocols());
Expand Down