-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannel.ts
More file actions
280 lines (218 loc) · 8.98 KB
/
channel.ts
File metadata and controls
280 lines (218 loc) · 8.98 KB
1
2
3
4
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* Channel example: MPMC async message passing
*
* Demonstrates using Channel for producer-consumer patterns,
* backpressure control, and rendezvous synchronization.
*/
import { Channel, type Receiver, type Sender } from '../../../src/mod.ts';
// ============================================================================
// Example 1: Basic send and receive
// ============================================================================
console.log('=== Example 1: Basic send and receive ===\n');
const basicChannel = Channel<string>(10);
await basicChannel.send('hello');
await basicChannel.send('world');
console.log(`Buffer length: ${basicChannel.length}`);
const msg1 = await basicChannel.receive();
const msg2 = await basicChannel.receive();
console.log(`Received: ${msg1.unwrap()}, ${msg2.unwrap()}`);
basicChannel.close();
// ============================================================================
// Example 2: Producer-Consumer pattern with async iterator
// ============================================================================
console.log('\n=== Example 2: Producer-Consumer with async iterator ===\n');
const taskChannel = Channel<number>(5);
// Producer
const producer = (async () => {
for (let i = 1; i <= 10; i++) {
console.log(` Producing: ${i}`);
await taskChannel.send(i);
}
taskChannel.close();
console.log(' Producer finished');
})();
// Consumer using async iterator
const consumer = (async () => {
let sum = 0;
for await (const num of taskChannel) {
console.log(` Consuming: ${num}`);
sum += num;
// Simulate processing time
await new Promise(r => setTimeout(r, 10));
}
console.log(` Consumer finished, sum = ${sum}`);
})();
await Promise.all([producer, consumer]);
// ============================================================================
// Example 3: Bounded channel with backpressure
// ============================================================================
console.log('\n=== Example 3: Bounded channel with backpressure ===\n');
const boundedChannel = Channel<string>(2);
console.log('Buffer capacity: 2');
console.log('Sending 3 messages (third will block until receive)...\n');
// Start sends
const send1 = boundedChannel.send('A').then(() => console.log(' Sent: A'));
const send2 = boundedChannel.send('B').then(() => console.log(' Sent: B'));
const send3 = boundedChannel.send('C').then(() => console.log(' Sent: C (was blocked)'));
// Wait a bit, then start receiving
await new Promise(r => setTimeout(r, 50));
console.log(`\n Buffer length before receive: ${boundedChannel.length}`);
await boundedChannel.receive();
console.log(' Received one message');
await Promise.all([send1, send2, send3]);
console.log(`\n Buffer length after all sends: ${boundedChannel.length}`);
// Drain remaining
while (boundedChannel.length > 0) {
await boundedChannel.receive();
}
boundedChannel.close();
// ============================================================================
// Example 4: Rendezvous channel (direct handoff)
// ============================================================================
console.log('\n=== Example 4: Rendezvous channel (capacity=0) ===\n');
const rendezvous = Channel<string>(0);
console.log('Rendezvous: sender blocks until receiver is ready\n');
// Sender (will block)
const senderDone = rendezvous.send('handoff').then(() => {
console.log(' Sender: handoff complete!');
});
console.log(' Sender started (blocking)...');
await new Promise(r => setTimeout(r, 50));
console.log(' Receiver starting...');
const value = await rendezvous.receive();
console.log(` Receiver got: ${value.unwrap()}`);
await senderDone;
rendezvous.close();
// ============================================================================
// Example 5: Multiple producers, single consumer (MPSC pattern)
// ============================================================================
console.log('\n=== Example 5: Multiple producers (MPSC) ===\n');
interface LogMessage {
source: string;
message: string;
}
const logChannel = Channel<LogMessage>(100);
// Multiple producers (services)
async function serviceLogger(name: string, count: number) {
const sender = logChannel.sender;
for (let i = 1; i <= count; i++) {
await sender.send({ source: name, message: `Log entry ${i}` });
await new Promise(r => setTimeout(r, 5));
}
}
// Single consumer (log writer)
const logs: string[] = [];
const logWriter = (async () => {
const receiver = logChannel.receiver;
for await (const log of receiver) {
logs.push(`[${log.source}] ${log.message}`);
}
})();
// Start multiple producers
await Promise.all([
serviceLogger('auth', 3),
serviceLogger('api', 3),
serviceLogger('db', 3),
]);
logChannel.close();
await logWriter;
console.log('Collected logs:');
logs.forEach(log => console.log(` ${log}`));
// ============================================================================
// Example 6: Multiple consumers (work stealing)
// ============================================================================
console.log('\n=== Example 6: Multiple consumers (work stealing) ===\n');
const workQueue = Channel<{ id: number; data: string; }>(10);
// Fill work queue
for (let i = 1; i <= 6; i++) {
workQueue.trySend({ id: i, data: `task-${i}` });
}
workQueue.close();
console.log('Starting 3 workers to process 6 tasks...\n');
const results: string[] = [];
async function worker(name: string) {
const receiver = workQueue.receiver;
for await (const task of receiver) {
console.log(` ${name} processing task ${task.id}`);
await new Promise(r => setTimeout(r, 20));
results.push(`${name}:${task.id}`);
}
console.log(` ${name} finished`);
}
await Promise.all([
worker('Worker-A'),
worker('Worker-B'),
worker('Worker-C'),
]);
console.log(`\nResults: ${results.join(', ')}`);
// ============================================================================
// Example 7: Non-blocking operations
// ============================================================================
console.log('\n=== Example 7: Non-blocking operations ===\n');
const fastChannel = Channel<number>(2);
// trySend - non-blocking send
console.log('trySend to empty channel:');
console.log(` trySend(1): ${fastChannel.trySend(1)}`);
console.log(` trySend(2): ${fastChannel.trySend(2)}`);
console.log(` trySend(3): ${fastChannel.trySend(3)} (buffer full)`);
// tryReceive - non-blocking receive
console.log('\ntryReceive from channel:');
console.log(` tryReceive(): ${fastChannel.tryReceive().unwrap()}`);
console.log(` tryReceive(): ${fastChannel.tryReceive().unwrap()}`);
console.log(` tryReceive(): ${fastChannel.tryReceive().isNone() ? 'None (empty)' : 'Some'}`);
fastChannel.close();
// ============================================================================
// Example 8: Graceful shutdown
// ============================================================================
console.log('\n=== Example 8: Graceful shutdown ===\n');
const shutdownChannel = Channel<string>(10);
// Long-running consumer
const longConsumer = (async () => {
console.log(' Consumer started');
for await (const msg of shutdownChannel) {
console.log(` Processing: ${msg}`);
await new Promise(r => setTimeout(r, 30));
}
console.log(' Consumer: channel closed, shutting down gracefully');
})();
// Producer sends some messages then closes
await shutdownChannel.send('task-1');
await shutdownChannel.send('task-2');
await shutdownChannel.send('task-3');
console.log(' Closing channel...');
shutdownChannel.close();
// Verify pending sends fail after close
const sendAfterClose = await shutdownChannel.send('task-4');
console.log(` Send after close: ${sendAfterClose ? 'success' : 'failed (expected)'}`);
await longConsumer;
// ============================================================================
// Example 9: Sender/Receiver separation for type safety
// ============================================================================
console.log('\n=== Example 9: Sender/Receiver type separation ===\n');
interface Log {
type: string;
payload: unknown;
};
const typedChannel = Channel<Log>(10);
// Producer only sees Sender interface
async function producerOnly(sender: Sender<Log>) {
await sender.send({ type: 'event', payload: { userId: 123 } });
await sender.send({ type: 'log', payload: 'Hello' });
console.log(' Producer sent 2 messages');
// sender.receive() would be a TypeScript error!
}
// Consumer only sees Receiver interface
async function consumerOnly(receiver: Receiver<Log>) {
let count = 0;
for await (const msg of receiver) {
console.log(` Consumer got: ${msg.type}`);
count++;
}
console.log(` Consumer processed ${count} messages`);
// receiver.send() would be a TypeScript error!
}
const producerTask = producerOnly(typedChannel.sender);
await producerTask;
typedChannel.close();
await consumerOnly(typedChannel.receiver);
console.log('\n=== All examples complete ===\n');