@@ -9,11 +9,7 @@ import com.segment.analytics.kotlin.core.platform.plugins.SegmentDestination
9
9
import com.segment.analytics.kotlin.core.platform.plugins.StartupQueue
10
10
import com.segment.analytics.kotlin.core.platform.plugins.logger.SegmentLog
11
11
import com.segment.analytics.kotlin.core.platform.plugins.logger.log
12
- import kotlinx.coroutines.CoroutineScope
13
- import kotlinx.coroutines.SupervisorJob
14
- import kotlinx.coroutines.asCoroutineDispatcher
15
- import kotlinx.coroutines.launch
16
- import kotlinx.coroutines.runBlocking
12
+ import kotlinx.coroutines.*
17
13
import kotlinx.serialization.DeserializationStrategy
18
14
import kotlinx.serialization.SerializationStrategy
19
15
import kotlinx.serialization.json.Json
@@ -83,11 +79,11 @@ open class Analytics protected constructor(
83
79
object : CoroutineConfiguration {
84
80
override val store = Store ()
85
81
override val analyticsScope = CoroutineScope (SupervisorJob ())
86
- override val analyticsDispatcher =
82
+ override val analyticsDispatcher : CloseableCoroutineDispatcher =
87
83
Executors .newCachedThreadPool().asCoroutineDispatcher()
88
- override val networkIODispatcher =
84
+ override val networkIODispatcher : CloseableCoroutineDispatcher =
89
85
Executors .newSingleThreadExecutor().asCoroutineDispatcher()
90
- override val fileIODispatcher =
86
+ override val fileIODispatcher : CloseableCoroutineDispatcher =
91
87
Executors .newFixedThreadPool(2 ).asCoroutineDispatcher()
92
88
})
93
89
@@ -535,6 +531,23 @@ open class Analytics protected constructor(
535
531
}
536
532
}
537
533
534
+ /* *
535
+ * Shuts down the library by freeing up resources includes queues and Threads. This is a
536
+ * non-reversible operation. This instance of Analytics will be shutdown and no longer process
537
+ * events.
538
+ *
539
+ * Should only be called in containerized environments where you need to free resources like
540
+ * CoroutineDispatchers and ExecutorService instances so they allow the container to shutdown
541
+ * properly.
542
+ */
543
+ fun shutdown () {
544
+ (analyticsDispatcher as CloseableCoroutineDispatcher ).close()
545
+ (networkIODispatcher as CloseableCoroutineDispatcher ).close()
546
+ (fileIODispatcher as CloseableCoroutineDispatcher ).close()
547
+
548
+ store.shutdown();
549
+ }
550
+
538
551
/* *
539
552
* Retrieve the userId registered by a previous `identify` call in a blocking way.
540
553
* Note: this method invokes `runBlocking` internal, it's not recommended to be used
0 commit comments