Skip to content

Commit

Permalink
Desktop, Cli, Mobile: Remove the need for sync locks (#11377)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 authored Nov 12, 2024
1 parent 3887010 commit 6c0258e
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 189 deletions.
1 change: 0 additions & 1 deletion packages/lib/Synchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ export default class Synchronizer {
public lockHandler() {
if (this.lockHandler_) return this.lockHandler_;
this.lockHandler_ = new LockHandler(this.api());
this.lockHandler_.enabled = Setting.value('featureFlag.syncLockEnabled');
return this.lockHandler_;
}

Expand Down
11 changes: 0 additions & 11 deletions packages/lib/models/settings/builtInMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1610,17 +1610,6 @@ const builtInMetadata = (Setting: typeof SettingType) => {
isGlobal: true,
},

'featureFlag.syncLockEnabled': {
value: true,
type: SettingItemType.Bool,
public: true,
storage: SettingStorage.File,
label: () => 'Enable sync locks',
description: () => 'This is an experimental setting to disable sync locks',
section: 'sync',
isGlobal: true,
},

'featureFlag.linuxKeychain': {
value: false,
type: SettingItemType.Bool,
Expand Down
18 changes: 17 additions & 1 deletion packages/lib/services/synchronizer/LockHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export default class LockHandler {
private refreshTimers_: RefreshTimers = {};
private autoRefreshInterval_: number = 1000 * 60;
private lockTtl_: number = defaultLockTtl;
private enabled_ = true;
private enabled_ = false;

public constructor(api: FileApi, options: LockHandlerOptions = null) {
if (!options) options = {};
Expand Down Expand Up @@ -206,6 +206,8 @@ export default class LockHandler {
public async locks(lockType: LockType = null): Promise<Lock[]> {
if (!this.enabled) return [];

if (this.enabled) throw new Error('Lock handler is enabled');

if (this.useBuiltInLocks) {
const locks = (await this.api_.listLocks()).items;
return locks;
Expand All @@ -227,6 +229,8 @@ export default class LockHandler {
}

private async saveLock(lock: Lock) {
if (!this.enabled) return;
if (this.enabled) throw new Error('Lock handler is enabled');
await this.api_.put(this.lockFilePath(lock), JSON.stringify(lock));
}

Expand Down Expand Up @@ -285,6 +289,10 @@ export default class LockHandler {
}

private async acquireExclusiveLock(clientType: LockClientType, clientId: string, options: AcquireLockOptions = null): Promise<Lock> {
if (!this.enabled) return nullLock();

if (this.enabled) throw new Error('Lock handler is enabled');

if (this.useBuiltInLocks) return this.api_.acquireLock(LockType.Exclusive, clientType, clientId);

// The logic to acquire an exclusive lock, while avoiding race conditions is as follow:
Expand Down Expand Up @@ -375,6 +383,8 @@ export default class LockHandler {
public startAutoLockRefresh(lock: Lock, errorHandler: Function): string {
if (!this.enabled) return '';

if (this.enabled) throw new Error('Lock handler is enabled');

const handle = this.autoLockRefreshHandle(lock);
if (this.refreshTimers_[handle]) {
throw new Error(`There is already a timer refreshing this lock: ${handle}`);
Expand Down Expand Up @@ -434,6 +444,8 @@ export default class LockHandler {
public stopAutoLockRefresh(lock: Lock) {
if (!this.enabled) return;

if (this.enabled) throw new Error('Lock handler is enabled');

const handle = this.autoLockRefreshHandle(lock);
if (!this.refreshTimers_[handle]) {
// Should not throw an error because lock may have been cleared in startAutoLockRefresh
Expand All @@ -449,6 +461,8 @@ export default class LockHandler {
public async acquireLock(lockType: LockType, clientType: LockClientType, clientId: string, options: AcquireLockOptions = null): Promise<Lock> {
if (!this.enabled) return nullLock();

if (this.enabled) throw new Error('Lock handler is enabled');

options = {
...defaultAcquireLockOptions(),
...options,
Expand All @@ -466,6 +480,8 @@ export default class LockHandler {
public async releaseLock(lockType: LockType, clientType: LockClientType, clientId: string) {
if (!this.enabled) return;

if (this.enabled) throw new Error('Lock handler is enabled');

if (this.useBuiltInLocks) {
await this.api_.releaseLock(lockType, clientType, clientId);
return;
Expand Down
Loading

0 comments on commit 6c0258e

Please sign in to comment.