/
K8sMultiplexer.cs
365 lines (316 loc) · 13.6 KB
/
K8sMultiplexer.cs
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace KubeClient.Extensions.WebSockets
{
using Streams;
/// <summary>
/// A multiplexer / demultiplexer for Kubernetes-style WebSocket streams.
/// </summary>
/// <remarks>
/// Kubernetes streams add a single-byte prefix (indicating the stream index) to each payload (this can be one or more WebSocket packets, until EndOfMessage=<c>true</c>).
///
/// For example, when using the PodV1 exec API, there are up to 3 separate streams: STDIN, STDOUT, and STDERR (with indexes 0, 1, and 2, respectively).
/// </remarks>
public sealed class K8sMultiplexer
: IDisposable
{
/// <summary>
/// The default buffer size used for Kubernetes WebSockets.
/// </summary>
const int DefaultBufferSize = 1024;
/// <summary>
/// Input (read) streams, keyed by stream index.
/// </summary>
readonly Dictionary<byte, K8sMultiplexedReadStream> _inputStreams = new Dictionary<byte, K8sMultiplexedReadStream>();
/// <summary>
/// Output (write) streams, keyed by stream index.
/// </summary>
readonly Dictionary<byte, K8sMultiplexedWriteStream> _outputStreams = new Dictionary<byte, K8sMultiplexedWriteStream>();
/// <summary>
/// Pending write requests from output streams that will be interleaved and written to the WebSocket.
/// </summary>
readonly BlockingCollection<PendingSend> _pendingWrites = new BlockingCollection<PendingSend>(new ConcurrentQueue<PendingSend>());
/// <summary>
/// A source for cancellation tokens used to halt the multiplexer's operation.
/// </summary>
CancellationTokenSource _cancellationSource = new CancellationTokenSource();
/// <summary>
/// A <see cref="Task"/> representing the WebSocket message-receive pump.
/// </summary>
Task _receivePump;
/// <summary>
/// A <see cref="Task"/> representing the WebSocket message-send pump.
/// </summary>
Task _sendPump;
/// <summary>
/// Create a new <see cref="K8sMultiplexer"/>.
/// </summary>
/// <param name="socket">
/// The target WebSocket.
/// </param>
/// <param name="inputStreamCount">
/// The number of of expected input streams.
/// </param>
/// <param name="outputStreamCount">
/// The number of expected output streams.
/// </param>
public K8sMultiplexer(WebSocket socket, byte inputStreamCount, byte outputStreamCount)
{
if (socket == null)
throw new ArgumentNullException(nameof(socket));
if (inputStreamCount == 0 && outputStreamCount == 0)
throw new ArgumentException($"Must specify at least one of {nameof(inputStreamCount)} or {nameof(outputStreamCount)}.");
Socket = socket;
for (byte readStreamIndex = 0; readStreamIndex < inputStreamCount; readStreamIndex++)
_inputStreams[readStreamIndex] = new K8sMultiplexedReadStream(readStreamIndex);
for (byte writeStreamIndex = 0; writeStreamIndex < outputStreamCount; writeStreamIndex++)
_outputStreams[writeStreamIndex] = new K8sMultiplexedWriteStream(writeStreamIndex, EnqueueSend);
}
/// <summary>
/// Dispose of resources being used by the <see cref="K8sMultiplexer"/>.
/// </summary>
public void Dispose()
{
if (_cancellationSource != null)
{
_cancellationSource.Cancel();
_cancellationSource.Dispose();
_cancellationSource = null;
}
if (_receivePump != null)
_receivePump.Wait();
if (_sendPump != null)
_sendPump.Wait();
}
/// <summary>
/// The target WebSocket.
/// </summary>
WebSocket Socket { get; }
/// <summary>
/// The <see cref="CancellationToken"/> used to halt the multiplexer's operation.
/// </summary>
CancellationToken Cancellation => _cancellationSource.Token;
/// <summary>
/// Get the input stream (if defined) with the specified stream index.
/// </summary>
/// <param name="streamIndex">
/// The Kubernetes stream index of the target stream.
/// </param>
/// <returns>
/// The <see cref="Stream"/>, or <c>null</c> if no stream is defined with the specified index.
/// </returns>
public Stream GetInputStream(byte streamIndex)
{
K8sMultiplexedReadStream readStream;
_inputStreams.TryGetValue(streamIndex, out readStream);
return readStream;
}
/// <summary>
/// Get the output stream (if defined) with the specified stream index.
/// </summary>
/// <param name="streamIndex">
/// The Kubernetes stream index of the target stream.
/// </param>
/// <returns>
/// The <see cref="Stream"/>, or <c>null</c> if no stream is defined with the specified index.
/// </returns>
public Stream GetOutputStream(byte streamIndex)
{
K8sMultiplexedWriteStream writeStream;
_outputStreams.TryGetValue(streamIndex, out writeStream);
return writeStream;
}
/// <summary>
/// Start processing stream data.
/// </summary>
public void Start()
{
if (_receivePump != null || _sendPump != null)
throw new InvalidOperationException("Read / write pumps are already running.");
_receivePump = ReceivePump();
_sendPump = SendPump();
}
/// <summary>
/// Enqueue a send operation (asynchronously write data to the outgoing stream).
/// </summary>
/// <param name="data">
/// The data to write.
/// </param>
/// <param name="cancellation">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the operation.
/// </param>
/// <returns>
/// A <see cref="Task"/> representing the asynchronous operation.
/// </returns>
Task EnqueueSend(ArraySegment<byte> data, CancellationToken cancellation)
{
PendingSend pendingWrite = new PendingSend(data, cancellation);
cancellation.Register(
() => pendingWrite.Completion.TrySetCanceled(cancellation)
);
_pendingWrites.Add(pendingWrite);
return pendingWrite.Completion.Task;
}
/// <summary>
/// Receive incoming data from the WebSocket.
/// </summary>
/// <returns>
/// A <see cref="Task"/> representing the pump's operation.
/// </returns>
async Task ReceivePump()
{
await Task.Yield();
ArraySegment<byte> buffer = null;
try
{
while (Socket.State == WebSocketState.Open)
{
buffer = new ArraySegment<byte>(
ArrayPool<byte>.Shared.Rent(minimumLength: DefaultBufferSize)
);
WebSocketReceiveResult readResult = await Socket.ReceiveAsync(buffer, Cancellation);
if (readResult.Count <= 1 && readResult.EndOfMessage)
{
// Effectively an empty packet; ignore.
ArrayPool<byte>.Shared.Return(buffer.Array, clearArray: true);
continue;
}
// Extract stream index.
byte streamIndex = buffer[0];
K8sMultiplexedReadStream readStream;
if (!_inputStreams.TryGetValue(streamIndex, out readStream))
{
// Unknown stream; discard the rest of the message.
while (!readResult.EndOfMessage)
readResult = await Socket.ReceiveAsync(buffer, Cancellation);
ArrayPool<byte>.Shared.Return(buffer.Array, clearArray: true);
continue;
}
// Skip over stream index.
buffer = new ArraySegment<byte>(buffer.Array,
offset: buffer.Offset + 1,
count: readResult.Count - 1
);
readStream.AddPendingRead(buffer);
while (!readResult.EndOfMessage)
{
buffer = new ArraySegment<byte>(
ArrayPool<byte>.Shared.Rent(minimumLength: DefaultBufferSize)
);
readResult = await Socket.ReceiveAsync(buffer, Cancellation);
if (readResult.Count == 0 || readResult.MessageType != WebSocketMessageType.Binary)
{
ArrayPool<byte>.Shared.Return(buffer.Array);
break;
}
buffer = new ArraySegment<byte>(buffer.Array,
offset: buffer.Offset,
count: readResult.Count - 1
);
readStream.AddPendingRead(buffer);
}
}
}
catch (OperationCanceledException)
{
// Clean termination.
if (buffer != null)
ArrayPool<byte>.Shared.Return(buffer.Array);
}
catch (Exception)
{
if (buffer != null)
ArrayPool<byte>.Shared.Return(buffer.Array);
throw;
}
}
/// <summary>
/// Send outgoing data to the WebSocket.
/// </summary>
/// <returns>
/// A <see cref="Task"/> representing the pump's operation.
/// </returns>
async Task SendPump()
{
await Task.Yield();
try
{
while (!Cancellation.IsCancellationRequested)
{
PendingSend pendingWrite;
if (!_pendingWrites.TryTake(out pendingWrite, Timeout.Infinite, Cancellation))
continue;
using (CancellationTokenSource linkedCancellation = CancellationTokenSource.CreateLinkedTokenSource(Cancellation, pendingWrite.Cancellation))
{
try
{
await Socket.SendAsync(pendingWrite.Data,
WebSocketMessageType.Binary,
endOfMessage: true,
cancellationToken: linkedCancellation.Token
);
pendingWrite.Completion.TrySetResult(null);
}
catch (OperationCanceledException sendCancelled) when (sendCancelled.CancellationToken == linkedCancellation.Token)
{
pendingWrite.Completion.TrySetCanceled(sendCancelled.CancellationToken);
}
catch (Exception writeFailed)
{
pendingWrite.Completion.TrySetException(writeFailed);
}
finally
{
ArrayPool<byte>.Shared.Return(pendingWrite.Data.Array, clearArray: true);
}
}
}
}
catch (OperationCanceledException)
{
// Clean termination.
}
}
}
/// <summary>
/// Represents a pending send operation.
/// </summary>
class PendingSend // TODO: Include stream index here and let the multiplexer prepend it instead of doing it in the write-stream.
{
/// <summary>
/// Create a new <see cref="PendingSend"/>.
/// </summary>
/// <param name="data">
/// The data (including stream-index prefix) to be written to the web socket.
/// </param>
/// <param name="cancellation">
/// A cancellation token to that can be used to cancel the send operation.
/// </param>
public PendingSend(ArraySegment<byte> data, CancellationToken cancellation)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
Data = data;
Cancellation = cancellation;
}
/// <summary>
/// The data (including stream-index prefix) to be written to the web socket.
/// </summary>
public ArraySegment<byte> Data { get; }
/// <summary>
/// A cancellation token to that can be used to cancel the send operation.
/// </summary>
public CancellationToken Cancellation { get; }
/// <summary>
/// A <see cref="TaskCompletionSource{TResult}"/> used to represent the asynchronous send operation.
/// </summary>
public TaskCompletionSource<object> Completion { get; } = new TaskCompletionSource<object>();
}
}