Skip to content

Commit e89d9b5

Browse files
sirtimidclaude
andcommitted
chore(remotes): extract reuseOrReturnChannel to shared utility
Deduplicate the reuseOrReturnChannel function that existed in both transport.ts and reconnection-orchestrator.ts by extracting it to a new channel-utils.ts module. Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent e733aa7 commit e89d9b5

File tree

3 files changed

+58
-75
lines changed

3 files changed

+58
-75
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import type { ConnectionFactory } from './connection-factory.ts';
2+
import type { PeerRegistry } from './peer-registry.ts';
3+
import type { Channel } from '../types.ts';
4+
5+
/**
6+
* Check if an existing channel exists for a peer, and if so, reuse it.
7+
* Otherwise, return the dialed channel for the caller to register.
8+
*
9+
* @param peerId - The peer ID for the channel.
10+
* @param dialedChannel - The newly dialed channel.
11+
* @param peerRegistry - The peer registry to check for existing channels.
12+
* @param connectionFactory - The connection factory to close channels.
13+
* @returns The channel to use, or null if existing channel died and dialed was closed.
14+
*/
15+
export async function reuseOrReturnChannel(
16+
peerId: string,
17+
dialedChannel: Channel,
18+
peerRegistry: PeerRegistry,
19+
connectionFactory: ConnectionFactory,
20+
): Promise<Channel | null> {
21+
const existingChannel = peerRegistry.getChannel(peerId);
22+
if (existingChannel) {
23+
if (dialedChannel !== existingChannel) {
24+
await connectionFactory.closeChannel(dialedChannel, peerId);
25+
const currentChannel = peerRegistry.getChannel(peerId);
26+
if (currentChannel === existingChannel) {
27+
return existingChannel;
28+
}
29+
if (currentChannel) {
30+
return currentChannel;
31+
}
32+
return null;
33+
}
34+
const currentChannel = peerRegistry.getChannel(peerId);
35+
if (currentChannel === existingChannel) {
36+
return existingChannel;
37+
}
38+
if (currentChannel) {
39+
return currentChannel;
40+
}
41+
return null;
42+
}
43+
return dialedChannel;
44+
}

packages/ocap-kernel/src/remotes/platform/reconnection-orchestrator.ts

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import type { Logger } from '@metamask/logger';
77
import { fromString } from 'uint8arrays';
88

9+
import { reuseOrReturnChannel } from './channel-utils.ts';
910
import type { ConnectionFactory } from './connection-factory.ts';
1011
import type { MessageQueue } from './message-queue.ts';
1112
import type { PeerRegistry } from './peer-registry.ts';
@@ -111,43 +112,6 @@ export function makeReconnectionOrchestrator(
111112
}
112113
}
113114

114-
/**
115-
* Check if an existing channel exists for a peer, and if so, reuse it.
116-
* Otherwise, return the dialed channel for the caller to register.
117-
*
118-
* @param peerId - The peer ID for the channel.
119-
* @param dialedChannel - The newly dialed channel.
120-
* @returns The channel to use, or null if existing channel died and dialed was closed.
121-
*/
122-
async function reuseOrReturnChannel(
123-
peerId: string,
124-
dialedChannel: Channel,
125-
): Promise<Channel | null> {
126-
const existingChannel = peerRegistry.getChannel(peerId);
127-
if (existingChannel) {
128-
if (dialedChannel !== existingChannel) {
129-
await connectionFactory.closeChannel(dialedChannel, peerId);
130-
const currentChannel = peerRegistry.getChannel(peerId);
131-
if (currentChannel === existingChannel) {
132-
return existingChannel;
133-
}
134-
if (currentChannel) {
135-
return currentChannel;
136-
}
137-
return null;
138-
}
139-
const currentChannel = peerRegistry.getChannel(peerId);
140-
if (currentChannel === existingChannel) {
141-
return existingChannel;
142-
}
143-
if (currentChannel) {
144-
return currentChannel;
145-
}
146-
return null;
147-
}
148-
return dialedChannel;
149-
}
150-
151115
/**
152116
* Handle connection loss for a given peer ID.
153117
* Skips reconnection if the peer was intentionally closed.
@@ -244,7 +208,12 @@ export function makeReconnectionOrchestrator(
244208

245209
queue = peerRegistry.getMessageQueue(peerId);
246210

247-
channel = await reuseOrReturnChannel(peerId, channel);
211+
channel = await reuseOrReturnChannel(
212+
peerId,
213+
channel,
214+
peerRegistry,
215+
connectionFactory,
216+
);
248217
if (channel === null) {
249218
logger.log(
250219
`${peerId}:: existing channel died during reuse check, continuing reconnection loop`,

packages/ocap-kernel/src/remotes/platform/transport.ts

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Logger } from '@metamask/logger';
44
import { fromString } from 'uint8arrays';
55

66
import { makeChannelReader } from './channel-reader.ts';
7+
import { reuseOrReturnChannel } from './channel-utils.ts';
78
import { ConnectionFactory } from './connection-factory.ts';
89
import { PeerRegistry } from './peer-registry.ts';
910
import { makeReconnectionOrchestrator } from './reconnection-orchestrator.ts';
@@ -246,42 +247,6 @@ export async function initNetwork(
246247
outputError,
247248
});
248249

249-
/**
250-
* Check if an existing channel exists for a peer, and if so, reuse it.
251-
*
252-
* @param peerId - The peer ID for the channel.
253-
* @param dialedChannel - The newly dialed channel.
254-
* @returns The channel to use, or null if existing channel died and dialed was closed.
255-
*/
256-
async function reuseOrReturnChannel(
257-
peerId: string,
258-
dialedChannel: Channel,
259-
): Promise<Channel | null> {
260-
const existingChannel = peerRegistry.getChannel(peerId);
261-
if (existingChannel) {
262-
if (dialedChannel !== existingChannel) {
263-
await connectionFactory.closeChannel(dialedChannel, peerId);
264-
const currentChannel = peerRegistry.getChannel(peerId);
265-
if (currentChannel === existingChannel) {
266-
return existingChannel;
267-
}
268-
if (currentChannel) {
269-
return currentChannel;
270-
}
271-
return null;
272-
}
273-
const currentChannel = peerRegistry.getChannel(peerId);
274-
if (currentChannel === existingChannel) {
275-
return existingChannel;
276-
}
277-
if (currentChannel) {
278-
return currentChannel;
279-
}
280-
return null;
281-
}
282-
return dialedChannel;
283-
}
284-
285250
/**
286251
* Clean up stale peer data for peers inactive for more than stalePeerTimeoutMs.
287252
*/
@@ -361,7 +326,12 @@ export async function initNetwork(
361326
return;
362327
}
363328

364-
channel = await reuseOrReturnChannel(targetPeerId, channel);
329+
channel = await reuseOrReturnChannel(
330+
targetPeerId,
331+
channel,
332+
peerRegistry,
333+
connectionFactory,
334+
);
365335
if (channel === null) {
366336
logger.log(
367337
`${targetPeerId}:: existing channel died during reuse check, triggering reconnection`,

0 commit comments

Comments
 (0)