@@ -9,11 +9,7 @@ import com.segment.analytics.kotlin.core.platform.plugins.SegmentDestination
99import com.segment.analytics.kotlin.core.platform.plugins.StartupQueue
1010import com.segment.analytics.kotlin.core.platform.plugins.logger.SegmentLog
1111import 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.*
1713import kotlinx.serialization.DeserializationStrategy
1814import kotlinx.serialization.SerializationStrategy
1915import kotlinx.serialization.json.Json
@@ -83,11 +79,11 @@ open class Analytics protected constructor(
8379 object : CoroutineConfiguration {
8480 override val store = Store ()
8581 override val analyticsScope = CoroutineScope (SupervisorJob ())
86- override val analyticsDispatcher =
82+ override val analyticsDispatcher : CloseableCoroutineDispatcher =
8783 Executors .newCachedThreadPool().asCoroutineDispatcher()
88- override val networkIODispatcher =
84+ override val networkIODispatcher : CloseableCoroutineDispatcher =
8985 Executors .newSingleThreadExecutor().asCoroutineDispatcher()
90- override val fileIODispatcher =
86+ override val fileIODispatcher : CloseableCoroutineDispatcher =
9187 Executors .newFixedThreadPool(2 ).asCoroutineDispatcher()
9288 })
9389
@@ -535,6 +531,23 @@ open class Analytics protected constructor(
535531 }
536532 }
537533
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+
538551 /* *
539552 * Retrieve the userId registered by a previous `identify` call in a blocking way.
540553 * Note: this method invokes `runBlocking` internal, it's not recommended to be used
0 commit comments