worker: add connect and setConnectionsListener#53488
Closed
ShogunPanda wants to merge 1 commit intonodejs:mainfrom
Closed
worker: add connect and setConnectionsListener#53488ShogunPanda wants to merge 1 commit intonodejs:mainfrom
ShogunPanda wants to merge 1 commit intonodejs:mainfrom
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds two new API to
worker_threadsthat allow for cross-thread communication viaMessagePorts.A thread can invoke
worker.connectto start a new connection to another thread. This method is blocking. Upon success, the return value is aMessagePortwhich can be used to exchange messages.The core idea is that a thread willing to accept connections from other thread uses
worker.setConnectionsListenerto install a callback that it is invoked with a thread id, a port and (optional) data each time another thread attempts a connection.The listener can return
trueto accept the connection. Any other return value will result in the connection being refused.By default, if a thread has no listener associated, the connection will be refused.
Notable Change Text
A new set of experimental APIs has been added to
worker_threads:connectandsetConnectionsListener. These APIs aim to simplify 1-1 inter-thread communication in Node.jsEvery thread (including the main one) can start a connection to any another thread (including the main one) using the
connectAPI and providing the targetthreadId.If the connection is successful, the call will return a
MessagePortthat can be used for the communication.A thread can opt-in to receive incoming connections by calling the
setConnectionsListenerAPI. The listener will be invoked for each connection attempt and must returntrueto accept the connection. A thread without a connection listener will refused any connection by default.