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

Request: SharedWorkers in Deno #7728

Open
ghost opened this issue Sep 27, 2020 · 8 comments
Open

Request: SharedWorkers in Deno #7728

ghost opened this issue Sep 27, 2020 · 8 comments
Labels
cli related to cli/ dir suggestion suggestions for new features (yet to be agreed)

Comments

@ghost
Copy link

ghost commented Sep 27, 2020

Is it planned that Deno support SharedWorkers and / or ServerWorkers at some point?

I don't see the use for ServiceWorkers in Deno, but SharedWorkers are useful for multi-threaded code that uses normal workers, as they allow access to the same worker via ports.

@kitsonk kitsonk added cli related to cli/ dir suggestion suggestions for new features (yet to be agreed) labels Sep 27, 2020
@rektide
Copy link

rektide commented Sep 28, 2020

Super useful for actor-based systems & data-sharing in general! A worker pretty much is an actor.

Timely ask too. CloudFlare just introduced Durable Objects. There's no durability implied here, but the general architecture, within a process, maps pretty clearly to SharedWorkers, each shared worker being a (process-local) (semi-sort-of-) durable object.

@ghost
Copy link
Author

ghost commented Sep 29, 2020

Super useful for actor-based systems & data-sharing in general! A worker pretty much is an actor.

Timely ask too. CloudFlare just introduced Durable Objects. There's no durability implied here, but the general architecture, within a process, maps pretty clearly to SharedWorkers, each shared worker being a (process-local) (semi-sort-of-) durable object.

I just read up on that article, posted literally today, quite the coincidence, but it sure seems nice.
Those "Durable Objects" don't imply durability either, they seem to offer synchronization and speed instead, which is exactly what I needed this for.
I'm sure that Deno will have to eventually implement SharedWorkers, as multiple major browsers have them, so it's more of a question about when they will do it.

I was writing code that involved multiple workers to separate and parallelize code, there was one specific worker that had permission to access resources that were slow to use, thus why I moved it to a worker in the first place, and it requires synchronization.

Ex: synchronized read / write access to data on the disk. postMessages can come in at any time and fill up the event loop's queue, but the handler will only operate one at a time, you don't want to have multiple workers writing to a file at once.

For what I was doing, SharedArray buffers wouldn't have been useful, nor easy to keep in sync.

Overall though, the key idea was safety, as only one part of the code has exclusive access and sets up its own barrier to safely provide access to something, without sacrificing performance, as all of the work is done outside of the current thread.

The problem arrives when one wants access to those resources from within multiple workers at once. You can't exactly pass a worker into another worker worker[0].postMessage( workers[1] ), and creating another worker just adds bloat and beats the purpose here, so there is currently no viable alternative as Deno lacks SharedWorkers.

@kitsonk
Copy link
Contributor

kitsonk commented Sep 29, 2020

We've been talking about this, even before the CloudFlare announcement or this issue.

We are leaning towards using workers and SharedWorkers certainly is attractive. We would certainly like to have something that exposes a browser standard API, versus the path the CloudFlare appears to be taking, which is unique to that platform.

One of the big ones on this journey though is we need to properly address structured cloning (#3557), as this is the foundation for the ability deal with objects across the worker barrier.

@ghost
Copy link
Author

ghost commented Sep 29, 2020

We've been talking about this, even before the CloudFlare announcement or this issue.
...
One of the big ones on this journey though is we need to properly address structured cloning (#3557), as this is the foundation for the ability deal with objects across the worker barrier.

Oh, I don't doubt it, it falls perfectly within Deno's philosophy, and it just seems awesome all around.
I personally have been eagerly waiting for any improvements on the Worker situation, whether it's that structured cloning is finally implemented or it's that a new type of worker that is added. But in the meanwhile, I can understand that the implementation of structured cloning in Deno will take quite some time, especially with everything else the Deno development team is doing.

@kitsonk
Copy link
Contributor

kitsonk commented Sep 29, 2020

Workers is getting some love in the very near future. In fact I believe @bartlomieju has some plans for it this week.

@bartlomieju
Copy link
Member

@kitsonk I didn't mean workers as in Worker API but our internal workers 😅 structured clone is waiting on denoland/rusty_v8#442

@ghost
Copy link
Author

ghost commented Jul 17, 2021

It's been some time since structured cloning has been working for message passing, and we have functioning MessageChannels! 🎉

Is there anything else that might be blocking a possible implementation?

@bartlomieju
Copy link
Member

Is there anything else that might be blocking a possible implementation?

Not really, however implementation wise it's gonna be quite challenging task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli related to cli/ dir suggestion suggestions for new features (yet to be agreed)
Projects
None yet
Development

No branches or pull requests

3 participants