Skip to content

Commit

Permalink
All: Resolves #10407: Added feature flag to disable sync lock support (
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 authored Aug 23, 2024
1 parent c691fed commit b617a84
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/lib/Synchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ 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: 11 additions & 0 deletions packages/lib/models/settings/builtInMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,17 @@ 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.syncAccurateTimestamps': {
// value: false,
Expand Down
29 changes: 29 additions & 0 deletions packages/lib/services/synchronizer/LockHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ export interface Lock {
updatedTime?: number;
}

const nullLock = (): Lock => {
return {
clientId: '',
clientType: LockClientType.Desktop,
type: LockType.None,
id: 'NULL_LOCK',
updatedTime: Date.now(),
};
};

function lockIsActive(lock: Lock, currentDate: Date, lockTtl: number): boolean {
return currentDate.getTime() - lock.updatedTime < lockTtl;
}
Expand Down Expand Up @@ -140,6 +150,7 @@ export default class LockHandler {
private refreshTimers_: RefreshTimers = {};
private autoRefreshInterval_: number = 1000 * 60;
private lockTtl_: number = defaultLockTtl;
private enabled_ = true;

public constructor(api: FileApi, options: LockHandlerOptions = null) {
if (!options) options = {};
Expand All @@ -149,6 +160,14 @@ export default class LockHandler {
if ('autoRefreshInterval' in options) this.autoRefreshInterval_ = options.autoRefreshInterval;
}

public get enabled(): boolean {
return this.enabled_;
}

public set enabled(v: boolean) {
this.enabled_ = v;
}

public get lockTtl(): number {
return this.lockTtl_;
}
Expand Down Expand Up @@ -185,6 +204,8 @@ export default class LockHandler {
}

public async locks(lockType: LockType = null): Promise<Lock[]> {
if (!this.enabled) return [];

if (this.useBuiltInLocks) {
const locks = (await this.api_.listLocks()).items;
return locks;
Expand Down Expand Up @@ -352,6 +373,8 @@ export default class LockHandler {

// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
public startAutoLockRefresh(lock: Lock, errorHandler: Function): string {
if (!this.enabled) return '';

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 @@ -409,6 +432,8 @@ export default class LockHandler {
}

public stopAutoLockRefresh(lock: Lock) {
if (!this.enabled) return;

const handle = this.autoLockRefreshHandle(lock);
if (!this.refreshTimers_[handle]) {
// Should not throw an error because lock may have been cleared in startAutoLockRefresh
Expand All @@ -422,6 +447,8 @@ export default class LockHandler {
}

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

options = {
...defaultAcquireLockOptions(),
...options,
Expand All @@ -437,6 +464,8 @@ export default class LockHandler {
}

public async releaseLock(lockType: LockType, clientType: LockClientType, clientId: string) {
if (!this.enabled) return;

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

0 comments on commit b617a84

Please sign in to comment.