-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
214 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { difference } from 'lodash-es'; | ||
|
||
import type { BlobStorage } from '../../storage'; | ||
import { MANUALLY_STOP, throwIfAborted } from '../../utils/throw-if-aborted'; | ||
|
||
export class BlobSyncEngine { | ||
constructor( | ||
readonly local: BlobStorage, | ||
readonly remotes: BlobStorage[] | ||
) {} | ||
|
||
private async sync(signal?: AbortSignal) { | ||
throwIfAborted(signal); | ||
|
||
for (const remote of this.remotes) { | ||
let localList: string[] = []; | ||
let remoteList: string[] = []; | ||
|
||
try { | ||
localList = (await this.local.list(signal)).map(b => b.key); | ||
throwIfAborted(signal); | ||
remoteList = (await remote.list(signal)).map(b => b.key); | ||
throwIfAborted(signal); | ||
} catch (err) { | ||
if (err === MANUALLY_STOP) { | ||
throw err; | ||
} | ||
console.error(`error when sync`, err); | ||
continue; | ||
} | ||
|
||
const needUpload = difference(localList, remoteList); | ||
for (const key of needUpload) { | ||
try { | ||
const data = await this.local.get(key, signal); | ||
throwIfAborted(signal); | ||
if (data) { | ||
await remote.set(data, signal); | ||
throwIfAborted(signal); | ||
} | ||
} catch (err) { | ||
if (err === MANUALLY_STOP) { | ||
throw err; | ||
} | ||
console.error( | ||
`error when sync ${key} from [${this.local.peer}] to [${remote.peer}]`, | ||
err | ||
); | ||
} | ||
} | ||
|
||
const needDownload = difference(remoteList, localList); | ||
|
||
for (const key of needDownload) { | ||
try { | ||
const data = await remote.get(key, signal); | ||
throwIfAborted(signal); | ||
if (data) { | ||
await this.local.set(data, signal); | ||
throwIfAborted(signal); | ||
} | ||
} catch (err) { | ||
if (err === MANUALLY_STOP) { | ||
throw err; | ||
} | ||
console.error( | ||
`error when sync ${key} from [${remote.peer}] to [${this.local.peer}]`, | ||
err | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
async run(signal?: AbortSignal) { | ||
if (signal?.aborted) { | ||
return; | ||
} | ||
|
||
try { | ||
await this.sync(signal); | ||
} catch (error) { | ||
if (error === MANUALLY_STOP) { | ||
return; | ||
} | ||
console.error('sync blob error', error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters