-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
workers: experimental BroadcastChannel #36271
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ll have to think a bit about how this works on the C++ side … this definitely breaks the MessagePort
/MessagePortData
encapsulation in a big way, and probably in a way that can be avoided, and probably with less code than this would currently introduce…
Basically, what I think I’d rather aim for is:
|
@jasnell Fwiw, if you want, I can probably find the time to write a prototype for that suggestion, and you can see if you like it or not :) |
Thanks :-) I've already started working it up (yes it's a holiday here but this is Fun Coding not Work Coding so it's all good :-)...lol). The |
Yeah, I think there’s not much we can do besides fail … but we can do that during sending, right? |
I think so. We can fail in PostMessage if the |
8a74b55
to
08ab8d6
Compare
@addaleax ... ok, completely revised the implementation as discussed. Please take a look. Here, I'm done for the day and ready to go eat some turkey. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I really like where this is going :)
08ab8d6
to
e31c009
Compare
@addaleax @benjamingr ... marking this ready for review now. There's still work that can be done on this to ensure that it's completely up to spec. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, apart from the error code thing :)
e31c009
to
d1c68a2
Compare
@addaleax .. updated! :-) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice :)
d1c68a2
to
5e06220
Compare
Signed-off-by: James M Snell <jasnell@gmail.com>
94d2d6f
to
6066197
Compare
|
||
Closes the `BroadcastChannel` connection. | ||
|
||
### `broadcastChannel.onmessage` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason these are documented as event handlers and not event "message"
etc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because from the documentation I've been able to find on web usage, the onmessage
pattern tends to be more common. We can tweak the documentation later if necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clarify because of the "we can tweak the docs later" comment - a LGTM from me with comments always means "This is fine to land but I have these comments/questions we should probably address at some point".
If I think something isn't ready to land I never LGTM, I typically (like in this case) check the code out, play with it, put a debugger (in test/parallel/test-worker-broadcastchannel.js here, kind of annoying in worker_threads because I do it from ndb and not vscode), add some logs and LGTM.
I typically don't leave style nits because I usually really don't care about those.
There are a few (very minor) nits here that I might follow up with a PR about later if someone ever complains about them namely stuff like onmessage
not being logged when you util.inspect
a BroadcastChannel here but it being logged in Chrome. I honestly think these things aren't worth mentioning in PRs most of the time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JS LGTM
The API itself leaves a lot to be desired but I guess we should follow the DOM's MessagePort so little we can do about it here :]
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #36271 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Landed in 9e446b3 |
This addresses the `TODO` left on my request in 9e446b3. :) Refs: nodejs#36271
This looks like it landed without a CI run after the rebase. Likely related: Windows CI now fails with the test added here. |
I don't have a Windows machine to test on locally, but I've optimistically opened #36353 changing the expected Windows behavior to be the spec-compliant behavior mentioned in the comment. If that passes CI, then I'll propose fast-tracking. |
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #36271 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
PR-URL: #36435 Notable changes: * child_processes: * add AbortSignal support (Benjamin Gruenbaum) (#36308) * deps: * update ICU to 68.1 (Michaël Zasso) (#36187) * events: * support signal in EventTarget (Benjamin Gruenbaum) (#36258) * graduate Event, EventTarget, AbortController (James M Snell) (#35949) * http: * enable call chaining with setHeader() (pooja d.p) (#35924) * module: * add isPreloading indicator (James M Snell) (#36263) * stream: * support abort signal (Benjamin Gruenbaum) (#36061) * add FileHandle support to Read/WriteStream (Momtchil Momtchev) (#35922) * worker: * add experimental BroadcastChannel (James M Snell) (#36271)
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: nodejs#36271 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
PR-URL: #36435 Notable changes: * child_processes: * add AbortSignal support (Benjamin Gruenbaum) (#36308) * deps: * update ICU to 68.1 (Michaël Zasso) (#36187) * events: * support signal in EventTarget (Benjamin Gruenbaum) (#36258) * graduate Event, EventTarget, AbortController (James M Snell) (#35949) * http: * enable call chaining with setHeader() (pooja d.p) (#35924) * module: * add isPreloading indicator (James M Snell) (#36263) * stream: * support abort signal (Benjamin Gruenbaum) (#36061) * add FileHandle support to Read/WriteStream (Momtchil Momtchev) (#35922) * worker: * add experimental BroadcastChannel (James M Snell) (#36271)
PR-URL: #36435 Notable changes: * child_processes: * add AbortSignal support (Benjamin Gruenbaum) (#36308) * deps: * update ICU to 68.1 (Michaël Zasso) (#36187) * events: * support signal in EventTarget (Benjamin Gruenbaum) (#36258) * graduate Event, EventTarget, AbortController (James M Snell) (#35949) * http: * enable call chaining with setHeader() (pooja d.p) (#35924) * module: * add isPreloading indicator (James M Snell) (#36263) * stream: * support abort signal (Benjamin Gruenbaum) (#36061) * add FileHandle support to Read/WriteStream (Momtchil Momtchev) (#35922) * worker: * add experimental BroadcastChannel (James M Snell) (#36271)
This will need a backport to land on v14.x |
Currently very experimental and definitely not finished implementation of
BroadcastChannel
for Node.js.The intent for this is for it to be completely compatible with the browser API. It's not there yet.
For folks who are not familiar with
BroadcastChannel
, it is essentially a one-to-many alternative toMessageChannel
.All
BroadcastChannel
instances that share the same channel name, created in the main thread or worker threads, will receive a copy of the posted message.Message delivery is at-most once, with no retention. When a message is posted, it is synchronously copied and queued into each of the other instances. Those other instances will deliver the message copy asynchronously.
Transferables are not supported with
BroadcastChannel
, so only messages that can be cloned are supported.One use case of
BroadcastChannel
is to signal a group of worker threads that a process is shutting down, without having to send individual signals to each worker:Worker:
Main:
@addaleax: I'd be interested in your opinions on how to improve the C++ part of the implementation.
@benjamingr: I'd be interested in your thoughts on the JavaScript side of the implementation.
This is definitely a work in progress still, motivated by some needs around Piscina.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes