autobee 2.7.4 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +14 -2
  2. package/lib/writers.js +32 -9
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -69,7 +69,15 @@ module.exports = class Autobee extends ReadyResource {
69
69
  this.id = null
70
70
  this.bootstrap = null
71
71
  this._handlers = handlers
72
- this.stats = { undos: 0, fastForwards: 0, drains: 0, applies: 0, appends: 0 }
72
+ this.stats = {
73
+ undos: 0,
74
+ fastForwards: 0,
75
+ drains: 0,
76
+ applies: 0,
77
+ appends: 0,
78
+ writersOpened: 0,
79
+ writersClosed: 0
80
+ }
73
81
 
74
82
  const systemStore = this.store.session()
75
83
  this.system = new System(this, systemStore, {
@@ -205,9 +213,13 @@ module.exports = class Autobee extends ReadyResource {
205
213
  if (this._acking) this.bumpSoon()
206
214
  }
207
215
 
216
+ isLocalTrusted() {
217
+ return this.trusted.isTrusted(this.local.key, this._workingView.view)
218
+ }
219
+
208
220
  async _updateAcking() {
209
221
  if (this._interrupting) return
210
- this.setAcking(await this.trusted.isTrusted(this.local.key, this._workingView.view))
222
+ this.setAcking(await this.isLocalTrusted())
211
223
  }
212
224
 
213
225
  // network free: only a migration needs peers, so ready() awaits the full
package/lib/writers.js CHANGED
@@ -10,6 +10,7 @@ const WRITER_PREFETCH = 10
10
10
 
11
11
  const BATCH_WRITER_SANITY = 1024
12
12
  const BATCH_OPTIMISTIC_SANITY = 32
13
+ const DEFAULT_MAX_UNTRUSTED_WRITERS = 128
13
14
 
14
15
  const MAX_FORWARD_TIME_DRIFT = 2 * 3600 * 1000
15
16
 
@@ -24,6 +25,11 @@ class ActiveWriters {
24
25
  this.writable = false
25
26
  this.pending = []
26
27
 
28
+ this.maxUntrustedWriters =
29
+ auto._handlers.maxUntrustedWriters === undefined
30
+ ? DEFAULT_MAX_UNTRUSTED_WRITERS
31
+ : auto._handlers.maxUntrustedWriters || Infinity
32
+
27
33
  this._pendingBuffer = new Map()
28
34
  this._adding = new Map()
29
35
  this._lock = new ReadWriteLock()
@@ -250,6 +256,14 @@ class ActiveWriters {
250
256
  let w = this.active.get(id)
251
257
  if (w) return w
252
258
 
259
+ // sanity limit while untrusted, a dropped writer is requested again by the
260
+ // links waiting on it or by peers announcing it once the round has gc'd
261
+ if (this.active.size >= this.maxUntrustedWriters && !(await this.auto.isLocalTrusted())) {
262
+ return null
263
+ }
264
+
265
+ this.auto.stats.writersOpened++
266
+
253
267
  const local = b4a.equals(key, this.auto.local.key)
254
268
  const encryption = this.auto.encryptionKey ? new WriterEncryption(this.auto) : null
255
269
 
@@ -446,6 +460,7 @@ class Writer {
446
460
  }
447
461
 
448
462
  close() {
463
+ if (!this.isClosed && this !== this.writers.localWriter) this.writers.auto.stats.writersClosed++
449
464
  this.isClosed = true
450
465
 
451
466
  if (this.waiting) {
@@ -677,25 +692,33 @@ class Writer {
677
692
 
678
693
  const has = await Promise.all(promises)
679
694
 
695
+ let missing = false
696
+
680
697
  for (let i = 0; i < links.length; i++) {
681
698
  if (has[i]) continue
682
699
  const link = links[i]
683
- const id = b4a.toString(link.key, 'hex')
684
700
 
685
- this.waiting = this.writers.triggers.add(id, link.length, this)
701
+ // one trigger per writer, wait on the first missing link
702
+ if (!missing) {
703
+ const id = b4a.toString(link.key, 'hex')
686
704
 
687
- // check it didn't arrive underneath us
688
- if (await this.system.has(link)) {
689
- this.writers.triggers.remove(this.waiting)
690
- this.waiting = null
691
- continue
705
+ this.waiting = this.writers.triggers.add(id, link.length, this)
706
+
707
+ // check it didn't arrive underneath us
708
+ if (await this.system.has(link)) {
709
+ this.writers.triggers.remove(this.waiting)
710
+ this.waiting = null
711
+ continue
712
+ }
713
+
714
+ missing = true
692
715
  }
693
716
 
717
+ // but wake up every missing link so they fetch in parallel
694
718
  this.writers.wakeup(link.key, link.length).catch(noop)
695
- return false
696
719
  }
697
720
 
698
- return true
721
+ return !missing
699
722
  }
700
723
 
701
724
  get appending() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autobee",
3
- "version": "2.7.4",
3
+ "version": "2.8.0",
4
4
  "description": "Bee based autobase",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Holepunch Inc.",