This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: preload mfs root preiodically and preload dag.put
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
- Loading branch information
Showing
9 changed files
with
87 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const mfs = require('ipfs-mfs/core') | ||
|
||
module.exports = self => { | ||
const mfsSelf = Object.assign({}, self) | ||
|
||
// A patched dag API to ensure preload doesn't happen for MFS operations | ||
mfsSelf.dag = Object.assign({}, self.dag, { | ||
put: promisify((node, opts, cb) => { | ||
if (typeof opts === 'function') { | ||
cb = opts | ||
opts = {} | ||
} | ||
|
||
opts = Object.assign({}, opts, { preload: false }) | ||
|
||
return self.dag.put(node, opts, cb) | ||
}) | ||
}) | ||
|
||
return mfs(mfsSelf, mfsSelf._options) | ||
} |
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
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,47 @@ | ||
'use strict' | ||
|
||
const debug = require('debug') | ||
|
||
const log = debug('jsipfs:mfs-preload') | ||
log.error = debug('jsipfs:mfs-preload:error') | ||
|
||
module.exports = (self, options) => { | ||
options = options || {} | ||
options.interval = options.interval || 30 * 1000 | ||
|
||
let rootCid | ||
let timeoutId | ||
|
||
const preloadMfs = () => { | ||
self.files.stat('/', (err, stats) => { | ||
if (err) { | ||
timeoutId = setTimeout(preloadMfs, options.interval) | ||
return log.error('failed to stat MFS root for preload', err) | ||
} | ||
|
||
if (rootCid !== stats.hash) { | ||
log(`preloading updated MFS root ${rootCid} -> ${stats.hash}`) | ||
|
||
self._preload(stats.hash, (err) => { | ||
timeoutId = setTimeout(preloadMfs, options.interval) | ||
if (err) return log.error(`failed to preload MFS root ${stats.hash}`, err) | ||
rootCid = stats.hash | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
return { | ||
start () { | ||
self.files.stat('/', (err, stats) => { | ||
if (err) return log.error('failed to stat MFS root for preload', err) | ||
rootCid = stats.hash | ||
log(`monitoring MFS root ${rootCid}`) | ||
timeoutId = setTimeout(preloadMfs, options.interval) | ||
}) | ||
}, | ||
stop () { | ||
clearTimeout(timeoutId) | ||
} | ||
} | ||
} |