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

Adds callback cache identity #358

Merged
merged 5 commits into from
Sep 15, 2021
Merged

Adds callback cache identity #358

merged 5 commits into from
Sep 15, 2021

Conversation

vegaro
Copy link
Contributor

@vegaro vegaro commented Sep 15, 2021

We are getting some customers calling alias or identify too often. Sometimes with the same parameters.

Something I have noticed while trying to replicate the issue with this code:

for (i in 1..10) {
    if (newUserId != Purchases.sharedInstance.appUserID) {
        Purchases.sharedInstance.loginWith {
            // Do other stuff
        }
    }
}

Is that most of the times the code will enter in the if clause. The reason is that we only update the appUserID when the API call has come back with a result, so the check would most of the times evaluate to the same.

In this PR, I have added the same caching mechanism we currently have for other API calls. It prevents the same calls being executed at the same time with the same parameters. This should prevent apps from hitting our servers with /alias and /identify calls that look the same too often.

@vegaro vegaro requested a review from a team September 15, 2021 04:38
Comment on lines +56 to +61
@get:Synchronized @set:Synchronized
@Volatile var createAliasCallbacks = mutableMapOf<CallbackCacheKey, MutableList<CreateAliasCallback>>()

@get:Synchronized @set:Synchronized
@Volatile var identifyCallbacks = mutableMapOf<CallbackCacheKey, MutableList<IdentifyCallback>>()

Copy link
Member

Choose a reason for hiding this comment

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

it's fine for now, but we really should try to make this a common thing, which we can reuse easily. Perhaps at the HTTPClient level

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same thoughts I had

@vegaro vegaro merged commit cfaca56 into main Sep 15, 2021
@vegaro vegaro deleted the adds-callback-cache-identity branch September 15, 2021 20:12
vegaro added a commit that referenced this pull request Sep 15, 2021
* adds callback cache to identity and createAlias

* add log

* use debuglog instead of Log

* adds a delay
# Conflicts:
#	common/src/main/java/com/revenuecat/purchases/common/Backend.kt
#	common/src/test/java/com/revenuecat/purchases/common/BackendTest.kt
#	strings/src/main/java/com/revenuecat/purchases/strings/NetworkStrings.kt
@@ -280,7 +291,11 @@ class Backend(
onSuccessHandler: () -> Unit,
onErrorHandler: (PurchasesError) -> Unit
) {
enqueue(object : Dispatcher.AsyncCall() {
val cacheKey = listOfNotNull(
appUserID,
Copy link
Contributor

@beylmk beylmk Sep 21, 2021

Choose a reason for hiding this comment

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

looks like tests only check that we don't repeat the call if both are the same. maybe we can add a test to confirm that if EITHER parameter has changed, we WILL repeat the call?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, that's a good catch. Want me to write it or you take care of it?

Copy link
Contributor

Choose a reason for hiding this comment

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

i can!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants