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

Avoid creating threads when dispatching tasks #1496

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,15 @@ internal class PurchasesFactory(

val eTagManager = ETagManager(context)

val dispatcher = Dispatcher(createDefaultExecutor(), runningIntegrationTests)
val backendDispatcher = Dispatcher(service ?: createDefaultExecutor(), runningIntegrationTests)
val eventsDispatcher = Dispatcher(createEventsExecutor(), runningIntegrationTests)
val dispatcher = Dispatcher(createDefaultExecutor(), runningIntegrationTests = runningIntegrationTests)
val backendDispatcher = Dispatcher(
service ?: createDefaultExecutor(),
runningIntegrationTests = runningIntegrationTests,
)
val eventsDispatcher = Dispatcher(
createEventsExecutor(),
runningIntegrationTests = runningIntegrationTests,
)

var diagnosticsFileHelper: DiagnosticsFileHelper? = null
var diagnosticsTracker: DiagnosticsTracker? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package com.revenuecat.purchases.common

import android.os.Handler
import android.os.Looper
import com.revenuecat.purchases.PurchasesError
import com.revenuecat.purchases.common.networking.HTTPResult
import com.revenuecat.purchases.common.verification.SignatureVerificationException
Expand All @@ -24,6 +26,7 @@ internal enum class Delay(val minDelay: Duration, val maxDelay: Duration) {

internal open class Dispatcher(
private val executorService: ExecutorService,
private val mainHandler: Handler? = Handler(Looper.getMainLooper()),
private val runningIntegrationTests: Boolean = false,
) {
private companion object {
Expand Down Expand Up @@ -59,28 +62,24 @@ internal open class Dispatcher(
) {
synchronized(this.executorService) {
if (!executorService.isShutdown) {
val future = if (delay != Delay.NONE && executorService is ScheduledExecutorService) {
if (delay != Delay.NONE && executorService is ScheduledExecutorService) {
var delayToApply = (delay.minDelay.inWholeMilliseconds..delay.maxDelay.inWholeMilliseconds).random()
if (runningIntegrationTests) {
delayToApply = (delayToApply * INTEGRATION_TEST_DELAY_PERCENTAGE).toLong()
}
executorService.schedule(command, delayToApply, TimeUnit.MILLISECONDS)
} else {
executorService.submit(command)
}

// Exceptions are being swallowed if using execute instead of submit
// Future.get is blocking so we create a Thread
// More info: https://github.com/RevenueCat/purchases-android/pull/234
Thread {
try {
future.get()
} catch (e: InterruptedException) {
Thread.currentThread().interrupt()
} catch (@Suppress("TooGenericExceptionCaught") e: Exception) {
e.cause?.let { throw it }
executorService.submit {
try {
command.run()
} catch (@Suppress("TooGenericExceptionCaught") e: Exception) {
errorLog("Exception running command: $e")
mainHandler?.post {
e.cause?.let { throw it }
}
tonidero marked this conversation as resolved.
Show resolved Hide resolved
}
}
}.start()
}
}
}
}
Expand Down