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

feat(network): NET-1356 browser webrtc send buffering #2756

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

juslesan
Copy link
Contributor

@juslesan juslesan commented Sep 17, 2024

Summary

Added additional buffering logic to the browser webrtc connections. If the connection buffer fills up messages are dropped. This will alleviate the issue by allowing more messages to be buffered.

Copy link

linear bot commented Sep 17, 2024

@github-actions github-actions bot added the dht Related to DHT package label Sep 17, 2024
@juslesan juslesan marked this pull request as ready for review September 26, 2024 13:15
Copy link
Collaborator

@ptesavol ptesavol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a browser test should be added to see how the system actually behaves (Logging when different things actually happen, if they do + throughput benchmark). Also see the comments.

this.running = true
let sendAttempts = 0;
(async () => {
while (this.queue.length > 0 && this.running) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the bufferedAmout really get smaller in the same tick on browser? I know in node-datachannel it does because there is a worker thread emptying the queue, but is the situation the same in the browser?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely not smaller but it seems that it does get larger. We do have a listener for the onBufferedAmountLow event

this.queue.unshift(messageToSend)
}
sendAttempts++
if (sendAttempts % 200 === 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't 200 attempts quite a lot?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really a reattempt or anything, it is simply a counter used for CPU breaking

}
sendAttempts++
if (sendAttempts % 200 === 0) {
await wait(0) // give up CPU cycle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a shorter pause should be used here see https://stackoverflow.com/questions/18826570/settimeout0-vs-window-postmessage-vs-messageport-postmessage on how to implement one. (I have tried this in the past, and the send buffer does get drained also during the shorter pauses)

if (this.lastState === 'connected') {
this.dataChannel?.send(data as Buffer)
if (this.dataChannel!.bufferedAmount > 1 * 1000 * 1000) {
this.messageQueue.pause()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit dangerous, doSend() is not an arrow function, so it is not guarantedd that "this" points to the right object. Please make doSend() an arrow function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line 96 the doSend function is bound to the message queue using an arrow function. No need to make the class method itself in to an arrow function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dht Related to DHT package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants