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

Rework the caching logic for subscription and entitlements #741

Merged
merged 7 commits into from
Mar 22, 2024

Conversation

miasma13
Copy link
Contributor

@miasma13 miasma13 commented Mar 22, 2024

Required:

Task/Issue URL:
iOS PR: duckduckgo/macos-browser#2485
macOS PR: duckduckgo/iOS#2627
What kind of version bump will this require?: Patch

Description:
Change default staleness time interval for subscription and entitlement cache to 20min
When storing subscription in the cache use the date based on default interval or its date of renewalOrExpiry - whatever is earlier
Make the VPN code use cache and its logic for doing fetch if data is stale
On applicationDidBecomeActive refresh subscription details and entitlements
In the app rely on subscription details and entitlements based on cache logic (cached if still valid else fetch)


Internal references:

Software Engineering Expectations
Technical Design Template

Copy link
Member

@federicocappelli federicocappelli left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@quanganhdo quanganhdo left a comment

Choose a reason for hiding this comment

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

Left some thoughts on the implementation. Not necessary blocker for merging, but they're worth pointing out IMO.

entitlementsCache: UserDefaultsCache<[Entitlement]>(subscriptionAppGroup: subscriptionAppGroup, key: UserDefaultsCacheKey.subscriptionEntitlements))
entitlementsCache: UserDefaultsCache<[Entitlement]>(subscriptionAppGroup: subscriptionAppGroup,
key: UserDefaultsCacheKey.subscriptionEntitlements,
settings: UserDefaultsCacheSettings(defaultExpirationInterval: .minutes(20))))
Copy link
Member

Choose a reason for hiding this comment

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

This should be a configurable param as well.

if !subscription.isActive {
signOut()
}
}

_ = await fetchEntitlements(cachePolicy: .reloadIgnoringLocalCacheData)
Copy link
Member

@quanganhdo quanganhdo Mar 22, 2024

Choose a reason for hiding this comment

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

If fetchRemoteEntitlements returns a failure I think we should handle this. Otherwise just blocking the endpoint at router-level would make the refresh meaningless.

@@ -55,7 +56,9 @@ public final class SubscriptionService: APIService {

switch result {
case .success(let subscriptionResponse):
subscriptionCache.set(subscriptionResponse)
let defaultExpiryDate = Date().addingTimeInterval(subscriptionCache.settings.defaultExpirationInterval)
Copy link
Member

Choose a reason for hiding this comment

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

This relies on system time which can be manipulated. If device time is set to a distance future, any entitlement check that depends on the cache would just succeed. While we do use reloadIgnoringLocalCacheData when refreshing periodically, we don't currently handle the failure.

@miasma13 miasma13 merged commit 91d9eb2 into main Mar 22, 2024
7 checks passed
@miasma13 miasma13 deleted the michal/subs-and-ent-caching branch March 22, 2024 19:21
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