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 4 commits
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 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,25 @@
) {
synchronized(this.executorService) {
if (!executorService.isShutdown) {
val future = if (delay != Delay.NONE && executorService is ScheduledExecutorService) {
val commandHandlingExceptions = Runnable {
try {
command.run()
} catch (@Suppress("TooGenericExceptionCaught") e: Exception) {
errorLog("Exception running command: $e")

Check warning on line 69 in purchases/src/main/kotlin/com/revenuecat/purchases/common/Dispatcher.kt

View check run for this annotation

Codecov / codecov/patch

purchases/src/main/kotlin/com/revenuecat/purchases/common/Dispatcher.kt#L68-L69

Added lines #L68 - L69 were not covered by tests
mainHandler?.post {
throw e.cause ?: e
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain this? why not just throw e?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not needed, I was mostly copying here what we had previously... In my tests, the cause was sometimes null, so I fallback to the original exception, but we could probably just propagate the original exception

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I am not sure why we were doing e.cause 🤔

}
}
}
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)
executorService.schedule(commandHandlingExceptions, delayToApply, TimeUnit.MILLISECONDS)
} else {
executorService.submit(command)
executorService.submit(commandHandlingExceptions)
}

// 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 }
}
}.start()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.revenuecat.purchases.common.Dispatcher
import io.mockk.mockk
import java.util.concurrent.RejectedExecutionException

internal class SyncDispatcher : Dispatcher(mockk()) {
internal class SyncDispatcher : Dispatcher(mockk(), MockHandlerFactory.createMockHandler()) {

private var closed = false

Expand Down