Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket: use FixedQueue instead of Set #3283

Merged
merged 8 commits into from
May 22, 2024
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 70 additions & 43 deletions lib/web/websocket/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@

const { WebsocketFrameSend } = require('./frame')
const { opcodes, sendHints } = require('./constants')
const FixedQueue = require('../../dispatcher/fixed-queue')

/** @type {Uint8Array} */
/** @type {typeof Uint8Array} */
const FastBuffer = Buffer[Symbol.species]

/**
* @typedef {object} SendQueueNode
* @property {Promise<void> | null} promise
* @property {((...args: any[]) => any)} callback
* @property {Buffer | null} frame
*/

class SendQueue {
#queued = new Set()
#size = 0
/**
* @type {FixedQueue | null}
*/
#queue = null
tsctx marked this conversation as resolved.
Show resolved Hide resolved

/**
* @type {boolean}
*/
#running = false

/** @type {import('net').Socket} */
/** @type {import('node:net').Socket} */
#socket

constructor (socket) {
Expand All @@ -19,66 +34,78 @@ class SendQueue {

add (item, cb, hint) {
if (hint !== sendHints.blob) {
const data = clone(item, hint)

if (this.#size === 0) {
this.#dispatch(data, cb, hint)
const frame = createFrame(item, hint)
if (!this.#running) {
// fast-path
this.#socket.write(frame, cb)
} else {
this.#queued.add([data, cb, true, hint])
this.#size++

this.#run()
/** @type {SendQueueNode} */
const node = {
promise: null,
callback: cb,
frame
}
if (this.#queue === null) {
this.#queue = new FixedQueue()
}
tsctx marked this conversation as resolved.
Show resolved Hide resolved
this.#queue.push(node)
}

return
}

const promise = item.arrayBuffer()
const queue = [null, cb, false, hint]
promise.then((ab) => {
queue[0] = clone(ab, hint)
queue[2] = true

this.#run()
})

this.#queued.add(queue)
this.#size++
}

#run () {
for (const queued of this.#queued) {
const [data, cb, done, hint] = queued
/** @type {SendQueueNode} */
const node = {
promise: item.arrayBuffer().then((ab) => {
node.promise = null
node.frame = createFrame(ab, hint)
}),
callback: cb,
frame: null
}

if (!done) return
if (this.#queue === null) {
this.#queue = new FixedQueue()
}
tsctx marked this conversation as resolved.
Show resolved Hide resolved

tsctx marked this conversation as resolved.
Show resolved Hide resolved
this.#queued.delete(queued)
this.#size--
this.#queue.push(node)

this.#dispatch(data, cb, hint)
if (!this.#running) {
this.#run()
}
}

#dispatch (data, cb, hint) {
const frame = new WebsocketFrameSend()
const opcode = hint === sendHints.string ? opcodes.TEXT : opcodes.BINARY

frame.frameData = data
const buffer = frame.createFrame(opcode)

this.#socket.write(buffer, cb)
async #run () {
this.#running = true
/** @type {FixedQueue} */
tsctx marked this conversation as resolved.
Show resolved Hide resolved
const queue = this.#queue
while (!queue.isEmpty()) {
const node = queue.shift()
// wait pending promise
if (node.promise !== null) {
await node.promise
}
// write
this.#socket.write(node.frame, node.callback)
// cleanup
node.callback = node.frame = null
}
this.#running = false
}
}

function clone (data, hint) {
function createFrame (data, hint) {
return new WebsocketFrameSend(toBuffer(data, hint)).createFrame(hint === sendHints.string ? opcodes.TEXT : opcodes.BINARY)
}

function toBuffer (data, hint) {
switch (hint) {
case sendHints.string:
return Buffer.from(data)
case sendHints.arrayBuffer:
case sendHints.blob:
return new FastBuffer(data)
case sendHints.typedArray:
return Buffer.copyBytesFrom(data)
return new FastBuffer(data.buffer, data.byteOffset, data.byteLength)
}
}

Expand Down
Loading