Skip to content

Commit 6f77812

Browse files
authored
Improve timeout handling in IPFSBlockStorage methods (#4)
1 parent dad5fe7 commit 6f77812

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/storage/ipfs-block.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ const IPFSBlockStorage = async ({ ipfs, pin, timeout } = {}) => {
3838
*/
3939
const put = async (hash, data) => {
4040
const cid = CID.parse(hash, base58btc)
41-
const { signal } = new TimeoutController(timeout || DefaultTimeout)
42-
await ipfs.blockstore.put(cid, data, { signal })
41+
const timeoutController = new TimeoutController(timeout || DefaultTimeout)
42+
try {
43+
await ipfs.blockstore.put(cid, data, { signal: timeoutController.signal })
44+
} finally {
45+
timeoutController.clear()
46+
}
4347

4448
if (pin && !(await ipfs.pins.isPinned(cid))) {
4549
await drain(ipfs.pins.add(cid))
@@ -58,10 +62,14 @@ const IPFSBlockStorage = async ({ ipfs, pin, timeout } = {}) => {
5862
*/
5963
const get = async (hash) => {
6064
const cid = CID.parse(hash, base58btc)
61-
const { signal } = new TimeoutController(timeout || DefaultTimeout)
62-
const block = await ipfs.blockstore.get(cid, { signal })
63-
if (block) {
64-
return block
65+
const timeoutController = new TimeoutController(timeout || DefaultTimeout)
66+
try {
67+
const block = await ipfs.blockstore.get(cid, { signal: timeoutController.signal })
68+
if (block) {
69+
return block
70+
}
71+
} finally {
72+
timeoutController.clear()
6573
}
6674
}
6775

0 commit comments

Comments
 (0)