Skip to content

Commit

Permalink
fix: Improve locking mechanism
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
  • Loading branch information
marcelklehr committed May 18, 2024
1 parent df20961 commit 755c83c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/lib/adapters/Git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,16 @@ export default class GitAdapter extends CachingAdapter {
throw new SlashError()
}

if (this.lockingInterval) {
clearInterval(this.lockingInterval)
}
if (needLock) {
await this.obtainLock()
this.lockingInterval = setInterval(() => this.setLock(), LOCK_INTERVAL) // Set lock every minute
}

const status = await this.pullFromServer()

this.lockingInterval = setInterval(() => this.setLock(), LOCK_INTERVAL) // Set lock every minute

this.initialTreeHash = await this.bookmarksCache.hash(true)

Logger.log('onSyncStart: completed')
Expand Down
7 changes: 6 additions & 1 deletion src/lib/adapters/GoogleDrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,12 @@ export default class GoogleDriveAdapter extends CachingAdapter {
})

this.bookmarksCache = XbelSerializer.deserialize(xmlDocText)
this.lockingInterval = setInterval(() => this.setLock(this.fileId), LOCK_INTERVAL) // Set lock every minute
if (this.lockingInterval) {
clearInterval(this.lockingInterval)
}
if (needLock) {
this.lockingInterval = setInterval(() => this.setLock(this.fileId), LOCK_INTERVAL) // Set lock every minute
}
} else {
this.resetCache()
this.alwaysUpload = true
Expand Down
9 changes: 7 additions & 2 deletions src/lib/adapters/NextcloudBookmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface NextcloudBookmarksConfig {
includeCredentials?: boolean
allowRedirects?: boolean
allowNetwork?: boolean
label?: string
}

interface IChildFolder {
Expand Down Expand Up @@ -101,7 +102,7 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes

getLabel():string {
const data = this.getData()
return data.username.includes('@') ? data.username + ' on ' + new URL(data.url).hostname : data.username + '@' + new URL(data.url).hostname
return data.label || data.username.includes('@') ? data.username + ' on ' + new URL(data.url).hostname : data.username + '@' + new URL(data.url).hostname
}

acceptsBookmark(bm: Bookmark):boolean {
Expand Down Expand Up @@ -137,15 +138,19 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
}
}

if (this.lockingInterval) {
clearInterval(this.lockingInterval)
}
if (needLock) {
if (!(await this.acquireLock())) {
throw new ResourceLockedError()
}
this.lockingInterval = setInterval(() => !this.ended && this.acquireLock(), LOCK_INTERVAL)
}

this.canceled = false
this.ended = false
this.lockingInterval = setInterval(() => !this.ended && this.acquireLock(), LOCK_INTERVAL)

}

async onSyncComplete(): Promise<void> {
Expand Down
19 changes: 15 additions & 4 deletions src/lib/adapters/WebDav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default class WebDavAdapter extends CachingAdapter {
if (res.headers['Last-Modified']) {
const date = new Date(res.headers['Last-Modified'])
const dateLocked = date.valueOf()
if (dateLocked > Date.now() - LOCK_TIMEOUT) {
if (dateLocked + LOCK_TIMEOUT > Date.now()) {
throw new ResourceLockedError()
}
} else {
Expand Down Expand Up @@ -120,7 +120,15 @@ export default class WebDavAdapter extends CachingAdapter {
'text/html',
'<html><body>I am a lock file</body></html>'
)
await this.lockingPromise
try {
await this.lockingPromise
} catch (e) {
if (e instanceof HttpError && e.status === 423) {
this.locked = false
throw new ResourceLockedError()
}
throw e
}
this.locked = true
}

Expand Down Expand Up @@ -253,8 +261,13 @@ export default class WebDavAdapter extends CachingAdapter {
throw new SlashError()
}

if (this.lockingInterval) {
clearInterval(this.lockingInterval)
}
if (needLock) {
await this.obtainLock()
this.lockingInterval = setInterval(() => this.setLock(), LOCK_INTERVAL) // Set lock every minute

}

const resp = await this.pullFromServer()
Expand All @@ -265,8 +278,6 @@ export default class WebDavAdapter extends CachingAdapter {
}
}

this.lockingInterval = setInterval(() => this.setLock(), LOCK_INTERVAL) // Set lock every minute

this.initialTreeHash = await this.bookmarksCache.hash(true)

Logger.log('onSyncStart: completed')
Expand Down

0 comments on commit 755c83c

Please sign in to comment.