Skip to content

Commit d40fcce

Browse files
gmaclennanachou11
andauthored
fix: Add file descriptor pool to storage (#123)
* fix: Add file descriptor pool to storage * sanity check - why do tests pass in windows? * Revert "sanity check - why do tests pass in windows?" This reverts commit dec316d. * add explanation of iterator in comment * Update tests/core-manager.js Co-authored-by: Andrew Chou <andrewchou@fastmail.com> --------- Co-authored-by: Andrew Chou <andrewchou@fastmail.com>
1 parent b09ee8b commit d40fcce

6 files changed

Lines changed: 267 additions & 20 deletions

File tree

lib/core-manager/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class CoreManager extends TypedEmitter {
6363
projectKey,
6464
projectSecretKey,
6565
encryptionKeys = {},
66-
storage
66+
storage,
6767
}) {
6868
super()
6969
assert(
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* File descriptor pool for random-access-storage to limit the number of file
3+
* descriptors used. Important particularly for Android where the hard limit for
4+
* the app is 1024.
5+
*/
6+
export class RandomAccessFilePool {
7+
/** @param {number} maxSize max number of file descriptors to use */
8+
constructor (maxSize) {
9+
this.maxSize = maxSize
10+
/** @type {Set<import('random-access-file')>} */
11+
this.active = new Set()
12+
}
13+
14+
/** @param {import('random-access-file')} file */
15+
_onactive (file) {
16+
if (this.active.size >= this.maxSize) {
17+
// suspend least recently inserted this manually iterates in insertion
18+
// order, but only iterates to the first one (least recently inserted)
19+
const toSuspend = this.active[Symbol.iterator]().next().value
20+
toSuspend.suspend()
21+
this.active.delete(toSuspend)
22+
}
23+
this.active.add(file)
24+
}
25+
26+
/** @param {import('random-access-file')} file */
27+
_oninactive (file) {
28+
this.active.delete(file)
29+
}
30+
}

package-lock.json

Lines changed: 155 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@
5454
"eslint-config-prettier": "^8.8.0",
5555
"nanobench": "^3.0.0",
5656
"prettier": "^2.8.8",
57+
"random-access-file": "^4.0.4",
5758
"random-access-memory": "^6.2.0",
5859
"rimraf": "^5.0.0",
60+
"tempy": "^3.1.0",
5961
"ts-proto": "^1.147.1",
6062
"type-fest": "^3.10.0",
6163
"typedoc": "^0.24.6",
@@ -69,7 +71,7 @@
6971
"b4a": "^1.6.3",
7072
"base32.js": "^0.1.0",
7173
"better-sqlite3": "^8.3.0",
72-
"corestore": "^6.5.2",
74+
"corestore": "^6.8.4",
7375
"hypercore": "^10.9.0",
7476
"hypercore-crypto": "^3.3.1",
7577
"hyperdrive": "^11.0.0-alpha.10",

0 commit comments

Comments
 (0)