Skip to content

Commit

Permalink
feat: prevent parallel syncs
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuartu committed Nov 7, 2024
1 parent 596a96a commit 0327163
Showing 1 changed file with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ const metadata: StateMetadata<UserStorageControllerState> = {

type ControllerConfig = {
accountSyncing?: {
maxNumberOfAccountsToAdd?: number;
/**
* Callback that fires when account sync adds an account.
* This is used for analytics.
Expand Down Expand Up @@ -274,17 +275,26 @@ export default class UserStorageController extends BaseController<

#accounts = {
// This is replaced with the actual value in the constructor
// We will remove this once the feature will be released
isAccountSyncingEnabled: false,
isAccountSyncingInProgress: false,
addedAccountsCount: 0,
maxNumberOfAccountsToAdd: 0,
canSync: () => {
try {
this.#assertProfileSyncingEnabled();

return (
this.#accounts.isAccountSyncingEnabled && this.#auth.isAuthEnabled()
);
if (this.#accounts.isAccountSyncingInProgress) {
return false;
}

if (!this.#accounts.isAccountSyncingEnabled) {
return false;
}

if (!this.#auth.isAuthEnabled()) {
return false;
}

return true;
} catch {
return false;
}
Expand All @@ -294,8 +304,7 @@ export default class UserStorageController extends BaseController<
'AccountsController:accountAdded',
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async (account) => {
if (this.#accounts.isAccountSyncingInProgress) {
this.#accounts.addedAccountsCount += 1;
if (!this.#accounts.canSync()) {
return;
}

Expand All @@ -307,7 +316,7 @@ export default class UserStorageController extends BaseController<
'AccountsController:accountRenamed',
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async (account) => {
if (this.#accounts.isAccountSyncingInProgress) {
if (!this.#accounts.canSync()) {
return;
}
await this.saveInternalAccountToUserStorage(account);
Expand Down Expand Up @@ -440,6 +449,9 @@ export default class UserStorageController extends BaseController<
env?.isAccountSyncingEnabled,
);

this.#accounts.maxNumberOfAccountsToAdd =
config?.accountSyncing?.maxNumberOfAccountsToAdd ?? 100;

this.getMetaMetricsState = getMetaMetricsState;
this.#keyringController.setupLockedStateSubscriptions();
this.#registerMessageHandlers();
Expand Down Expand Up @@ -819,7 +831,6 @@ export default class UserStorageController extends BaseController<

try {
this.#accounts.isAccountSyncingInProgress = true;
this.#accounts.addedAccountsCount = 0;

const profileId = await this.#auth.getProfileId();

Expand Down Expand Up @@ -849,7 +860,10 @@ export default class UserStorageController extends BaseController<
// so we only add new accounts if the user has more accounts than the internal accounts list
if (!hasMoreInternalAccountsThanUserStorageAccounts) {
const numberOfAccountsToAdd =
userStorageAccountsList.length - internalAccountsList.length;
Math.min(
userStorageAccountsList.length,
this.#accounts.maxNumberOfAccountsToAdd,
) - internalAccountsList.length;

// Create new accounts to match the user storage accounts list

Expand Down

0 comments on commit 0327163

Please sign in to comment.