Skip to content

Commit

Permalink
Pause on full storage (#474)
Browse files Browse the repository at this point in the history
* Pause downloading on write error

* Use hypercore-errors ^1.1.1
  • Loading branch information
HDegroote authored Jan 22, 2024
1 parent 9d2ef9c commit 204f9d3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/block-store.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const b4a = require('b4a')
const { WRITE_FAILED } = require('hypercore-errors')

module.exports = class BlockStore {
constructor (storage, tree) {
Expand Down Expand Up @@ -54,7 +55,7 @@ module.exports = class BlockStore {
_write (offset, data) {
return new Promise((resolve, reject) => {
this.storage.write(offset, data, (err) => {
if (err) reject(err)
if (err) reject(WRITE_FAILED(err.message))
else resolve(offset + data.byteLength)
})
})
Expand Down
4 changes: 2 additions & 2 deletions lib/oplog.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const cenc = require('compact-encoding')
const b4a = require('b4a')
const { crc32 } = require('crc-universal')
const { OPLOG_CORRUPT, OPLOG_HEADER_OVERFLOW } = require('hypercore-errors')
const { OPLOG_CORRUPT, OPLOG_HEADER_OVERFLOW, WRITE_FAILED } = require('hypercore-errors')

module.exports = class Oplog {
constructor (storage, { pageSize = 4096, headerEncoding = cenc.raw, entryEncoding = cenc.raw, readonly = false } = {}) {
Expand Down Expand Up @@ -216,7 +216,7 @@ module.exports = class Oplog {
_append (buf, count) {
return new Promise((resolve, reject) => {
this.storage.write(this._entryOffset + this.byteLength, buf, err => {
if (err) return reject(err)
if (err) return reject(WRITE_FAILED(err.message))

this.byteLength += buf.byteLength
this.length += count
Expand Down
8 changes: 8 additions & 0 deletions lib/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,14 @@ class Peer {
}
} catch (err) {
safetyCatch(err)

if (err.code === 'WRITE_FAILED') {
// For example, we don't want to keep pulling data when storage is full
// TODO: notify the user somehow
this.paused = true
return
}

if (this.core.closed && !isCriticalError(err)) return

if (err.code !== 'INVALID_OPERATION') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"fast-fifo": "^1.3.0",
"flat-tree": "^1.9.0",
"hypercore-crypto": "^3.2.1",
"hypercore-errors": "^1.1.0",
"hypercore-errors": "^1.1.1",
"hypercore-id-encoding": "^1.2.0",
"hypertrace": "^1.2.1",
"is-options": "^1.0.1",
Expand Down

0 comments on commit 204f9d3

Please sign in to comment.