@livestore/webmesh 0.3.0-dev.2 → 0.3.0-dev.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/channel/message-channel-internal.d.ts +26 -0
- package/dist/channel/message-channel-internal.d.ts.map +1 -0
- package/dist/channel/message-channel-internal.js +217 -0
- package/dist/channel/message-channel-internal.js.map +1 -0
- package/dist/channel/message-channel.d.ts +21 -19
- package/dist/channel/message-channel.d.ts.map +1 -1
- package/dist/channel/message-channel.js +132 -162
- package/dist/channel/message-channel.js.map +1 -1
- package/dist/channel/proxy-channel.d.ts +3 -3
- package/dist/channel/proxy-channel.d.ts.map +1 -1
- package/dist/channel/proxy-channel.js +38 -19
- package/dist/channel/proxy-channel.js.map +1 -1
- package/dist/common.d.ts +36 -14
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +7 -4
- package/dist/common.js.map +1 -1
- package/dist/mesh-schema.d.ts +71 -5
- package/dist/mesh-schema.d.ts.map +1 -1
- package/dist/mesh-schema.js +55 -6
- package/dist/mesh-schema.js.map +1 -1
- package/dist/mod.d.ts +2 -2
- package/dist/mod.d.ts.map +1 -1
- package/dist/mod.js +2 -2
- package/dist/mod.js.map +1 -1
- package/dist/node.d.ts +43 -21
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +271 -95
- package/dist/node.js.map +1 -1
- package/dist/node.test.d.ts +1 -1
- package/dist/node.test.d.ts.map +1 -1
- package/dist/node.test.js +391 -156
- package/dist/node.test.js.map +1 -1
- package/dist/websocket-connection.d.ts +6 -7
- package/dist/websocket-connection.d.ts.map +1 -1
- package/dist/websocket-connection.js +21 -26
- package/dist/websocket-connection.js.map +1 -1
- package/dist/websocket-edge.d.ts +50 -0
- package/dist/websocket-edge.d.ts.map +1 -0
- package/dist/websocket-edge.js +69 -0
- package/dist/websocket-edge.js.map +1 -0
- package/dist/websocket-server.d.ts.map +1 -1
- package/dist/websocket-server.js +23 -9
- package/dist/websocket-server.js.map +1 -1
- package/package.json +7 -6
- package/src/channel/message-channel-internal.ts +356 -0
- package/src/channel/message-channel.ts +190 -310
- package/src/channel/proxy-channel.ts +259 -231
- package/src/common.ts +12 -13
- package/src/mesh-schema.ts +62 -6
- package/src/mod.ts +2 -2
- package/src/node.test.ts +554 -189
- package/src/node.ts +417 -134
- package/src/websocket-edge.ts +159 -0
- package/src/websocket-server.ts +26 -9
- package/tmp/pack.tgz +0 -0
- package/src/websocket-connection.ts +0 -158
|
@@ -1,175 +1,140 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
1
|
+
import { Cause, Deferred, Effect, Either, Exit, Option, Queue, Schema, Scope, Stream, TQueue, WebChannel, } from '@livestore/utils/effect';
|
|
2
|
+
import { nanoid } from '@livestore/utils/nanoid';
|
|
3
|
+
import * as WebmeshSchema from '../mesh-schema.js';
|
|
4
|
+
import { makeMessageChannelInternal } from './message-channel-internal.js';
|
|
5
|
+
/**
|
|
6
|
+
* Behaviour:
|
|
7
|
+
* - Waits until there is an initial edge
|
|
8
|
+
* - Automatically reconnects on disconnect
|
|
9
|
+
*
|
|
10
|
+
* Implementation notes:
|
|
11
|
+
* - We've split up the functionality into a wrapper channel and an internal channel.
|
|
12
|
+
* - The wrapper channel is responsible for:
|
|
13
|
+
* - Forwarding send/listen messages to the internal channel (via a queue)
|
|
14
|
+
* - Establishing the initial channel and reconnecting on disconnect
|
|
15
|
+
* - Listening for new edges as a hint to reconnect if not already connected
|
|
16
|
+
* - The wrapper channel maintains a edge counter which is used as the channel version
|
|
17
|
+
*
|
|
18
|
+
* If needed we can also implement further functionality (like heartbeat) in this wrapper channel.
|
|
19
|
+
*/
|
|
20
|
+
export const makeMessageChannel = ({ schema, newEdgeAvailablePubSub, channelName, checkTransferableEdges, nodeName, incomingPacketsQueue, target, sendPacket, }) => Effect.scopeWithCloseable((scope) => Effect.gen(function* () {
|
|
21
|
+
/** Only used to identify whether a source is the same instance to know when to reconnect */
|
|
22
|
+
const sourceId = nanoid();
|
|
23
|
+
const listenQueue = yield* Queue.unbounded();
|
|
24
|
+
const sendQueue = yield* TQueue.unbounded();
|
|
25
|
+
const initialEdgeDeferred = yield* Deferred.make();
|
|
26
|
+
const debugInfo = {
|
|
27
|
+
pendingSends: 0,
|
|
28
|
+
totalSends: 0,
|
|
29
|
+
connectCounter: 0,
|
|
30
|
+
isConnected: false,
|
|
31
|
+
innerChannelRef: { current: undefined },
|
|
32
|
+
};
|
|
33
|
+
// #region reconnect-loop
|
|
34
|
+
yield* Effect.gen(function* () {
|
|
35
|
+
const resultDeferred = yield* Deferred.make();
|
|
36
|
+
while (true) {
|
|
37
|
+
debugInfo.connectCounter++;
|
|
38
|
+
const channelVersion = debugInfo.connectCounter;
|
|
39
|
+
yield* Effect.spanEvent(`Connecting#${channelVersion}`);
|
|
40
|
+
const makeMessageChannelScope = yield* Scope.make();
|
|
41
|
+
// Attach the new scope to the parent scope
|
|
42
|
+
yield* Effect.addFinalizer((ex) => Scope.close(makeMessageChannelScope, ex));
|
|
43
|
+
/**
|
|
44
|
+
* Expected concurrency behaviour:
|
|
45
|
+
* - We're concurrently running the edge setup and the waitForNewEdgeFiber
|
|
46
|
+
* - Happy path:
|
|
47
|
+
* - The edge setup succeeds and we can interrupt the waitForNewEdgeFiber
|
|
48
|
+
* - Tricky paths:
|
|
49
|
+
* - While a edge is still being setup, we want to re-try when there is a new edge
|
|
50
|
+
* - If the edge setup returns a `MessageChannelResponseNoTransferables` error,
|
|
51
|
+
* we want to wait for a new edge and then re-try
|
|
52
|
+
* - Further notes:
|
|
53
|
+
* - If the parent scope closes, we want to also interrupt both the edge setup and the waitForNewEdgeFiber
|
|
54
|
+
* - We're creating a separate scope for each edge attempt, which
|
|
55
|
+
* - we'll use to fork the message channel in which allows us to interrupt it later
|
|
56
|
+
* - We need to make sure that "interruption" isn't "bubbling out"
|
|
57
|
+
*/
|
|
58
|
+
const waitForNewEdgeFiber = yield* Stream.fromPubSub(newEdgeAvailablePubSub).pipe(Stream.tap((edgeName) => Effect.spanEvent(`new-conn:${edgeName}`)), Stream.take(1), Stream.runDrain, Effect.as('new-edge'), Effect.fork);
|
|
59
|
+
const makeChannel = makeMessageChannelInternal({
|
|
60
|
+
nodeName,
|
|
61
|
+
sourceId,
|
|
62
|
+
incomingPacketsQueue,
|
|
63
|
+
target,
|
|
64
|
+
checkTransferableEdges,
|
|
65
|
+
channelName,
|
|
66
|
+
schema,
|
|
67
|
+
channelVersion,
|
|
68
|
+
newEdgeAvailablePubSub,
|
|
69
|
+
sendPacket,
|
|
70
|
+
scope: makeMessageChannelScope,
|
|
71
|
+
}).pipe(Scope.extend(makeMessageChannelScope), Effect.forkIn(makeMessageChannelScope),
|
|
72
|
+
// Given we only call `Effect.exit` later when joining the fiber,
|
|
73
|
+
// we don't want Effect to produce a "unhandled error" log message
|
|
74
|
+
Effect.withUnhandledErrorLogLevel(Option.none()));
|
|
75
|
+
const raceResult = yield* Effect.raceFirst(makeChannel, waitForNewEdgeFiber.pipe(Effect.disconnect));
|
|
76
|
+
if (raceResult === 'new-edge') {
|
|
77
|
+
yield* Scope.close(makeMessageChannelScope, Exit.fail('new-edge'));
|
|
78
|
+
// We'll try again
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
const channelExit = yield* raceResult.pipe(Effect.exit);
|
|
82
|
+
if (channelExit._tag === 'Failure') {
|
|
83
|
+
yield* Scope.close(makeMessageChannelScope, channelExit);
|
|
84
|
+
if (Cause.isFailType(channelExit.cause) &&
|
|
85
|
+
Schema.is(WebmeshSchema.MessageChannelResponseNoTransferables)(channelExit.cause.error)) {
|
|
86
|
+
// Only retry when there is a new edge available
|
|
87
|
+
yield* waitForNewEdgeFiber.pipe(Effect.exit);
|
|
32
88
|
}
|
|
33
|
-
const thisSideAlsoResponded = channelState._tag === 'ResponseSent';
|
|
34
|
-
const usePortFromThisSide = thisSideAlsoResponded && nodeName > target;
|
|
35
|
-
yield* Effect.annotateCurrentSpan({ usePortFromThisSide });
|
|
36
|
-
const winnerPort = usePortFromThisSide ? channelState.locallyCreatedPort : packet.port;
|
|
37
|
-
yield* Deferred.succeed(channelState.deferred, winnerPort);
|
|
38
|
-
return;
|
|
39
89
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
yield* Deferred.fail(channelState.deferred, packet);
|
|
44
|
-
channelStateRef.current = yield* makeInitialState;
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
case 'MessageChannelRequest': {
|
|
48
|
-
const mc = new MessageChannel();
|
|
49
|
-
const shouldReconnect = channelState._tag === 'Established';
|
|
50
|
-
const deferred = channelState._tag === 'Established'
|
|
51
|
-
? yield* Deferred.make()
|
|
52
|
-
: channelState.deferred;
|
|
53
|
-
channelStateRef.current = { _tag: 'ResponseSent', locallyCreatedPort: mc.port1, deferred };
|
|
54
|
-
yield* respondToSender(MeshSchema.MessageChannelResponseSuccess.make({
|
|
55
|
-
reqId: packet.id,
|
|
56
|
-
target,
|
|
57
|
-
source: nodeName,
|
|
58
|
-
channelName: packet.channelName,
|
|
59
|
-
hops: [],
|
|
60
|
-
remainingHops: packet.hops,
|
|
61
|
-
port: mc.port2,
|
|
62
|
-
}));
|
|
63
|
-
// If there's an established channel, we use the new request as a signal
|
|
64
|
-
// to drop the old channel and use the new one
|
|
65
|
-
if (shouldReconnect) {
|
|
66
|
-
yield* reconnect;
|
|
67
|
-
}
|
|
90
|
+
else {
|
|
91
|
+
const channel = channelExit.value;
|
|
92
|
+
yield* Deferred.succeed(resultDeferred, { channel, makeMessageChannelScope, channelVersion });
|
|
68
93
|
break;
|
|
69
94
|
}
|
|
70
|
-
default: {
|
|
71
|
-
return casesHandled(packet);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}).pipe(Effect.withSpan(`handleMessagePacket:${packet._tag}:${packet.source}→${packet.target}`, {
|
|
75
|
-
attributes: packetAsOtelAttributes(packet),
|
|
76
|
-
}));
|
|
77
|
-
yield* Stream.fromQueue(queue).pipe(Stream.tap(processMessagePacket), Stream.runDrain, Effect.tapCauseLogPretty, Effect.forkScoped);
|
|
78
|
-
const channelFromPort = (port) => Effect.gen(function* () {
|
|
79
|
-
channelStateRef.current = { _tag: 'Established' };
|
|
80
|
-
// NOTE to support re-connects we need to ack each message
|
|
81
|
-
const channel = yield* WebChannel.messagePortChannelWithAck({ port, schema });
|
|
82
|
-
return channel;
|
|
83
|
-
});
|
|
84
|
-
const channelState = channelStateRef.current;
|
|
85
|
-
if (channelState._tag === 'Initial' || channelState._tag === 'RequestSent') {
|
|
86
|
-
// Important to make a new deferred here as the old one might have been used already
|
|
87
|
-
// TODO model this better
|
|
88
|
-
const deferred = channelState._tag === 'RequestSent'
|
|
89
|
-
? yield* Deferred.make()
|
|
90
|
-
: channelState.deferred;
|
|
91
|
-
channelStateRef.current = { _tag: 'RequestSent', deferred };
|
|
92
|
-
const connectionRequest = Effect.gen(function* () {
|
|
93
|
-
const packet = MeshSchema.MessageChannelRequest.make({ source: nodeName, target, channelName, hops: [] });
|
|
94
|
-
const noTransferableResponse = checkTransferableConnections(packet);
|
|
95
|
-
if (noTransferableResponse !== undefined) {
|
|
96
|
-
yield* Effect.spanEvent(`No transferable connections found for ${packet.source}→${packet.target}`);
|
|
97
|
-
yield* Deferred.fail(deferred, noTransferableResponse);
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
yield* sendPacket(packet);
|
|
101
|
-
});
|
|
102
|
-
yield* connectionRequest;
|
|
103
|
-
const retryOnNewConnectionFiber = yield* Stream.fromPubSub(newConnectionAvailablePubSub).pipe(Stream.tap(() => Effect.spanEvent(`RetryOnNewConnection`)), Stream.tap(() => connectionRequest), Stream.runDrain, Effect.forkScoped);
|
|
104
|
-
const portResult = yield* deferred.pipe(Effect.either);
|
|
105
|
-
yield* Fiber.interrupt(retryOnNewConnectionFiber);
|
|
106
|
-
if (portResult._tag === 'Right') {
|
|
107
|
-
return yield* channelFromPort(portResult.right);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
// We'll keep retrying with a new connection
|
|
111
|
-
yield* Stream.fromPubSub(newConnectionAvailablePubSub).pipe(Stream.take(1), Stream.runDrain);
|
|
112
|
-
yield* reconnect;
|
|
113
|
-
return yield* Effect.interrupt;
|
|
114
95
|
}
|
|
115
96
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
97
|
+
// Now we wait until the first channel is established
|
|
98
|
+
const { channel, makeMessageChannelScope, channelVersion } = yield* resultDeferred;
|
|
99
|
+
yield* Effect.spanEvent(`Connected#${channelVersion}`);
|
|
100
|
+
debugInfo.isConnected = true;
|
|
101
|
+
debugInfo.innerChannelRef.current = channel;
|
|
102
|
+
yield* Deferred.succeed(initialEdgeDeferred, void 0);
|
|
103
|
+
// We'll now forward all incoming messages to the listen queue
|
|
104
|
+
yield* channel.listen.pipe(Stream.flatten(),
|
|
105
|
+
// Stream.tap((msg) => Effect.log(`${target}→${channelName}→${nodeName}:message:${msg.message}`)),
|
|
106
|
+
Stream.tapChunk((chunk) => Queue.offerAll(listenQueue, chunk)), Stream.runDrain, Effect.tapCauseLogPretty, Effect.forkIn(makeMessageChannelScope));
|
|
107
|
+
yield* Effect.gen(function* () {
|
|
108
|
+
while (true) {
|
|
109
|
+
const [msg, deferred] = yield* TQueue.peek(sendQueue);
|
|
110
|
+
// NOTE we don't need an explicit retry flow here since in case of the channel being closed,
|
|
111
|
+
// the send will never succeed. Meanwhile the send-loop fiber will be interrupted and
|
|
112
|
+
// given we only peeked at the queue, the message to send is still there.
|
|
113
|
+
yield* channel.send(msg);
|
|
114
|
+
yield* Deferred.succeed(deferred, void 0);
|
|
115
|
+
yield* TQueue.take(sendQueue); // Remove the message from the queue
|
|
125
116
|
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const internalChannel = yield* makeMessageChannelInternal;
|
|
137
|
-
yield* SubscriptionRef.set(internalChannelSref, internalChannel);
|
|
138
|
-
yield* Effect.spanEvent(`Connected#${connectCount}`);
|
|
139
|
-
yield* internalChannel.listen.pipe(Stream.flatten(), Stream.tap((msg) => Queue.offer(listenQueue, msg)), Stream.runDrain, Effect.tapCauseLogPretty, Effect.forkScoped);
|
|
140
|
-
yield* Effect.never;
|
|
141
|
-
}).pipe(Effect.scoped);
|
|
142
|
-
const fiberHandle = yield* FiberHandle.make();
|
|
143
|
-
const runConnect = Effect.gen(function* () {
|
|
144
|
-
// Cleanly shutdown the previous connection first
|
|
145
|
-
// Otherwise the old and new connection will "overlap"
|
|
146
|
-
yield* FiberHandle.clear(fiberHandle);
|
|
147
|
-
yield* FiberHandle.run(fiberHandle, connect);
|
|
148
|
-
});
|
|
149
|
-
yield* runConnect;
|
|
150
|
-
// Then listen for reconnects
|
|
151
|
-
yield* Stream.fromQueue(reconnectTriggerQueue).pipe(Stream.tap(() => runConnect), Stream.runDrain, Effect.tapCauseLogPretty, Effect.forkScoped);
|
|
152
|
-
// Wait for the initial connection to be established or for an error to occur
|
|
153
|
-
yield* Effect.raceFirst(SubscriptionRef.waitUntil(internalChannelSref, (channel) => channel !== false), FiberHandle.join(fiberHandle));
|
|
117
|
+
}).pipe(Effect.forkIn(makeMessageChannelScope));
|
|
118
|
+
// Wait until the channel is closed and then try to reconnect
|
|
119
|
+
yield* channel.closedDeferred;
|
|
120
|
+
yield* Scope.close(makeMessageChannelScope, Exit.succeed('channel-closed'));
|
|
121
|
+
yield* Effect.spanEvent(`Disconnected#${channelVersion}`);
|
|
122
|
+
debugInfo.isConnected = false;
|
|
123
|
+
debugInfo.innerChannelRef.current = undefined;
|
|
124
|
+
}).pipe(Effect.scoped, // Additionally scoping here to clean up finalizers after each loop run
|
|
125
|
+
Effect.forever, Effect.tapCauseLogPretty, Effect.forkScoped);
|
|
126
|
+
// #endregion reconnect-loop
|
|
154
127
|
const parentSpan = yield* Effect.currentSpan.pipe(Effect.orDie);
|
|
155
128
|
const send = (message) => Effect.gen(function* () {
|
|
156
|
-
const sendFiberHandle = yield* FiberHandle.make();
|
|
157
129
|
const sentDeferred = yield* Deferred.make();
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
yield* channel.send(message);
|
|
162
|
-
yield* Deferred.succeed(sentDeferred, void 0);
|
|
163
|
-
});
|
|
164
|
-
yield* innerSend.pipe(Effect.timeout(100), Effect.retry(Schedule.exponential(100)), Effect.orDie);
|
|
165
|
-
}).pipe(Effect.tapErrorCause(Effect.logError));
|
|
166
|
-
const rerunOnNewChannelFiber = yield* internalChannelSref.changes.pipe(Stream.filter((_) => _ === false), Stream.tap(() => FiberHandle.run(sendFiberHandle, trySend)), Stream.runDrain, Effect.fork);
|
|
167
|
-
yield* FiberHandle.run(sendFiberHandle, trySend);
|
|
130
|
+
debugInfo.pendingSends++;
|
|
131
|
+
debugInfo.totalSends++;
|
|
132
|
+
yield* TQueue.offer(sendQueue, [message, sentDeferred]);
|
|
168
133
|
yield* sentDeferred;
|
|
169
|
-
|
|
134
|
+
debugInfo.pendingSends--;
|
|
170
135
|
}).pipe(Effect.scoped, Effect.withParentSpan(parentSpan));
|
|
171
|
-
const listen = Stream.fromQueue(listenQueue).pipe(Stream.map(Either.right));
|
|
172
|
-
const closedDeferred = yield* Deferred.make();
|
|
136
|
+
const listen = Stream.fromQueue(listenQueue, { maxChunkSize: 1 }).pipe(Stream.map(Either.right));
|
|
137
|
+
const closedDeferred = yield* Deferred.make().pipe(Effect.acquireRelease(Deferred.done(Exit.void)));
|
|
173
138
|
const webChannel = {
|
|
174
139
|
[WebChannel.WebChannelSymbol]: WebChannel.WebChannelSymbol,
|
|
175
140
|
send,
|
|
@@ -177,7 +142,12 @@ export const makeMessageChannel = ({ nodeName, queue, newConnectionAvailablePubS
|
|
|
177
142
|
closedDeferred,
|
|
178
143
|
supportsTransferables: true,
|
|
179
144
|
schema,
|
|
145
|
+
debugInfo,
|
|
146
|
+
shutdown: Scope.close(scope, Exit.succeed('shutdown')),
|
|
147
|
+
};
|
|
148
|
+
return {
|
|
149
|
+
webChannel: webChannel,
|
|
150
|
+
initialEdgeDeferred,
|
|
180
151
|
};
|
|
181
|
-
|
|
182
|
-
}).pipe(Effect.withSpanScoped('makeMessageChannel'));
|
|
152
|
+
}));
|
|
183
153
|
//# sourceMappingURL=message-channel.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message-channel.js","sourceRoot":"","sources":["../../src/channel/message-channel.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"message-channel.js","sourceRoot":"","sources":["../../src/channel/message-channel.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,QAAQ,EACR,MAAM,EACN,MAAM,EACN,IAAI,EACJ,MAAM,EACN,KAAK,EACL,MAAM,EACN,KAAK,EACL,MAAM,EACN,MAAM,EACN,UAAU,GACX,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AAEhD,OAAO,KAAK,aAAa,MAAM,mBAAmB,CAAA;AAElD,OAAO,EAAE,0BAA0B,EAAE,MAAM,+BAA+B,CAAA;AAE1E;;;;;;;;;;;;;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EAAE,IAAI;QAC3B,MAAM;QACN,SAAS;QACT,QAAQ,EAAE,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;KACb,CAAA;IAE3C,OAAO;QACL,UAAU,EAAE,UAA6C;QACzD,mBAAmB;KACpB,CAAA;AACH,CAAC,CAAC,CACH,CAAA"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { PubSub } from '@livestore/utils/effect';
|
|
2
|
-
import { Effect, Queue, Schema, WebChannel } from '@livestore/utils/effect';
|
|
2
|
+
import { Effect, Queue, Schema, Scope, WebChannel } from '@livestore/utils/effect';
|
|
3
3
|
import { type ChannelName, type MeshNodeName, type ProxyQueueItem } from '../common.js';
|
|
4
4
|
import * as MeshSchema from '../mesh-schema.js';
|
|
5
5
|
interface MakeProxyChannelArgs {
|
|
6
6
|
queue: Queue.Queue<ProxyQueueItem>;
|
|
7
7
|
nodeName: MeshNodeName;
|
|
8
|
-
|
|
8
|
+
newEdgeAvailablePubSub: PubSub.PubSub<MeshNodeName>;
|
|
9
9
|
sendPacket: (packet: typeof MeshSchema.ProxyChannelPacket.Type) => Effect.Effect<void>;
|
|
10
10
|
channelName: ChannelName;
|
|
11
11
|
target: MeshNodeName;
|
|
@@ -14,6 +14,6 @@ interface MakeProxyChannelArgs {
|
|
|
14
14
|
listen: Schema.Schema<any, any>;
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
-
export declare const makeProxyChannel: ({ queue, nodeName,
|
|
17
|
+
export declare const makeProxyChannel: ({ queue, nodeName, newEdgeAvailablePubSub, sendPacket, target, channelName, schema, }: MakeProxyChannelArgs) => Effect.Effect<WebChannel.WebChannel<any, any, never>, never, Scope.Scope>;
|
|
18
18
|
export {};
|
|
19
19
|
//# sourceMappingURL=proxy-channel.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"proxy-channel.d.ts","sourceRoot":"","sources":["../../src/channel/proxy-channel.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AACrD,OAAO,EAEL,MAAM,
|
|
1
|
+
{"version":3,"file":"proxy-channel.d.ts","sourceRoot":"","sources":["../../src/channel/proxy-channel.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AACrD,OAAO,EAEL,MAAM,EAKN,KAAK,EAEL,MAAM,EACN,KAAK,EAGL,UAAU,EACX,MAAM,yBAAyB,CAAA;AAGhC,OAAO,EAEL,KAAK,WAAW,EAChB,KAAK,YAAY,EAEjB,KAAK,cAAc,EACpB,MAAM,cAAc,CAAA;AACrB,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAA;AAE/C,UAAU,oBAAoB;IAC5B,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,cAAc,CAAC,CAAA;IAClC,QAAQ,EAAE,YAAY,CAAA;IACtB,sBAAsB,EAAE,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;IACnD,UAAU,EAAE,CAAC,MAAM,EAAE,OAAO,UAAU,CAAC,kBAAkB,CAAC,IAAI,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACtF,WAAW,EAAE,WAAW,CAAA;IACxB,MAAM,EAAE,YAAY,CAAA;IACpB,MAAM,EAAE;QACN,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;QAC7B,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;KAChC,CAAA;CACF;AAED,eAAO,MAAM,gBAAgB,GAAI,uFAQ9B,oBAAoB,8EAsTpB,CAAA"}
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { casesHandled, shouldNeverHappen } from '@livestore/utils';
|
|
2
|
-
import { Deferred, Effect, Either, Fiber, FiberHandle, Queue, Schedule, Schema, Stream, SubscriptionRef, WebChannel, } from '@livestore/utils/effect';
|
|
2
|
+
import { Deferred, Effect, Either, Exit, Fiber, FiberHandle, Queue, Schedule, Schema, Scope, Stream, SubscriptionRef, WebChannel, } from '@livestore/utils/effect';
|
|
3
3
|
import { nanoid } from '@livestore/utils/nanoid';
|
|
4
4
|
import { packetAsOtelAttributes, } from '../common.js';
|
|
5
5
|
import * as MeshSchema from '../mesh-schema.js';
|
|
6
|
-
export const makeProxyChannel = ({ queue, nodeName,
|
|
6
|
+
export const makeProxyChannel = ({ queue, nodeName, newEdgeAvailablePubSub, sendPacket, target, channelName, schema, }) => Effect.scopeWithCloseable((scope) => Effect.gen(function* () {
|
|
7
7
|
const channelStateRef = { current: { _tag: 'Initial' } };
|
|
8
|
+
const debugInfo = {
|
|
9
|
+
pendingSends: 0,
|
|
10
|
+
totalSends: 0,
|
|
11
|
+
connectCounter: 0,
|
|
12
|
+
isConnected: false,
|
|
13
|
+
};
|
|
8
14
|
/**
|
|
9
15
|
* We need to unique identify a channel as multiple channels might exist between the same two nodes.
|
|
10
16
|
* We do this by letting each channel end generate a unique id and then combining them in a deterministic way.
|
|
@@ -18,7 +24,7 @@ export const makeProxyChannel = ({ queue, nodeName, newConnectionAvailablePubSub
|
|
|
18
24
|
return state;
|
|
19
25
|
});
|
|
20
26
|
const setStateToEstablished = (channelId) => Effect.gen(function* () {
|
|
21
|
-
// TODO avoid "double" `Connected` events (we might call `setStateToEstablished` twice during initial
|
|
27
|
+
// TODO avoid "double" `Connected` events (we might call `setStateToEstablished` twice during initial edge)
|
|
22
28
|
yield* Effect.spanEvent(`Connected (${channelId})`).pipe(Effect.withParentSpan(channelSpan));
|
|
23
29
|
channelStateRef.current = {
|
|
24
30
|
_tag: 'Established',
|
|
@@ -28,13 +34,14 @@ export const makeProxyChannel = ({ queue, nodeName, newConnectionAvailablePubSub
|
|
|
28
34
|
combinedChannelId: channelId,
|
|
29
35
|
};
|
|
30
36
|
yield* SubscriptionRef.set(connectedStateRef, channelStateRef.current);
|
|
37
|
+
debugInfo.isConnected = true;
|
|
31
38
|
});
|
|
32
|
-
const
|
|
39
|
+
const edgeRequest = Effect.suspend(() => sendPacket(MeshSchema.ProxyChannelRequest.make({ channelName, hops: [], source: nodeName, target, channelIdCandidate })));
|
|
33
40
|
const getCombinedChannelId = (otherSideChannelIdCandidate) => [channelIdCandidate, otherSideChannelIdCandidate].sort().join('_');
|
|
34
41
|
const processProxyPacket = ({ packet, respondToSender }) => Effect.gen(function* () {
|
|
35
42
|
// yield* Effect.log(`${nodeName}:processing packet ${packet._tag} from ${packet.source}`)
|
|
36
43
|
const otherSideName = packet.source;
|
|
37
|
-
const channelKey =
|
|
44
|
+
const channelKey = `target:${otherSideName}, channelName:${packet.channelName}`;
|
|
38
45
|
const channelState = channelStateRef.current;
|
|
39
46
|
switch (packet._tag) {
|
|
40
47
|
case 'ProxyChannelRequest': {
|
|
@@ -43,9 +50,11 @@ export const makeProxyChannel = ({ queue, nodeName, newConnectionAvailablePubSub
|
|
|
43
50
|
yield* SubscriptionRef.set(connectedStateRef, false);
|
|
44
51
|
channelStateRef.current = { _tag: 'Pending', initiatedVia: 'incoming-request' };
|
|
45
52
|
yield* Effect.spanEvent(`Reconnecting`).pipe(Effect.withParentSpan(channelSpan));
|
|
46
|
-
|
|
53
|
+
debugInfo.isConnected = false;
|
|
54
|
+
debugInfo.connectCounter++;
|
|
55
|
+
// If we're already connected, we need to re-establish the edge
|
|
47
56
|
if (channelState._tag === 'Established' && channelState.combinedChannelId !== combinedChannelId) {
|
|
48
|
-
yield*
|
|
57
|
+
yield* edgeRequest;
|
|
49
58
|
}
|
|
50
59
|
}
|
|
51
60
|
yield* respondToSender(MeshSchema.ProxyChannelResponseSuccess.make({
|
|
@@ -63,8 +72,9 @@ export const makeProxyChannel = ({ queue, nodeName, newConnectionAvailablePubSub
|
|
|
63
72
|
case 'ProxyChannelResponseSuccess': {
|
|
64
73
|
if (channelState._tag !== 'Pending') {
|
|
65
74
|
// return shouldNeverHappen(`Expected proxy channel to be pending but got ${channelState._tag}`)
|
|
66
|
-
if (channelState._tag === 'Established' &&
|
|
67
|
-
|
|
75
|
+
if (channelState._tag === 'Established' &&
|
|
76
|
+
channelState.combinedChannelId !== packet.combinedChannelId) {
|
|
77
|
+
return shouldNeverHappen(`ProxyChannel[${channelKey}]: Expected proxy channel to have the same combinedChannelId as the packet:\n${channelState.combinedChannelId} (channel) === ${packet.combinedChannelId} (packet)`);
|
|
68
78
|
}
|
|
69
79
|
else {
|
|
70
80
|
// for now just ignore it but should be looked into (there seems to be some kind of race condition/inefficiency)
|
|
@@ -72,7 +82,7 @@ export const makeProxyChannel = ({ queue, nodeName, newConnectionAvailablePubSub
|
|
|
72
82
|
}
|
|
73
83
|
const combinedChannelId = getCombinedChannelId(packet.channelIdCandidate);
|
|
74
84
|
if (combinedChannelId !== packet.combinedChannelId) {
|
|
75
|
-
return yield* Effect.die(`Expected proxy channel to have the same combinedChannelId as the packet:\n${combinedChannelId} (channel) === ${packet.combinedChannelId} (packet)`);
|
|
85
|
+
return yield* Effect.die(`ProxyChannel[${channelKey}]: Expected proxy channel to have the same combinedChannelId as the packet:\n${combinedChannelId} (channel) === ${packet.combinedChannelId} (packet)`);
|
|
76
86
|
}
|
|
77
87
|
yield* setStateToEstablished(packet.combinedChannelId);
|
|
78
88
|
return;
|
|
@@ -84,7 +94,7 @@ export const makeProxyChannel = ({ queue, nodeName, newConnectionAvailablePubSub
|
|
|
84
94
|
return;
|
|
85
95
|
}
|
|
86
96
|
if (channelState.combinedChannelId !== packet.combinedChannelId) {
|
|
87
|
-
return yield* Effect.die(`Expected proxy channel to have the same combinedChannelId as the packet:\n${channelState.combinedChannelId} (channel) === ${packet.combinedChannelId} (packet)`);
|
|
97
|
+
return yield* Effect.die(`ProxyChannel[${channelKey}]: Expected proxy channel to have the same combinedChannelId as the packet:\n${channelState.combinedChannelId} (channel) === ${packet.combinedChannelId} (packet)`);
|
|
88
98
|
}
|
|
89
99
|
yield* respondToSender(MeshSchema.ProxyChannelPayloadAck.make({
|
|
90
100
|
reqId: packet.id,
|
|
@@ -105,7 +115,7 @@ export const makeProxyChannel = ({ queue, nodeName, newConnectionAvailablePubSub
|
|
|
105
115
|
return;
|
|
106
116
|
}
|
|
107
117
|
const ack = channelState.ackMap.get(packet.reqId) ??
|
|
108
|
-
shouldNeverHappen(`Expected ack for ${packet.reqId}
|
|
118
|
+
shouldNeverHappen(`[ProxyChannel[${channelKey}]] Expected ack for ${packet.reqId}`);
|
|
109
119
|
yield* Deferred.succeed(ack, void 0);
|
|
110
120
|
channelState.ackMap.delete(packet.reqId);
|
|
111
121
|
return;
|
|
@@ -122,23 +132,25 @@ export const makeProxyChannel = ({ queue, nodeName, newConnectionAvailablePubSub
|
|
|
122
132
|
yield* Effect.spanEvent(`Connecting`);
|
|
123
133
|
const ackMap = new Map();
|
|
124
134
|
// check if already established via incoming `ProxyChannelRequest` from other side
|
|
125
|
-
// which indicates we already have a
|
|
135
|
+
// which indicates we already have a edge to the target node
|
|
126
136
|
// const channelState = channelStateRef.current
|
|
127
137
|
{
|
|
128
138
|
if (channelStateRef.current._tag !== 'Initial') {
|
|
129
139
|
return shouldNeverHappen('Expected proxy channel to be Initial');
|
|
130
140
|
}
|
|
131
141
|
channelStateRef.current = { _tag: 'Pending', initiatedVia: 'outgoing-request' };
|
|
132
|
-
yield*
|
|
133
|
-
const
|
|
142
|
+
yield* edgeRequest;
|
|
143
|
+
const retryOnNewEdgeFiber = yield* Stream.fromPubSub(newEdgeAvailablePubSub).pipe(Stream.tap(() => edgeRequest), Stream.runDrain, Effect.forkScoped);
|
|
134
144
|
const { combinedChannelId: channelId } = yield* waitForEstablished;
|
|
135
|
-
yield* Fiber.interrupt(
|
|
145
|
+
yield* Fiber.interrupt(retryOnNewEdgeFiber);
|
|
136
146
|
yield* setStateToEstablished(channelId);
|
|
137
147
|
}
|
|
138
148
|
const send = (message) => Effect.gen(function* () {
|
|
139
149
|
const payload = yield* Schema.encodeUnknown(schema.send)(message);
|
|
140
150
|
const sendFiberHandle = yield* FiberHandle.make();
|
|
141
151
|
const sentDeferred = yield* Deferred.make();
|
|
152
|
+
debugInfo.pendingSends++;
|
|
153
|
+
debugInfo.totalSends++;
|
|
142
154
|
const trySend = Effect.gen(function* () {
|
|
143
155
|
const { combinedChannelId } = (yield* SubscriptionRef.waitUntil(connectedStateRef, (channel) => channel !== false));
|
|
144
156
|
const innerSend = Effect.gen(function* () {
|
|
@@ -152,12 +164,17 @@ export const makeProxyChannel = ({ queue, nodeName, newConnectionAvailablePubSub
|
|
|
152
164
|
target,
|
|
153
165
|
combinedChannelId,
|
|
154
166
|
});
|
|
167
|
+
// TODO consider handling previous ackMap entries which might leak/fill-up memory
|
|
168
|
+
// as only successful acks are removed from the map
|
|
155
169
|
ackMap.set(packet.id, ack);
|
|
156
170
|
yield* sendPacket(packet);
|
|
157
171
|
yield* ack;
|
|
158
172
|
yield* Deferred.succeed(sentDeferred, void 0);
|
|
173
|
+
debugInfo.pendingSends--;
|
|
159
174
|
});
|
|
160
|
-
|
|
175
|
+
// TODO make this configurable
|
|
176
|
+
// Schedule.exponential(10): 10, 20, 40, 80, 160, 320, ...
|
|
177
|
+
yield* innerSend.pipe(Effect.timeout(100), Effect.retry(Schedule.exponential(10)), Effect.orDie);
|
|
161
178
|
}).pipe(Effect.tapErrorCause(Effect.logError));
|
|
162
179
|
const rerunOnNewChannelFiber = yield* connectedStateRef.changes.pipe(Stream.filter((_) => _ === false), Stream.tap(() => FiberHandle.run(sendFiberHandle, trySend)), Stream.runDrain, Effect.fork);
|
|
163
180
|
yield* FiberHandle.run(sendFiberHandle, trySend);
|
|
@@ -165,7 +182,7 @@ export const makeProxyChannel = ({ queue, nodeName, newConnectionAvailablePubSub
|
|
|
165
182
|
yield* Fiber.interrupt(rerunOnNewChannelFiber);
|
|
166
183
|
}).pipe(Effect.scoped, Effect.withSpan(`sendAckWithRetry:ProxyChannelPayload`), Effect.withParentSpan(channelSpan));
|
|
167
184
|
const listen = Stream.fromQueue(listenQueue).pipe(Stream.map(Either.right));
|
|
168
|
-
const closedDeferred = yield* Deferred.make();
|
|
185
|
+
const closedDeferred = yield* Deferred.make().pipe(Effect.acquireRelease(Deferred.done(Exit.void)));
|
|
169
186
|
const webChannel = {
|
|
170
187
|
[WebChannel.WebChannelSymbol]: WebChannel.WebChannelSymbol,
|
|
171
188
|
send,
|
|
@@ -173,7 +190,9 @@ export const makeProxyChannel = ({ queue, nodeName, newConnectionAvailablePubSub
|
|
|
173
190
|
closedDeferred,
|
|
174
191
|
supportsTransferables: true,
|
|
175
192
|
schema,
|
|
193
|
+
shutdown: Scope.close(scope, Exit.void),
|
|
194
|
+
debugInfo,
|
|
176
195
|
};
|
|
177
196
|
return webChannel;
|
|
178
|
-
}).pipe(Effect.withSpanScoped('makeProxyChannel'));
|
|
197
|
+
}).pipe(Effect.withSpanScoped('makeProxyChannel')));
|
|
179
198
|
//# sourceMappingURL=proxy-channel.js.map
|