Skip to content

Performance: throttle cache updates, and avoid double updates on app launch#2983

Closed
NachoSoto wants to merge 1 commit into
mainfrom
update-caches-throttle
Closed

Performance: throttle cache updates, and avoid double updates on app launch#2983
NachoSoto wants to merge 1 commit into
mainfrom
update-caches-throttle

Conversation

@NachoSoto

Copy link
Copy Markdown
Contributor

Possibly as a consequence of #2623 we were calling updateAllCachesIfNeeded on app launch together with updateCachesIfInForeground, meaning that we were updating all caches twice.
Thanks to our de-duping logic this is normally not a big issue, but this issue combined with what #2954 fixes, means that if there are pending transactions, opening the app would probably end up posting receipts duplicated times.

This adds a new log as well to detect this:

[purchases] DEBUG: ℹ️ Throttling cache update, only 0.8 seconds elapsed

And a new integration test that posts UIApplication.willEnterForegroundNotification to test this.

@NachoSoto NachoSoto added the perf label Aug 8, 2023
@NachoSoto NachoSoto requested a review from a team August 8, 2023 00:10
@NachoSoto NachoSoto force-pushed the update-caches-throttle branch from 21c7ab1 to 0bab77e Compare August 8, 2023 00:16

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was only 2 because fireNotifications was firing the didEnterForeground and didEnterBackground, and this test was detecting the double update.
Now it's 1 because it only gets updated automatically (regardless of throttling) when entering the background.

@codecov

codecov Bot commented Aug 8, 2023

Copy link
Copy Markdown

Codecov Report

Merging #2983 (0bab77e) into main (7a23d5b) will increase coverage by 0.05%.
The diff coverage is 91.66%.

❗ Current head 0bab77e differs from pull request most recent head cd6213e. Consider uploading reports for the commit cd6213e to get more accurate results

@@            Coverage Diff             @@
##             main    #2983      +/-   ##
==========================================
+ Coverage   86.68%   86.73%   +0.05%     
==========================================
  Files         219      218       -1     
  Lines       15636    15636              
==========================================
+ Hits        13554    13562       +8     
+ Misses       2082     2074       -8     
Files Changed Coverage Δ
Sources/Misc/DateAndTime/TimingUtil.swift 91.66% <ø> (+2.34%) ⬆️
Sources/Misc/DateAndTime/Clock.swift 66.66% <57.14%> (-33.34%) ⬇️
Sources/Logging/Strings/PurchaseStrings.swift 89.65% <100.00%> (-0.31%) ⬇️
Sources/Misc/SystemInfo.swift 98.40% <100.00%> (+4.33%) ⬆️
Sources/Networking/Backend.swift 88.76% <100.00%> (ø)
Sources/Purchasing/Purchases/Purchases.swift 76.65% <100.00%> (+0.83%) ⬆️
Sources/Purchasing/ReceiptFetcher.swift 91.89% <100.00%> (-0.06%) ⬇️

... and 23 files with indirect coverage changes

@joshdholtz joshdholtz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good to me! Just a little thought on naming 🤷‍♂️

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this naming might be a little confusing here 🤔 setLastCacheUpdateTime() makes it seem like this method should be called inside of updateAllCachesIfNeeded() when its completed. Should it some like... setLastCacheFetchedTime() 🤷‍♂️

@tonidero tonidero left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A couple comments but nothing blocking

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is now throttled in the applicationWillEnterForeground but not on the applicationDidEnterBackground... Not a blocker but wanted to mention it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I feel we should extract all the code here to an updateCachesOnAppForeground or something like that, and move this there... Just in case we want to do something else on app foreground and we don't want to skip it. NABD though.

…p launch

Possibly as a consequence of #2623 we were calling `updateAllCachesIfNeeded` on app launch together with `updateCachesIfInForeground`, meaning that we were updating all caches twice.
Thanks to our de-duping logic this is normally not a big issue, but this issue combined with what #2954 fixes, means that if there are pending transactions, opening the app would probably end up posting receipts duplicated times.

This adds a new log as well to detect this:
> [purchases] DEBUG: ℹ️ Throttling cache update, only 0.8 seconds elapsed

And a new integration test that posts `UIApplication.willEnterForegroundNotification` to test this.
@NachoSoto

Copy link
Copy Markdown
Contributor Author

Decided that we don't need this.
Completion-block-based de-dupping + integration tests (testSimultanousCallsToGetCustomerInfoWithPendingTransactionPostsReceiptOnlyOnce / #3013) are enough.

@NachoSoto NachoSoto closed this Aug 14, 2023
@NachoSoto NachoSoto deleted the update-caches-throttle branch August 14, 2023 20:16
NachoSoto added a commit that referenced this pull request Aug 14, 2023
…quest de-dupping

This adds more coverage for why we don't actually need #2983.
NachoSoto added a commit that referenced this pull request Aug 14, 2023
This extracts a useful refactor from #2983: `ClockType` now lives in `SystemInfo`, so types that depend on type can use the same clock, and tests can rely on the mocked `TestClock`.
NachoSoto added a commit that referenced this pull request Aug 14, 2023
…equest de-dupping (#3013)

This adds more coverage for why we don't actually need #2983.
NachoSoto added a commit that referenced this pull request Aug 14, 2023
…undNotification` to simulate a real app

#2983 was an attempt to work around this. Instead, this PR adds integration tests to cover this behavior and ensure that we don't make duplicate requests.
MarkVillacampa pushed a commit that referenced this pull request Sep 6, 2023
…equest de-dupping (#3013)

This adds more coverage for why we don't actually need #2983.
@vegaro vegaro added pr:other and removed pr:perf labels Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants