Skip to content

Commit c0af1db

Browse files
authored
[fix] ensure executor is active before starting EventSource (#145)
1 parent a673f3a commit c0af1db

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

android-client-sdk/src/main/java/com/devcycle/sdk/android/api/DVCClient.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import org.json.JSONObject
1919
import java.lang.ref.WeakReference
2020
import java.net.URI
2121
import java.util.concurrent.ConcurrentLinkedQueue
22+
import java.util.concurrent.ExecutorService
23+
import java.util.concurrent.Executors
2224
import java.util.concurrent.atomic.AtomicBoolean
2325
import kotlin.coroutines.CoroutineContext
2426

@@ -42,6 +44,7 @@ class DVCClient private constructor(
4244
) {
4345
private var config: BucketedUserConfig? = null
4446
private var eventSource: EventSource? = null
47+
private var executorService: ExecutorService? = null
4548
private val defaultIntervalInMs: Long = 10000
4649
private val flushInMs: Long = options?.flushEventsIntervalMs ?: defaultIntervalInMs
4750
private val dvcSharedPrefs: DVCSharedPrefs = DVCSharedPrefs(context)
@@ -151,10 +154,17 @@ class DVCClient private constructor(
151154
if (type == "refetchConfig" || type == "") { // Refetch the config if theres no type
152155
refetchConfig(true, lastModified, etag)
153156
}
154-
}), URI(config?.sse?.url)).build()
157+
}), URI(config?.sse?.url)).executor(this.getValidExecutorService()).build()
155158
eventSource?.start()
156159
}
157160

161+
private fun getValidExecutorService(): ExecutorService? {
162+
if (executorService == null || executorService?.isTerminated == true) {
163+
executorService = Executors.newSingleThreadExecutor()
164+
}
165+
return executorService
166+
}
167+
158168
fun onInitialized(callback: DVCCallback<String>) {
159169
if (isInitialized.get()) {
160170
callback.onSuccess("Config loaded")

android-client-sdk/src/main/java/com/devcycle/sdk/android/eventsource/EventSource.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ public static final class Builder {
570570
private int maxEventTasksInFlight = 0;
571571
private boolean streamEventData;
572572
private Set<String> expectFields = null;
573+
private ExecutorService streamExecutor;
573574

574575
/**
575576
* Creates a new builder.
@@ -1012,6 +1013,11 @@ public Builder expectFields(String... fieldNames) {
10121013
return this;
10131014
}
10141015

1016+
public Builder executor(ExecutorService streamExecutor) {
1017+
this.streamExecutor = streamExecutor;
1018+
return this;
1019+
}
1020+
10151021
/**
10161022
* Constructs an {@link EventSource} using the builder's current properties.
10171023
* @return the new EventSource instance

0 commit comments

Comments
 (0)