Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Add a lockfile to prevent corruption
Browse files Browse the repository at this point in the history
  • Loading branch information
mjtognetti committed Nov 22, 2019
1 parent 05055cc commit 1aa38b1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
3 changes: 2 additions & 1 deletion dist/RepoBackend.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion dist/RepoBackend.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/RepoBackend.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hypermerge",
"version": "2.0.0-beta.23",
"version": "2.0.0-beta.25",
"description": "Node.js library for building p2p collaborative applications without server infrastructure",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -39,6 +39,7 @@
"js-sha1": "^0.6.0",
"mime-types": "^2.1.24",
"noise-peer": "^1.1.0",
"proper-lockfile": "^4.1.1",
"pump": "^3.0.0",
"random-access-file": "^2.1.3",
"random-access-memory": "^3.0.0",
Expand All @@ -52,6 +53,7 @@
"@types/debug": "^0.0.31",
"@types/mime-types": "^2.1.0",
"@types/node": "^12.12.6",
"@types/proper-lockfile": "^4.1.1",
"@types/pump": "^1.1.0",
"@types/tape": "^4.2.32",
"@types/uuid": "^3.4.5",
Expand Down
8 changes: 7 additions & 1 deletion src/RepoBackend.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import lockfile from 'proper-lockfile'
import Queue from './Queue'
import { Metadata, PublicMetadata } from './Metadata'
import { Actor, ActorMsg } from './Actor'
Expand Down Expand Up @@ -79,6 +80,7 @@ export class RepoBackend {
network: Network
messages: MessageRouter<PeerMsg>
replication: ReplicationManager
lockRelease?: () => void
swarmKey: Buffer // TODO: Remove this once we no longer use discovery-swarm/discovery-cloud
private db: SqlDatabase.Database
private fileServer: FileServer
Expand All @@ -90,6 +92,8 @@ export class RepoBackend {
// initialize storage
if (!opts.memory) {
ensureDirectoryExists(this.path)
// Attempt to acquite a lock on the repo to prevent concurrent corrupting access.
this.lockRelease = lockfile.lockSync(this.path)
}
this.storage = opts.memory ? ram : raf
this.db = SqlDatabase.open(path.resolve(this.path, 'hypermerge.db'), opts.memory || false)
Expand Down Expand Up @@ -245,7 +249,9 @@ export class RepoBackend {
this.replication.close(),
this.network.close(),
this.fileServer.close(),
])
]).then(() => {
this.lockRelease?.()
})
}

private async allReadyActors(docId: DocId): Promise<Actor[]> {
Expand Down
33 changes: 32 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,25 @@
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA==

"@types/proper-lockfile@^4.1.1":
version "4.1.1"
resolved "https://registry.yarnpkg.com/@types/proper-lockfile/-/proper-lockfile-4.1.1.tgz#99f026cbfdbe6305bdd454ffd5fefc1bd064beb1"
integrity sha512-HAjVfDa73pFgivViHyDu8HHHcds+W4MgOuZZAdyFJrHS8ngtCXmhl4hc2YXqSOwO6Bsa+iF2Sgxb2+gv874VOQ==
dependencies:
"@types/retry" "*"

"@types/pump@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@types/pump/-/pump-1.1.0.tgz#ed5214af511da32b6ee85c8d33ad3d59bb79ad8f"
integrity sha512-YGGbsqf5o7sF8gGANP8ZYxgaRGlFgEAImx5tCvA4YKRCfqbsDQZO48UmWynZzSjbhn0ZWSlsWOcb5NwvOx8KcQ==
dependencies:
"@types/node" "*"

"@types/retry@*":
version "0.12.0"
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d"
integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==

"@types/tape@^4.2.32":
version "4.2.32"
resolved "https://registry.yarnpkg.com/@types/tape/-/tape-4.2.32.tgz#1188330d22c4e822648c344faa070277737982d9"
Expand Down Expand Up @@ -636,6 +648,11 @@ glob@^7.0.5, glob@~7.1.4:
once "^1.3.0"
path-is-absolute "^1.0.0"

graceful-fs@^4.1.11:
version "4.2.3"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423"
integrity sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ==

has-flag@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-2.0.0.tgz#e8207af1cc7b30d446cc70b734b5e8be18f88d51"
Expand Down Expand Up @@ -1258,6 +1275,15 @@ process-nextick-args@~2.0.0:
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"
integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==

proper-lockfile@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/proper-lockfile/-/proper-lockfile-4.1.1.tgz#284cf9db9e30a90e647afad69deb7cb06881262c"
integrity sha512-1w6rxXodisVpn7QYvLk706mzprPTAPCYAqxMvctmPN3ekuRk/kuGkGc82pangZiAt4R3lwSuUzheTTn0/Yb7Zg==
dependencies:
graceful-fs "^4.1.11"
retry "^0.12.0"
signal-exit "^3.0.2"

protocol-buffers-encodings@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/protocol-buffers-encodings/-/protocol-buffers-encodings-1.1.0.tgz#f3905631106669b85381bad47a336add7d206873"
Expand Down Expand Up @@ -1401,6 +1427,11 @@ resumer@~0.0.0:
dependencies:
through "~2.3.4"

retry@^0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/retry/-/retry-0.12.0.tgz#1b42a6266a21f07421d1b0b54b7dc167b01c013b"
integrity sha1-G0KmJmoh8HQh0bC1S33BZ7AcATs=

run-node@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/run-node/-/run-node-1.0.0.tgz#46b50b946a2aa2d4947ae1d886e9856fd9cabe5e"
Expand Down Expand Up @@ -1468,7 +1499,7 @@ shuffled-priority-queue@^2.1.0:
dependencies:
unordered-set "^2.0.1"

signal-exit@^3.0.0:
signal-exit@^3.0.0, signal-exit@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
integrity sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=
Expand Down

0 comments on commit 1aa38b1

Please sign in to comment.