• Grisha Kruglov's avatar
    Move sync scope ownership into account manager; API simplification · 7afec7d4
    Grisha Kruglov authored
    I've started pulling on one little thread, and ended up with a few more changes than initially anticipated.
    Raison d'être for this PR - introducing access token caching for Sync.
    - Some background on the issue: Rust FirefoxAccount object maintains an in-memory cache of access tokens, keyed by 'scope'. During every sync, we "rehydrate" an instance of FirefoxAccount, starting with a fresh cache. We then obtain an access token from it to sync; this performs a network request (since the internal cache is empty), which is quite costly at scale for our services. This creates a situation when we may overwhelm our own servers with a large enough, actively syncing user base.
    - This PR adds a caching layer for sync authInfo objects. Sync workers no longer interact with the account directly, and instead look into the cache to obtain authentication info necessary for syncing. No more "talk to the FxA server before every sync".
    Account manager is responsible for keeping the cache up-to-date, and resetting it when necessary. Cache is currently updated: on startup (but only if access token has expired), on authentication, and when we recover from auth problems.
    And this is where the "thread pulling" begins! In order to "own" the access token for sync, account manager needs to be aware of the "sync scope".
    Before, we just relied on the application to specify that scope. Instead, I've changed account manager's constructor to take a SyncConfig object which allows consuming application to configure how sync should behave (enabled at all?, periodic syncing enabled? how often to sync? which stores should be synced?).
    Ownership of the "sync manager" moved down the stack, from the application layer into the account manager.
    Application is now expected to interact with sync only via AccountManager's `sync` method, which exposes an internal SyncManager instance (if sync is enabled).
    Above changes were a good reason to move support classes from feature-sync and into services-firefox-account. Note that since "sync" is part of our "storage" modules, this change doesn't mean that you need to take an extra native dependency on your classpath simply if you need to use FxA. Thanks to concept-sync, actual "Firefox Sync" machinery (within libplaces) is still fully decoupled from FxA. `feature-sync` has been removed entirely.
    Since we're churning the public API anyway, I took the chance to introduce a few more simplifications at the API layer:
    - 'SyncManager' interface was removed, since we're not expecting to have multiple implementations of it
    - 'Config' was renamed to 'ServerConfig'
    - 'DeviceTuple' was renamed to 'DeviceConfig'
    - account manager grew a new public API, 'setSyncConfig', which allows application to re-configure how it wants sync to behave
    - 'AuthInfo' was renamed to 'SyncAuthInfo', and a bunch of cleanup happened in that area
    - 'AccountObservable'@'onError' method was removed. The only error that could have been passed into it (unable to restore account) wasn't actionable by the application anyway, and none of the integrations did anything with that call
    Documentation of public APIs and classes was improved.