-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * gogo hotswaps * missing file * comment * fix hans bug * Add metric and test --------- Co-authored-by: HDegroote <75906619+HDegroote@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
184 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const TICKS = 16 | ||
|
||
module.exports = class HotswapQueue { | ||
constructor () { | ||
this.priorities = [[], [], []] | ||
} | ||
|
||
* pick (peer) { | ||
for (let i = 0; i < this.priorities.length; i++) { | ||
// try first one more than second one etc etc | ||
let ticks = (this.priorities.length - i) * TICKS | ||
const queue = this.priorities[i] | ||
|
||
for (let j = 0; j < queue.length; j++) { | ||
const r = j + Math.floor(Math.random() * queue.length - j) | ||
const a = queue[j] | ||
const b = queue[r] | ||
|
||
if (r !== j) { | ||
queue[(b.hotswap.index = j)] = b | ||
queue[(a.hotswap.index = r)] = a | ||
} | ||
|
||
if (hasInflight(b, peer)) continue | ||
|
||
yield b | ||
|
||
if (--ticks <= 0) break | ||
} | ||
} | ||
} | ||
|
||
add (block) { | ||
if (block.hotswap !== null) this.remove(block) | ||
if (block.inflight.length === 0 || block.inflight.length >= 3) return | ||
|
||
// TODO: also use other stuff to determine queue prio | ||
const queue = this.priorities[block.inflight.length - 1] | ||
|
||
const index = queue.push(block) - 1 | ||
block.hotswap = { ref: this, queue, index } | ||
} | ||
|
||
remove (block) { | ||
const hotswap = block.hotswap | ||
if (hotswap === null) return | ||
|
||
block.hotswap = null | ||
const head = hotswap.queue.pop() | ||
if (head === block) return | ||
hotswap.queue[(head.hotswap.index = hotswap.index)] = head | ||
} | ||
} | ||
|
||
function hasInflight (block, peer) { | ||
for (let j = 0; j < block.inflight.length; j++) { | ||
if (block.inflight[j].peer === peer) return true | ||
} | ||
return false | ||
} |
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