@livestore/webmesh 0.3.0-dev.10 → 0.3.0-dev.12
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 +20 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/channel/message-channel copy.d.ts +9 -0
- package/dist/channel/message-channel copy.d.ts.map +1 -0
- package/dist/channel/message-channel copy.js +137 -0
- package/dist/channel/message-channel copy.js.map +1 -0
- package/dist/channel/message-channel-internal copy.d.ts +42 -0
- package/dist/channel/message-channel-internal copy.d.ts.map +1 -0
- package/dist/channel/message-channel-internal copy.js +239 -0
- package/dist/channel/message-channel-internal copy.js.map +1 -0
- 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 +128 -162
- package/dist/channel/message-channel.js.map +1 -1
- package/dist/channel/proxy-channel.d.ts +2 -2
- package/dist/channel/proxy-channel.d.ts.map +1 -1
- package/dist/channel/proxy-channel.js +7 -5
- package/dist/channel/proxy-channel.js.map +1 -1
- package/dist/common.d.ts +8 -4
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +2 -1
- package/dist/common.js.map +1 -1
- package/dist/mesh-schema.d.ts +23 -1
- package/dist/mesh-schema.d.ts.map +1 -1
- package/dist/mesh-schema.js +21 -2
- package/dist/mesh-schema.js.map +1 -1
- package/dist/node.d.ts +12 -1
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +40 -9
- 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 +300 -147
- package/dist/node.test.js.map +1 -1
- package/dist/websocket-connection.d.ts +1 -2
- package/dist/websocket-connection.d.ts.map +1 -1
- package/dist/websocket-connection.js +5 -4
- package/dist/websocket-connection.js.map +1 -1
- package/package.json +3 -3
- package/src/channel/message-channel-internal.ts +356 -0
- package/src/channel/message-channel.ts +183 -311
- package/src/channel/proxy-channel.ts +238 -230
- package/src/common.ts +3 -1
- package/src/mesh-schema.ts +20 -2
- package/src/node.test.ts +426 -177
- package/src/node.ts +70 -12
- package/src/websocket-connection.ts +83 -79
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { casesHandled, shouldNeverHappen } from '@livestore/utils';
|
|
2
|
+
import { Deferred, Effect, Exit, OtelTracer, Predicate, Queue, Schema, Scope, Stream, WebChannel, } from '@livestore/utils/effect';
|
|
3
|
+
import { packetAsOtelAttributes } from '../common.js';
|
|
4
|
+
import * as MeshSchema from '../mesh-schema.js';
|
|
5
|
+
const makeDeferredResult = (Deferred.make);
|
|
6
|
+
/**
|
|
7
|
+
* The channel version is important here, as a channel will only be established once both sides have the same version.
|
|
8
|
+
* The version is used to avoid concurrency issues where both sides have different incompatible message ports.
|
|
9
|
+
*/
|
|
10
|
+
export const makeMessageChannelInternal = ({ nodeName, incomingPacketsQueue, target, checkTransferableConnections, channelName, schema: schema_, sendPacket, channelVersion, scope, sourceId, }) => Effect.gen(function* () {
|
|
11
|
+
// yield* Effect.addFinalizer((exit) =>
|
|
12
|
+
// Effect.spanEvent(`shutdown:${exit._tag === 'Success' ? 'Success' : Cause.pretty(exit.cause)}`),
|
|
13
|
+
// )
|
|
14
|
+
const deferred = yield* makeDeferredResult();
|
|
15
|
+
const span = yield* OtelTracer.currentOtelSpan.pipe(Effect.catchAll(() => Effect.succeed(undefined)));
|
|
16
|
+
// const span = {
|
|
17
|
+
// addEvent: (...msg: any[]) => console.log(`${nodeName}→${channelName}→${target}[${channelVersion}]`, ...msg),
|
|
18
|
+
// }
|
|
19
|
+
const schema = {
|
|
20
|
+
send: Schema.Union(schema_.send, MeshSchema.MessageChannelPing, MeshSchema.MessageChannelPong),
|
|
21
|
+
listen: Schema.Union(schema_.listen, MeshSchema.MessageChannelPing, MeshSchema.MessageChannelPong),
|
|
22
|
+
};
|
|
23
|
+
const channelStateRef = {
|
|
24
|
+
current: { _tag: 'Initial' },
|
|
25
|
+
};
|
|
26
|
+
const processMessagePacket = ({ packet, respondToSender }) => Effect.gen(function* () {
|
|
27
|
+
const channelState = channelStateRef.current;
|
|
28
|
+
span?.addEvent(`process:${packet._tag}`, {
|
|
29
|
+
channelState: channelState._tag,
|
|
30
|
+
packetId: packet.id,
|
|
31
|
+
packetReqId: packet.reqId,
|
|
32
|
+
packetChannelVersion: Predicate.hasProperty('channelVersion')(packet) ? packet.channelVersion : undefined,
|
|
33
|
+
});
|
|
34
|
+
// const reqIdStr =
|
|
35
|
+
// Predicate.hasProperty('reqId')(packet) && packet.reqId !== undefined ? ` for ${packet.reqId}` : ''
|
|
36
|
+
// yield* Effect.log(
|
|
37
|
+
// `${nodeName}→${channelName}→${target}[${channelVersion}]:process packet ${packet._tag} [${packet.id}${reqIdStr}], channel state: ${channelState._tag}`,
|
|
38
|
+
// )
|
|
39
|
+
if (channelState._tag === 'Initial')
|
|
40
|
+
return shouldNeverHappen();
|
|
41
|
+
if (packet._tag === 'MessageChannelResponseNoTransferables') {
|
|
42
|
+
yield* Deferred.fail(deferred, packet);
|
|
43
|
+
return 'close';
|
|
44
|
+
}
|
|
45
|
+
// If the other side has a higher version, we need to close this channel and
|
|
46
|
+
// recreate it with the new version
|
|
47
|
+
if (packet.channelVersion > channelVersion) {
|
|
48
|
+
span?.addEvent(`incoming packet has higher version (${packet.channelVersion}), closing channel`);
|
|
49
|
+
yield* Scope.close(scope, Exit.succeed('higher-version-expected'));
|
|
50
|
+
// TODO include expected version in the error so the channel gets recreated with the new version
|
|
51
|
+
return 'close';
|
|
52
|
+
}
|
|
53
|
+
// If this channel has a higher version, we need to signal the other side to close
|
|
54
|
+
// and recreate the channel with the new version
|
|
55
|
+
if (packet.channelVersion < channelVersion) {
|
|
56
|
+
const newPacket = MeshSchema.MessageChannelRequest.make({
|
|
57
|
+
source: nodeName,
|
|
58
|
+
sourceId,
|
|
59
|
+
target,
|
|
60
|
+
channelName,
|
|
61
|
+
channelVersion,
|
|
62
|
+
hops: [],
|
|
63
|
+
remainingHops: packet.hops,
|
|
64
|
+
reqId: undefined,
|
|
65
|
+
});
|
|
66
|
+
span?.addEvent(`incoming packet has lower version (${packet.channelVersion}), sending request to reconnect (${newPacket.id})`);
|
|
67
|
+
yield* sendPacket(newPacket);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (channelState._tag === 'Established' && packet._tag === 'MessageChannelRequest') {
|
|
71
|
+
if (packet.sourceId === channelState.otherSourceId) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
// In case the instance of the source has changed, we need to close the channel
|
|
76
|
+
// and reconnect with a new channel
|
|
77
|
+
span?.addEvent(`force-new-channel`);
|
|
78
|
+
yield* Scope.close(scope, Exit.succeed('force-new-channel'));
|
|
79
|
+
return 'close';
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
switch (packet._tag) {
|
|
83
|
+
// Assumption: Each side has sent an initial request and another request as a response for an incoming request
|
|
84
|
+
case 'MessageChannelRequest': {
|
|
85
|
+
if (channelState._tag !== 'RequestSent') {
|
|
86
|
+
// We can safely ignore further incoming requests as we're already creating a channel
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (packet.reqId === channelState.reqPacketId) {
|
|
90
|
+
// Circuit-breaker: We've already sent a request so we don't need to send another one
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
const newRequestPacket = MeshSchema.MessageChannelRequest.make({
|
|
94
|
+
source: nodeName,
|
|
95
|
+
sourceId,
|
|
96
|
+
target,
|
|
97
|
+
channelName,
|
|
98
|
+
channelVersion,
|
|
99
|
+
hops: [],
|
|
100
|
+
remainingHops: packet.hops,
|
|
101
|
+
reqId: packet.id,
|
|
102
|
+
});
|
|
103
|
+
span?.addEvent(`Re-sending new request (${newRequestPacket.id}) for incoming request (${packet.id})`);
|
|
104
|
+
yield* sendPacket(newRequestPacket);
|
|
105
|
+
}
|
|
106
|
+
const isWinner = nodeName > target;
|
|
107
|
+
if (isWinner) {
|
|
108
|
+
span?.addEvent(`winner side: creating message channel and sending response`);
|
|
109
|
+
const mc = new MessageChannel();
|
|
110
|
+
// We're using a message channel with acks here to make sure messages are not lost
|
|
111
|
+
// which might happen during re-connection scenarios.
|
|
112
|
+
// Also we need to eagerly start listening since we're using the channel "ourselves"
|
|
113
|
+
// for the initial ping-pong sequence.
|
|
114
|
+
const channel = yield* WebChannel.messagePortChannelWithAck({
|
|
115
|
+
port: mc.port1,
|
|
116
|
+
schema,
|
|
117
|
+
debugId: channelVersion,
|
|
118
|
+
}).pipe(Effect.andThen(WebChannel.toOpenChannel));
|
|
119
|
+
yield* respondToSender(MeshSchema.MessageChannelResponseSuccess.make({
|
|
120
|
+
reqId: packet.id,
|
|
121
|
+
target,
|
|
122
|
+
source: nodeName,
|
|
123
|
+
channelName: packet.channelName,
|
|
124
|
+
hops: [],
|
|
125
|
+
remainingHops: packet.hops,
|
|
126
|
+
port: mc.port2,
|
|
127
|
+
channelVersion,
|
|
128
|
+
}));
|
|
129
|
+
channelStateRef.current = { _tag: 'winner:ResponseSent', channel, otherSourceId: packet.sourceId };
|
|
130
|
+
// span?.addEvent(`winner side: waiting for ping`)
|
|
131
|
+
// Now we wait for the other side to respond via the channel
|
|
132
|
+
yield* channel.listen.pipe(Stream.flatten(), Stream.filter(Schema.is(MeshSchema.MessageChannelPing)), Stream.take(1), Stream.runDrain);
|
|
133
|
+
// span?.addEvent(`winner side: sending pong`)
|
|
134
|
+
yield* channel.send(MeshSchema.MessageChannelPong.make({}));
|
|
135
|
+
span?.addEvent(`winner side: established`);
|
|
136
|
+
channelStateRef.current = { _tag: 'Established', otherSourceId: packet.sourceId };
|
|
137
|
+
yield* Deferred.succeed(deferred, channel);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
span?.addEvent(`loser side: waiting for response`);
|
|
141
|
+
// Wait for `MessageChannelResponseSuccess` packet
|
|
142
|
+
channelStateRef.current = { _tag: 'loser:WaitingForResponse', otherSourceId: packet.sourceId };
|
|
143
|
+
}
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
case 'MessageChannelResponseSuccess': {
|
|
147
|
+
if (channelState._tag !== 'loser:WaitingForResponse') {
|
|
148
|
+
return shouldNeverHappen(`Expected to find message channel response from ${target}, but was in ${channelState._tag} state`);
|
|
149
|
+
}
|
|
150
|
+
// See message-channel notes above
|
|
151
|
+
const channel = yield* WebChannel.messagePortChannelWithAck({
|
|
152
|
+
port: packet.port,
|
|
153
|
+
schema,
|
|
154
|
+
debugId: channelVersion,
|
|
155
|
+
}).pipe(Effect.andThen(WebChannel.toOpenChannel));
|
|
156
|
+
const waitForPongFiber = yield* channel.listen.pipe(Stream.flatten(), Stream.filter(Schema.is(MeshSchema.MessageChannelPong)), Stream.take(1), Stream.runDrain, Effect.fork);
|
|
157
|
+
// span?.addEvent(`loser side: sending ping`)
|
|
158
|
+
// There seems to be some scenario where the initial ping message is lost.
|
|
159
|
+
// As a workaround until we find the root cause, we're retrying the ping a few times.
|
|
160
|
+
// TODO write a test that reproduces this issue and fix the root cause ()
|
|
161
|
+
// https://github.com/livestorejs/livestore/issues/262
|
|
162
|
+
yield* channel
|
|
163
|
+
.send(MeshSchema.MessageChannelPing.make({}))
|
|
164
|
+
.pipe(Effect.timeout(10), Effect.retry({ times: 2 }));
|
|
165
|
+
// span?.addEvent(`loser side: waiting for pong`)
|
|
166
|
+
yield* waitForPongFiber;
|
|
167
|
+
span?.addEvent(`loser side: established`);
|
|
168
|
+
channelStateRef.current = { _tag: 'Established', otherSourceId: channelState.otherSourceId };
|
|
169
|
+
yield* Deferred.succeed(deferred, channel);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
default: {
|
|
173
|
+
return casesHandled(packet);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}).pipe(Effect.withSpan(`handleMessagePacket:${packet._tag}:${packet.source}→${packet.target}`, {
|
|
177
|
+
attributes: packetAsOtelAttributes(packet),
|
|
178
|
+
}));
|
|
179
|
+
yield* Effect.gen(function* () {
|
|
180
|
+
while (true) {
|
|
181
|
+
const packet = yield* Queue.take(incomingPacketsQueue);
|
|
182
|
+
const res = yield* processMessagePacket(packet);
|
|
183
|
+
// We want to give requests another chance to be processed
|
|
184
|
+
if (res === 'close') {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}).pipe(Effect.interruptible, Effect.tapCauseLogPretty, Effect.forkScoped);
|
|
189
|
+
const channelState = channelStateRef.current;
|
|
190
|
+
if (channelState._tag !== 'Initial') {
|
|
191
|
+
return shouldNeverHappen(`Expected channel to be in Initial state, but was in ${channelState._tag} state`);
|
|
192
|
+
}
|
|
193
|
+
const connectionRequest = Effect.gen(function* () {
|
|
194
|
+
const packet = MeshSchema.MessageChannelRequest.make({
|
|
195
|
+
source: nodeName,
|
|
196
|
+
sourceId,
|
|
197
|
+
target,
|
|
198
|
+
channelName,
|
|
199
|
+
channelVersion,
|
|
200
|
+
hops: [],
|
|
201
|
+
reqId: undefined,
|
|
202
|
+
});
|
|
203
|
+
channelStateRef.current = { _tag: 'RequestSent', reqPacketId: packet.id };
|
|
204
|
+
// yield* Effect.log(`${nodeName}→${channelName}→${target}:connectionRequest [${channelVersion}]`)
|
|
205
|
+
const noTransferableResponse = checkTransferableConnections(packet);
|
|
206
|
+
if (noTransferableResponse !== undefined) {
|
|
207
|
+
yield* Effect.spanEvent(`No transferable connections found for ${packet.source}→${packet.target}`);
|
|
208
|
+
return yield* Effect.fail(noTransferableResponse);
|
|
209
|
+
}
|
|
210
|
+
yield* sendPacket(packet);
|
|
211
|
+
span?.addEvent(`initial connection request sent (${packet.id})`);
|
|
212
|
+
});
|
|
213
|
+
yield* connectionRequest;
|
|
214
|
+
const channel = yield* deferred;
|
|
215
|
+
return channel;
|
|
216
|
+
}).pipe(Effect.withSpanScoped(`makeMessageChannel:${channelVersion}`));
|
|
217
|
+
//# sourceMappingURL=message-channel-internal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-channel-internal.js","sourceRoot":"","sources":["../../src/channel/message-channel-internal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAElE,OAAO,EACL,QAAQ,EACR,MAAM,EACN,IAAI,EACJ,UAAU,EACV,SAAS,EACT,KAAK,EACL,MAAM,EACN,KAAK,EACL,MAAM,EACN,UAAU,GACX,MAAM,yBAAyB,CAAA;AAEhC,OAAO,EAA8D,sBAAsB,EAAE,MAAM,cAAc,CAAA;AACjH,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAA;AAgB/C,MAAM,kBAAkB,GAAG,CAAA,QAAQ,CAAC,IAGnC,CAAA,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,EACzC,QAAQ,EACR,oBAAoB,EACpB,MAAM,EACN,4BAA4B,EAC5B,WAAW,EACX,MAAM,EAAE,OAAO,EACf,UAAU,EACV,cAAc,EACd,KAAK,EACL,QAAQ,GAMT,EAIC,EAAE,CACF,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;IAClB,uCAAuC;IACvC,oGAAoG;IACpG,IAAI;IAwBJ,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,kBAAkB,EAAE,CAAA;IAE5C,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,UAAU,CAAC,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;IACrG,iBAAiB;IACjB,iHAAiH;IACjH,IAAI;IAEJ,MAAM,MAAM,GAAG;QACb,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,OAAO,CAA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|
-
|
|
1
|
+
import { Deferred, Effect, Scope, WebChannel } from '@livestore/utils/effect';
|
|
2
|
+
import type { MakeMessageChannelArgs } from './message-channel-internal.js';
|
|
3
|
+
/**
|
|
4
|
+
* Behaviour:
|
|
5
|
+
* - Waits until there is an initial connection
|
|
6
|
+
* - Automatically reconnects on disconnect
|
|
7
|
+
*
|
|
8
|
+
* Implementation notes:
|
|
9
|
+
* - We've split up the functionality into a wrapper channel and an internal channel.
|
|
10
|
+
* - The wrapper channel is responsible for:
|
|
11
|
+
* - Forwarding send/listen messages to the internal channel (via a queue)
|
|
12
|
+
* - Establishing the initial channel and reconnecting on disconnect
|
|
13
|
+
* - Listening for new connections as a hint to reconnect if not already connected
|
|
14
|
+
* - The wrapper channel maintains a connection counter which is used as the channel version
|
|
15
|
+
*
|
|
16
|
+
* If needed we can also implement further functionality (like heartbeat) in this wrapper channel.
|
|
17
|
+
*/
|
|
18
|
+
export declare const makeMessageChannel: ({ schema, newConnectionAvailablePubSub, channelName, checkTransferableConnections, nodeName, incomingPacketsQueue, target, sendPacket, }: MakeMessageChannelArgs) => Effect.Effect<{
|
|
19
|
+
webChannel: WebChannel.WebChannel<any, any>;
|
|
20
|
+
initialConnectionDeferred: Deferred.Deferred<void, never>;
|
|
21
|
+
}, never, Scope.Scope>;
|
|
20
22
|
//# sourceMappingURL=message-channel.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message-channel.d.ts","sourceRoot":"","sources":["../../src/channel/message-channel.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"message-channel.d.ts","sourceRoot":"","sources":["../../src/channel/message-channel.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,QAAQ,EACR,MAAM,EAKN,KAAK,EAGL,UAAU,EACX,MAAM,yBAAyB,CAAA;AAIhC,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAA;AAG3E;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,kBAAkB,6IAS5B,sBAAsB;gBAkLS,UAAU,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC;;sBAI9D,CAAA"}
|
|
@@ -1,175 +1,136 @@
|
|
|
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, Queue, Schema, Scope, Stream, TQueue, WebChannel, } from '@livestore/utils/effect';
|
|
2
|
+
import { nanoid } from '@livestore/utils/nanoid';
|
|
3
|
+
import { WebmeshSchema } from '../mod.js';
|
|
4
|
+
import { makeMessageChannelInternal } from './message-channel-internal.js';
|
|
5
|
+
/**
|
|
6
|
+
* Behaviour:
|
|
7
|
+
* - Waits until there is an initial connection
|
|
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 connections as a hint to reconnect if not already connected
|
|
16
|
+
* - The wrapper channel maintains a connection 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, newConnectionAvailablePubSub, channelName, checkTransferableConnections, 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 initialConnectionDeferred = 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 connection setup and the waitForNewConnectionFiber
|
|
46
|
+
* - Happy path:
|
|
47
|
+
* - The connection setup succeeds and we can interrupt the waitForNewConnectionFiber
|
|
48
|
+
* - Tricky paths:
|
|
49
|
+
* - While a connection is still being setup, we want to re-try when there is a new connection
|
|
50
|
+
* - If the connection setup returns a `MessageChannelResponseNoTransferables` error,
|
|
51
|
+
* we want to wait for a new connection and then re-try
|
|
52
|
+
* - Further notes:
|
|
53
|
+
* - If the parent scope closes, we want to also interrupt both the connection setup and the waitForNewConnectionFiber
|
|
54
|
+
* - We're creating a separate scope for each connection 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 waitForNewConnectionFiber = yield* Stream.fromPubSub(newConnectionAvailablePubSub).pipe(Stream.tap((connectionName) => Effect.spanEvent(`new-conn:${connectionName}`)), Stream.take(1), Stream.runDrain, Effect.as('new-connection'), Effect.fork);
|
|
59
|
+
const makeChannel = makeMessageChannelInternal({
|
|
60
|
+
nodeName,
|
|
61
|
+
sourceId,
|
|
62
|
+
incomingPacketsQueue,
|
|
63
|
+
target,
|
|
64
|
+
checkTransferableConnections,
|
|
65
|
+
channelName,
|
|
66
|
+
schema,
|
|
67
|
+
channelVersion,
|
|
68
|
+
newConnectionAvailablePubSub,
|
|
69
|
+
sendPacket,
|
|
70
|
+
scope: makeMessageChannelScope,
|
|
71
|
+
}).pipe(Scope.extend(makeMessageChannelScope), Effect.forkIn(makeMessageChannelScope));
|
|
72
|
+
const res = yield* Effect.raceFirst(makeChannel, waitForNewConnectionFiber.pipe(Effect.disconnect));
|
|
73
|
+
if (res === 'new-connection') {
|
|
74
|
+
yield* Scope.close(makeMessageChannelScope, Exit.fail('new-connection'));
|
|
75
|
+
// We'll try again
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
const result = yield* res.pipe(Effect.exit);
|
|
79
|
+
if (result._tag === 'Failure') {
|
|
80
|
+
yield* Scope.close(makeMessageChannelScope, result);
|
|
81
|
+
if (Cause.isFailType(result.cause) &&
|
|
82
|
+
Schema.is(WebmeshSchema.MessageChannelResponseNoTransferables)(result.cause.error)) {
|
|
83
|
+
yield* waitForNewConnectionFiber.pipe(Effect.exit);
|
|
32
84
|
}
|
|
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
85
|
}
|
|
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
|
-
}
|
|
86
|
+
else {
|
|
87
|
+
const channel = result.value;
|
|
88
|
+
yield* Deferred.succeed(resultDeferred, { channel, makeMessageChannelScope, channelVersion });
|
|
68
89
|
break;
|
|
69
90
|
}
|
|
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
91
|
}
|
|
115
92
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
93
|
+
// Now we wait until the first channel is established
|
|
94
|
+
const { channel, makeMessageChannelScope, channelVersion } = yield* resultDeferred;
|
|
95
|
+
yield* Effect.spanEvent(`Connected#${channelVersion}`);
|
|
96
|
+
debugInfo.isConnected = true;
|
|
97
|
+
debugInfo.innerChannelRef.current = channel;
|
|
98
|
+
yield* Deferred.succeed(initialConnectionDeferred, void 0);
|
|
99
|
+
// We'll now forward all incoming messages to the listen queue
|
|
100
|
+
yield* channel.listen.pipe(Stream.flatten(),
|
|
101
|
+
// Stream.tap((msg) => Effect.log(`${target}→${channelName}→${nodeName}:message:${msg.message}`)),
|
|
102
|
+
Stream.tapChunk((chunk) => Queue.offerAll(listenQueue, chunk)), Stream.runDrain, Effect.tapCauseLogPretty, Effect.forkIn(makeMessageChannelScope));
|
|
103
|
+
yield* Effect.gen(function* () {
|
|
104
|
+
while (true) {
|
|
105
|
+
const [msg, deferred] = yield* TQueue.peek(sendQueue);
|
|
106
|
+
// NOTE we don't need an explicit retry flow here since in case of the channel being closed,
|
|
107
|
+
// the send will never succeed. Meanwhile the send-loop fiber will be interrupted and
|
|
108
|
+
// given we only peeked at the queue, the message to send is still there.
|
|
109
|
+
yield* channel.send(msg);
|
|
110
|
+
yield* Deferred.succeed(deferred, void 0);
|
|
111
|
+
yield* TQueue.take(sendQueue); // Remove the message from the queue
|
|
125
112
|
}
|
|
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));
|
|
113
|
+
}).pipe(Effect.forkIn(makeMessageChannelScope));
|
|
114
|
+
// Wait until the channel is closed and then try to reconnect
|
|
115
|
+
yield* channel.closedDeferred;
|
|
116
|
+
yield* Scope.close(makeMessageChannelScope, Exit.succeed('channel-closed'));
|
|
117
|
+
yield* Effect.spanEvent(`Disconnected#${channelVersion}`);
|
|
118
|
+
debugInfo.isConnected = false;
|
|
119
|
+
debugInfo.innerChannelRef.current = undefined;
|
|
120
|
+
}).pipe(Effect.scoped, // Additionally scoping here to clean up finalizers after each loop run
|
|
121
|
+
Effect.forever, Effect.tapCauseLogPretty, Effect.forkScoped);
|
|
122
|
+
// #endregion reconnect-loop
|
|
154
123
|
const parentSpan = yield* Effect.currentSpan.pipe(Effect.orDie);
|
|
155
124
|
const send = (message) => Effect.gen(function* () {
|
|
156
|
-
const sendFiberHandle = yield* FiberHandle.make();
|
|
157
125
|
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);
|
|
126
|
+
debugInfo.pendingSends++;
|
|
127
|
+
debugInfo.totalSends++;
|
|
128
|
+
yield* TQueue.offer(sendQueue, [message, sentDeferred]);
|
|
168
129
|
yield* sentDeferred;
|
|
169
|
-
|
|
130
|
+
debugInfo.pendingSends--;
|
|
170
131
|
}).pipe(Effect.scoped, Effect.withParentSpan(parentSpan));
|
|
171
|
-
const listen = Stream.fromQueue(listenQueue).pipe(Stream.map(Either.right));
|
|
172
|
-
const closedDeferred = yield* Deferred.make();
|
|
132
|
+
const listen = Stream.fromQueue(listenQueue, { maxChunkSize: 1 }).pipe(Stream.map(Either.right));
|
|
133
|
+
const closedDeferred = yield* Deferred.make().pipe(Effect.acquireRelease(Deferred.done(Exit.void)));
|
|
173
134
|
const webChannel = {
|
|
174
135
|
[WebChannel.WebChannelSymbol]: WebChannel.WebChannelSymbol,
|
|
175
136
|
send,
|
|
@@ -177,7 +138,12 @@ export const makeMessageChannel = ({ nodeName, queue, newConnectionAvailablePubS
|
|
|
177
138
|
closedDeferred,
|
|
178
139
|
supportsTransferables: true,
|
|
179
140
|
schema,
|
|
141
|
+
debugInfo,
|
|
142
|
+
shutdown: Scope.close(scope, Exit.succeed('shutdown')),
|
|
143
|
+
};
|
|
144
|
+
return {
|
|
145
|
+
webChannel: webChannel,
|
|
146
|
+
initialConnectionDeferred,
|
|
180
147
|
};
|
|
181
|
-
|
|
182
|
-
}).pipe(Effect.withSpanScoped('makeMessageChannel'));
|
|
148
|
+
}));
|
|
183
149
|
//# sourceMappingURL=message-channel.js.map
|