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: add ipc wrappers #261

Merged
merged 31 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d8b0996
add test
achou11 Sep 12, 2023
7b129c0
broken initial implementation
achou11 Sep 12, 2023
4be1303
some fixes
gmaclennan Sep 12, 2023
9313f48
fix test
achou11 Sep 13, 2023
077edbf
remove todo comment
achou11 Sep 13, 2023
79252cb
wip: handle server closing
achou11 Sep 13, 2023
9f6a7a5
fixes to SubChannel implementation
achou11 Sep 14, 2023
76e0d63
Merge branch 'main' into 259/ipc-wrapper
achou11 Sep 14, 2023
c3139c6
remove unused import
achou11 Sep 14, 2023
32c5a01
use LRU for project ipc instances
achou11 Sep 14, 2023
e9cdec4
close server side manager channel
achou11 Sep 14, 2023
c7fe50f
whitespace fix
achou11 Sep 14, 2023
e067dc3
use eventemitter3 for SubChannel implementation to work in browser
achou11 Sep 14, 2023
5f23d77
Merge branch 'main' into 259/ipc-wrapper
achou11 Sep 18, 2023
892b653
remove usage of LRU cache
achou11 Sep 18, 2023
71a8398
expose close function for client
achou11 Sep 18, 2023
fd58298
move createProjectClient function
achou11 Sep 18, 2023
612f068
small fix to SubChannel
achou11 Sep 18, 2023
ada1aba
clean up tests
achou11 Sep 18, 2023
77cdf54
use constant
achou11 Sep 18, 2023
b7e98da
handle getProject calls failing on server side
achou11 Sep 18, 2023
687c2bc
add stressish test
achou11 Sep 18, 2023
d8ebfc2
Revert "handle getProject calls failing on server side"
achou11 Sep 19, 2023
be7cb48
fix SubChannel.close
achou11 Sep 19, 2023
d976081
update rpc-reflector
achou11 Sep 19, 2023
1fa204a
revised implementation for handling getProject failure
achou11 Sep 19, 2023
57456b7
improve areas with potential race conditions
achou11 Sep 19, 2023
fc86445
remove unused type
achou11 Sep 19, 2023
5119426
Merge branch 'main' into 259/ipc-wrapper
achou11 Sep 19, 2023
368d11c
package file updates
achou11 Sep 19, 2023
08ae6a8
close mapeo rpc channel on close
achou11 Sep 21, 2023
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
137 changes: 137 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"private-ip": "^3.0.0",
"protobufjs": "^7.2.3",
"protomux": "^3.4.1",
"rpc-reflector": "^1.3.10",
"sodium-universal": "^4.0.0",
"sub-encoder": "^2.1.1",
"tiny-typed-emitter": "^2.1.0"
Expand Down
56 changes: 56 additions & 0 deletions src/ipc-wrapper/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// @ts-check
import { createClient } from 'rpc-reflector'
import { SubChannel } from './sub-channel.js'

/**
* @param {import('rpc-reflector/client.js').MessagePortLike} messagePort
* @returns {import('rpc-reflector/client.js').ClientApi<import('../mapeo-manager.js').MapeoManager>}
*/
export function createMapeoClient(messagePort) {
// TODO: LRU cache?
/** @type {Map<import('../types.js').ProjectPublicId, import('rpc-reflector/client.js').ClientApi<import('../mapeo-project.js').MapeoProject>>} */
const existingProjectClients = new Map()

const managerChannel = new SubChannel(messagePort, '@@manager')

/** @type {import('rpc-reflector').ClientApi<import('../mapeo-manager.js').MapeoManager>} */
const managerClient = createClient(managerChannel)

const client = new Proxy(managerClient, {
get(target, prop, receiver) {
if (prop === 'getProject') {
return createProjectClient
} else {
return Reflect.get(target, prop, receiver)
}

/**
* @param {import('../types.js').ProjectPublicId} projectPublicId
* @returns {Promise<import('rpc-reflector/client.js').ClientApi<import('../mapeo-project.js').MapeoProject>>}
*/
function createProjectClient(projectPublicId) {
Copy link
Member

Choose a reason for hiding this comment

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

Move this out of the scope of get(), into the createMapeoClient() scope.

Copy link
Member Author

Choose a reason for hiding this comment

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

addressed via fd58298

const existingClient = existingProjectClients.get(projectPublicId)

if (existingClient) return Promise.resolve(existingClient)

achou11 marked this conversation as resolved.
Show resolved Hide resolved
const projectChannel = new SubChannel(messagePort, projectPublicId)

/** @type {import('rpc-reflector').ClientApi<import('../mapeo-project.js').MapeoProject>} */
const projectClient = new Proxy(createClient(projectChannel), {
get(target, prop, receiver) {
if (prop === 'then') {
return projectClient
}
return Reflect.get(target, prop, receiver)
},
})

existingProjectClients.set(projectPublicId, projectClient)

return Promise.resolve(projectClient)
}
},
})

return client
}
39 changes: 39 additions & 0 deletions src/ipc-wrapper/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// @ts-check
import { createServer } from 'rpc-reflector'
import { SubChannel } from './sub-channel.js'

/**
* @param {import('../mapeo-manager.js').MapeoManager} manager
* @param {import('rpc-reflector/server.js').MessagePortLike} messagePort
achou11 marked this conversation as resolved.
Show resolved Hide resolved
*/
export function createMapeoServer(manager, messagePort) {
// TODO: LRU? project.close() after time without use?
/** @type {Map<string, { close: () => void, project: import('../mapeo-project.js').MapeoProject }>}*/
const existing = new Map()

const managerChannel = new SubChannel(messagePort, '@@manager')

const managerServer = createServer(manager, managerChannel)

messagePort.on('message', async (payload) => {
// TODO: figure out better way to know that this is the project public id
const id = payload?.id
if (typeof id !== 'string' || id === '@@manager') return
achou11 marked this conversation as resolved.
Show resolved Hide resolved

if (existing.has(id)) return

const projectChannel = new SubChannel(messagePort, id)

const project = await manager.getProject(
gmaclennan marked this conversation as resolved.
Show resolved Hide resolved
/** @type {import('../types.js').ProjectPublicId} */ (id)
)
achou11 marked this conversation as resolved.
Show resolved Hide resolved

const { close } = createServer(project, projectChannel)

achou11 marked this conversation as resolved.
Show resolved Hide resolved
projectChannel.emit('message', payload.message)

existing.set(id, { close, project })
})

return managerServer
}
40 changes: 40 additions & 0 deletions src/ipc-wrapper/sub-channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// @ts-check
import { TypedEmitter } from 'tiny-typed-emitter'

/**
* @typedef {Object} Events
* @property {(message: any) => void} message
*/

/**
* @extends {TypedEmitter<Events>}
*/
export class SubChannel extends TypedEmitter {
Copy link
Member Author

Choose a reason for hiding this comment

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

wondering if i should avoid using TypedEmitter for this so that this can run in a browser environment as well (it's used for both server and client).

I havea local implementation that works using EventTarget based on this helpful post.

one of the main differences is the spec for the dispatchEvent() method:

The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent().

Most notably the part about synchronously invoking listeners. Not sure if the Node implementation adheres to this or if it still behaves similar to an event emitter, but wondering if this specific detail could cause problems in our case or not.

Copy link
Member Author

Choose a reason for hiding this comment

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

i guess the simpler option would be using https://github.com/primus/eventemitter3, which is what rpc-reflector uses.

it's cool that the EventTarget approach works for our use case though!

Copy link
Member Author

Choose a reason for hiding this comment

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

addressed via e067dc3 (using eventemitter3)

#id
#messagePort

/**
* @param {import('rpc-reflector/server.js').MessagePortLike} messagePort Parent channel to add namespace to
* @param {string} id ID for the subchannel
*/
constructor(messagePort, id) {
super()

this.#id = id
this.#messagePort = messagePort

// Listen to all messages on the shared channel but only handle the relevant ones
this.#messagePort.on('message', ({ id, message }) => {
if (this.#id !== id) return
this.emit('message', message)
})
}

/**
* Send messages with the subchannel's ID
* @param {any} message
*/
postMessage(message) {
this.#messagePort.postMessage({ id: this.#id, message })
Copy link
Member

Choose a reason for hiding this comment

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

I realized that this should probably also queue when idle (need to check regular MessagePort behaviour), but let's deal with that in a pos-MVP follow-up. It's not going to affect existing code

Copy link
Member Author

Choose a reason for hiding this comment

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

Created issue here: #277

}
}
Loading
Loading