-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathMessageWriter.cs
More file actions
666 lines (582 loc) · 23.2 KB
/
Copy pathMessageWriter.cs
File metadata and controls
666 lines (582 loc) · 23.2 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
using System;
using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using RESPite;
using RESPite.Internal;
using RESPite.Messages;
namespace StackExchange.Redis;
internal readonly ref struct MessageWriter
{
private readonly CommandMap _map;
private readonly byte[]? _channelPrefix;
public MessageWriter(byte[]? channelPrefix, CommandMap? map, IBufferWriter<byte> writer)
{
// ReSharper disable once NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract
_map = map ?? CommandMap.Default;
_channelPrefix = channelPrefix;
_writer = writer;
}
public static IBufferWriter<byte> BlockBuffer => BlockBufferSerializer.Shared;
public MessageWriter(PhysicalConnection connection, IBufferWriter<byte> writer)
{
if (connection.BridgeCouldBeNull is { } bridge)
{
_map = bridge.Multiplexer.CommandMap;
_channelPrefix = connection.ChannelPrefix;
}
else
{
_map = CommandMap.Default;
_channelPrefix = null;
}
_writer = writer ?? connection.Output;
}
private readonly IBufferWriter<byte> _writer;
public static ReadOnlyMemory<byte> FlushBlockBuffer() =>
BlockBufferSerializer.BlockBuffer.FinalizeMessage(BlockBufferSerializer.Shared);
public static void RevertBlockBuffer() => BlockBufferSerializer.Shared.Revert();
public static void ReleaseBlockBuffer(ReadOnlyMemory<byte> memory)
{
if (MemoryMarshal.TryGetMemoryManager<byte, BlockBufferSerializer.BlockBuffer>(
memory, out var block))
{
block.Release();
}
}
public static void ReleaseBlockBuffer(in ReadOnlySequence<byte> request) =>
BlockBufferSerializer.BlockBuffer.Release(in request);
public void Write(in RedisKey key)
{
var val = key.KeyValue;
if (val is string s)
{
WriteUnifiedPrefixedString(_writer, key.KeyPrefix, s);
}
else
{
WriteUnifiedPrefixedBlob(_writer, key.KeyPrefix, (byte[]?)val);
}
}
internal void Write(in RedisChannel channel)
=> WriteUnifiedPrefixedBlob(_writer, channel.IgnoreChannelPrefix ? null : _channelPrefix, channel.Value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void WriteBulkString(in RedisValue value)
=> WriteBulkString(value, _writer);
internal static void WriteBulkString(in RedisValue value, IBufferWriter<byte> writer)
{
switch (value.Type)
{
case RedisValue.StorageType.Null:
WriteUnifiedBlob(writer, (byte[]?)null);
break;
case RedisValue.StorageType.Int64:
WriteUnifiedInt64(writer, value.OverlappedValueInt64);
break;
case RedisValue.StorageType.UInt64:
WriteUnifiedUInt64(writer, value.OverlappedValueUInt64);
break;
case RedisValue.StorageType.Double:
WriteUnifiedDouble(writer, value.OverlappedValueDouble);
break;
case RedisValue.StorageType.String:
WriteUnifiedPrefixedString(writer, null, value.RawString());
break;
case RedisValue.StorageType.MemoryManager or RedisValue.StorageType.ByteArray or RedisValue.StorageType.ShortBlob:
WriteUnifiedSpan(writer, value.UnsafeRawSpan(out _));
break;
case RedisValue.StorageType.Sequence:
WriteUnifiedSequenceIterator(writer, value.RawSequenceIterator());
break;
default:
throw new InvalidOperationException($"Unexpected {value.Type} value: '{value}'");
}
}
internal void WriteBulkString(ReadOnlySpan<byte> value) => WriteUnifiedSpan(_writer, value);
internal const int
REDIS_MAX_ARGS =
1024 * 1024; // there is a <= 1024*1024 max constraint inside redis itself: https://github.com/antirez/redis/blob/6c60526db91e23fb2d666fc52facc9a11780a2a3/src/networking.c#L1024
internal void WriteHeader(string command, int arguments)
{
byte[]? lease = null;
try
{
int bytes = Encoding.ASCII.GetMaxByteCount(command.Length);
Span<byte> buffer = command.Length <= 32 ? stackalloc byte[32] : (lease = ArrayPool<byte>.Shared.Rent(bytes));
bytes = Encoding.ASCII.GetBytes(command, buffer);
var span = buffer.Slice(0, bytes);
AsciiHash.ToUpper(span);
WriteHeader(RedisCommand.UNKNOWN, arguments, span);
}
finally
{
if (lease is not null) ArrayPool<byte>.Shared.Return(lease);
}
}
internal void WriteHeader(RedisCommand command, int arguments)
{
// using >= here because we will be adding 1 for the command itself (which is an arg for the purposes of the multi-bulk protocol)
if (arguments >= REDIS_MAX_ARGS) throw ExceptionFactory.TooManyArgs(command.ToString(), arguments);
// in theory we should never see this; CheckMessage dealt with "regular" messages, and
// ExecuteMessage should have dealt with everything else
var commandBytes = _map.GetResp(command);
if (commandBytes.IsEmpty) throw ExceptionFactory.CommandDisabled(command);
// *{argCount}\r\n = 3 + MaxInt32TextLen
// ${cmd-len}\r\n = precomputed
// {cmd}\r\n = precomputed
var span = _writer.GetSpan(commandBytes.Length + 3 + Format.MaxInt32TextLen);
span[0] = (byte)'*';
int offset = WriteRaw(span, arguments + 1, offset: 1);
commandBytes.CopyTo(span.Slice(offset));
offset += commandBytes.Length;
_writer.Advance(offset);
}
internal void WriteHeader(RedisCommand command, int arguments, ReadOnlySpan<byte> commandBytes)
{
// using >= here because we will be adding 1 for the command itself (which is an arg for the purposes of the multi-bulk protocol)
if (arguments >= REDIS_MAX_ARGS) throw ExceptionFactory.TooManyArgs(command.ToString(), arguments);
// in theory we should never see this; CheckMessage dealt with "regular" messages, and
// ExecuteMessage should have dealt with everything else
if (commandBytes.IsEmpty) throw ExceptionFactory.CommandDisabled(command);
// *{argCount}\r\n = 3 + MaxInt32TextLen
// ${cmd-len}\r\n = 3 + MaxInt32TextLen
// {cmd}\r\n = 2 + commandBytes.Length
var span = _writer.GetSpan(commandBytes.Length + 8 + Format.MaxInt32TextLen + Format.MaxInt32TextLen);
span[0] = (byte)'*';
int offset = WriteRaw(span, arguments + 1, offset: 1);
span[offset++] = (byte)'$';
offset = AppendToSpan(span, commandBytes, offset: offset);
_writer.Advance(offset);
}
internal static void WriteMultiBulkHeader(IBufferWriter<byte> writer, long count)
{
// *{count}\r\n = 3 + MaxInt32TextLen
var span = writer.GetSpan(3 + Format.MaxInt32TextLen);
span[0] = (byte)'*';
int offset = WriteRaw(span, count, offset: 1);
writer.Advance(offset);
}
internal static void WriteMultiBulkHeader(IBufferWriter<byte> writer, long count, RespPrefix prefix)
{
// *{count}\r\n = 3 + MaxInt32TextLen
var span = writer.GetSpan(3 + Format.MaxInt32TextLen);
span[0] = (byte)prefix;
if ((prefix is RespPrefix.Map or RespPrefix.Attribute) & count > 0)
{
if ((count & 1) != 0) Throw(prefix, count);
count >>= 1;
static void Throw(RespPrefix type, long count) => throw new ArgumentOutOfRangeException(
paramName: nameof(count),
message: $"{type} data must be in pairs; got {count}");
}
int offset = WriteRaw(span, count, offset: 1);
writer.Advance(offset);
}
private static ReadOnlySpan<byte> NullBulkString => "$-1\r\n"u8;
private static ReadOnlySpan<byte> EmptyBulkString => "$0\r\n\r\n"u8;
internal static void WriteUnifiedPrefixedString(IBufferWriter<byte> writer, byte[]? prefix, string? value)
{
if (value == null)
{
// special case
writer.Write(NullBulkString);
}
else
{
// ${total-len}\r\n 3 + MaxInt32TextLen
// {prefix}{value}\r\n
int encodedLength = Encoding.UTF8.GetByteCount(value),
prefixLength = prefix?.Length ?? 0,
totalLength = prefixLength + encodedLength;
if (totalLength == 0)
{
// special-case
writer.Write(EmptyBulkString);
}
else
{
var span = writer.GetSpan(3 + Format.MaxInt32TextLen);
span[0] = (byte)'$';
int bytes = WriteRaw(span, totalLength, offset: 1);
writer.Advance(bytes);
if (prefixLength != 0) writer.Write(prefix);
if (encodedLength != 0) WriteRaw(writer, value, encodedLength);
WriteCrlf(writer);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int WriteCrlf(Span<byte> span, int offset)
{
span[offset++] = (byte)'\r';
span[offset++] = (byte)'\n';
return offset;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void WriteCrlf(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(2);
span[0] = (byte)'\r';
span[1] = (byte)'\n';
writer.Advance(2);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void WriteRaw(ReadOnlySpan<byte> value) => _writer.Write(value);
internal static int WriteRaw(Span<byte> span, long value, bool withLengthPrefix = false, int offset = 0)
{
if (value >= 0 && value <= 9)
{
if (withLengthPrefix)
{
span[offset++] = (byte)'1';
offset = WriteCrlf(span, offset);
}
span[offset++] = (byte)((int)'0' + (int)value);
}
else if (value >= 10 && value < 100)
{
if (withLengthPrefix)
{
span[offset++] = (byte)'2';
offset = WriteCrlf(span, offset);
}
span[offset++] = (byte)((int)'0' + ((int)value / 10));
span[offset++] = (byte)((int)'0' + ((int)value % 10));
}
else if (value >= 100 && value < 1000)
{
int v = (int)value;
int units = v % 10;
v /= 10;
int tens = v % 10, hundreds = v / 10;
if (withLengthPrefix)
{
span[offset++] = (byte)'3';
offset = WriteCrlf(span, offset);
}
span[offset++] = (byte)((int)'0' + hundreds);
span[offset++] = (byte)((int)'0' + tens);
span[offset++] = (byte)((int)'0' + units);
}
else if (value < 0 && value >= -9)
{
if (withLengthPrefix)
{
span[offset++] = (byte)'2';
offset = WriteCrlf(span, offset);
}
span[offset++] = (byte)'-';
span[offset++] = (byte)((int)'0' - (int)value);
}
else if (value <= -10 && value > -100)
{
if (withLengthPrefix)
{
span[offset++] = (byte)'3';
offset = WriteCrlf(span, offset);
}
value = -value;
span[offset++] = (byte)'-';
span[offset++] = (byte)((int)'0' + ((int)value / 10));
span[offset++] = (byte)((int)'0' + ((int)value % 10));
}
else
{
// we're going to write it, but *to the wrong place*
var availableChunk = span.Slice(offset);
var formattedLength = Format.FormatInt64(value, availableChunk);
if (withLengthPrefix)
{
// now we know how large the prefix is: write the prefix, then write the value
var prefixLength = Format.FormatInt32(formattedLength, availableChunk);
offset += prefixLength;
offset = WriteCrlf(span, offset);
availableChunk = span.Slice(offset);
var finalLength = Format.FormatInt64(value, availableChunk);
offset += finalLength;
Debug.Assert(finalLength == formattedLength);
}
else
{
offset += formattedLength;
}
}
return WriteCrlf(span, offset);
}
[ThreadStatic]
private static Encoder? s_PerThreadEncoder;
internal static Encoder GetPerThreadEncoder()
{
var encoder = s_PerThreadEncoder;
if (encoder == null)
{
s_PerThreadEncoder = encoder = Encoding.UTF8.GetEncoder();
}
else
{
encoder.Reset();
}
return encoder;
}
internal static unsafe void WriteRaw(IBufferWriter<byte> writer, string value, int expectedLength)
{
const int MaxQuickEncodeSize = 512;
fixed (char* cPtr = value)
{
int totalBytes;
if (expectedLength <= MaxQuickEncodeSize)
{
// encode directly in one hit
var span = writer.GetSpan(expectedLength);
fixed (byte* bPtr = &MemoryMarshal.GetReference(span))
{
totalBytes = Encoding.UTF8.GetBytes(
cPtr,
value.Length,
bPtr,
expectedLength);
}
writer.Advance(expectedLength);
}
else
{
// use an encoder in a loop
var encoder = GetPerThreadEncoder();
int charsRemaining = value.Length, charOffset = 0;
totalBytes = 0;
bool final = false;
while (true)
{
var span = writer
.GetSpan(5); // get *some* memory - at least enough for 1 character (but hopefully lots more)
int charsUsed, bytesUsed;
bool completed;
fixed (byte* bPtr = &MemoryMarshal.GetReference(span))
{
encoder.Convert(
cPtr + charOffset,
charsRemaining,
bPtr,
span.Length,
final,
out charsUsed,
out bytesUsed,
out completed);
}
writer.Advance(bytesUsed);
totalBytes += bytesUsed;
charOffset += charsUsed;
charsRemaining -= charsUsed;
if (charsRemaining <= 0)
{
if (charsRemaining < 0) throw new InvalidOperationException("String encode went negative");
if (completed) break; // fine
if (final) throw new InvalidOperationException("String encode failed to complete");
final = true; // flush the encoder to one more span, then exit
}
}
}
if (totalBytes != expectedLength) throw new InvalidOperationException("String encode length check failure");
}
}
private static void WriteUnifiedPrefixedBlob(IBufferWriter<byte> writer, byte[]? prefix, byte[]? value)
{
// ${total-len}\r\n
// {prefix}{value}\r\n
if (prefix == null || prefix.Length == 0 || value == null)
{
// if no prefix, just use the non-prefixed version;
// even if prefixed, a null value writes as null, so can use the non-prefixed version
WriteUnifiedBlob(writer, value);
}
else
{
var span = writer.GetSpan(3 +
Format
.MaxInt32TextLen); // note even with 2 max-len, we're still in same text range
span[0] = (byte)'$';
int bytes = WriteRaw(span, prefix.LongLength + value.LongLength, offset: 1);
writer.Advance(bytes);
writer.Write(prefix);
writer.Write(value);
span = writer.GetSpan(2);
WriteCrlf(span, 0);
writer.Advance(2);
}
}
private static void WriteUnifiedInt64(IBufferWriter<byte> writer, long value)
{
// note from specification: A client sends to the Redis server a RESP Array consisting of just Bulk Strings.
// (i.e. we can't just send ":123\r\n", we need to send "$3\r\n123\r\n"
// ${asc-len}\r\n = 4/5 (asc-len at most 2 digits)
// {asc}\r\n = MaxInt64TextLen + 2
var span = writer.GetSpan(7 + Format.MaxInt64TextLen);
span[0] = (byte)'$';
var bytes = WriteRaw(span, value, withLengthPrefix: true, offset: 1);
writer.Advance(bytes);
}
private static void WriteUnifiedUInt64(IBufferWriter<byte> writer, ulong value)
{
// note from specification: A client sends to the Redis server a RESP Array consisting of just Bulk Strings.
// (i.e. we can't just send ":123\r\n", we need to send "$3\r\n123\r\n"
Span<byte> valueSpan = stackalloc byte[Format.MaxInt64TextLen];
var len = Format.FormatUInt64(value, valueSpan);
// ${asc-len}\r\n = 4/5 (asc-len at most 2 digits)
// {asc}\r\n = {len} + 2
var span = writer.GetSpan(7 + len);
span[0] = (byte)'$';
int offset = WriteRaw(span, len, withLengthPrefix: false, offset: 1);
valueSpan.Slice(0, len).CopyTo(span.Slice(offset));
offset += len;
offset = WriteCrlf(span, offset);
writer.Advance(offset);
}
private static void WriteUnifiedDouble(IBufferWriter<byte> writer, double value)
{
#if NET8_0_OR_GREATER
Span<byte> valueSpan = stackalloc byte[Format.MaxDoubleTextLen];
var len = Format.FormatDouble(value, valueSpan);
// ${asc-len}\r\n = 4/5 (asc-len at most 2 digits)
// {asc}\r\n = {len} + 2
var span = writer.GetSpan(7 + len);
span[0] = (byte)'$';
int offset = WriteRaw(span, len, withLengthPrefix: false, offset: 1);
valueSpan.Slice(0, len).CopyTo(span.Slice(offset));
offset += len;
offset = WriteCrlf(span, offset);
writer.Advance(offset);
#else
// fallback: drop to string
WriteUnifiedPrefixedString(writer, null, Format.ToString(value));
#endif
}
internal static void WriteInteger(IBufferWriter<byte> writer, long value)
{
// note: client should never write integer; only server does this
// :{asc}\r\n = MaxInt64TextLen + 3
var span = writer.GetSpan(3 + Format.MaxInt64TextLen);
span[0] = (byte)':';
var bytes = WriteRaw(span, value, withLengthPrefix: false, offset: 1);
writer.Advance(bytes);
}
private static void WriteUnifiedBlob(IBufferWriter<byte> writer, byte[]? value)
{
if (value is null)
{
// special case:
writer.Write(NullBulkString);
}
else
{
WriteUnifiedSpan(writer, new ReadOnlySpan<byte>(value));
}
}
private static void WriteUnifiedSpan(IBufferWriter<byte> writer, ReadOnlySpan<byte> value)
{
// ${len}\r\n = 3 + MaxInt32TextLen
// {value}\r\n = 2 + value.Length
const int MaxQuickSpanSize = 512;
if (value.Length == 0)
{
// special case:
writer.Write(EmptyBulkString);
}
else if (value.Length <= MaxQuickSpanSize)
{
var span = writer.GetSpan(5 + Format.MaxInt32TextLen + value.Length);
span[0] = (byte)'$';
int bytes = AppendToSpan(span, value, 1);
writer.Advance(bytes);
}
else
{
// too big to guarantee can do in a single span
var span = writer.GetSpan(3 + Format.MaxInt32TextLen);
span[0] = (byte)'$';
int bytes = WriteRaw(span, value.Length, offset: 1);
writer.Advance(bytes);
writer.Write(value);
WriteCrlf(writer);
}
}
/*
private static void WriteUnifiedSequence(IBufferWriter<byte> writer, in ReadOnlySequence<byte> value)
{
if (value.IsSingleSegment)
{
WriteUnifiedSpan(writer, value.FirstSpan);
}
else
{
// value.Length is a long, so reserve room for a 64-bit length ('$' + up to 20 digits + CRLF)
var span = writer.GetSpan(3 + Format.MaxInt64TextLen);
span[0] = (byte)'$';
int bytes = WriteRaw(span, value.Length, offset: 1);
writer.Advance(bytes);
foreach (var memory in value)
{
writer.Write(memory.Span);
}
WriteCrlf(writer);
}
}
*/
private static void WriteUnifiedSequenceIterator(IBufferWriter<byte> writer, ReadOnlySequenceSegmentIterator<byte> seq)
{
var span = writer.GetSpan(3 + Format.MaxInt32TextLen);
span[0] = (byte)'$';
int bytes = WriteRaw(span, seq.Length, offset: 1);
writer.Advance(bytes);
while (seq.TryNext(out var memory))
{
writer.Write(memory.Span);
}
WriteCrlf(writer);
}
private static int AppendToSpan(Span<byte> span, ReadOnlySpan<byte> value, int offset = 0)
{
offset = WriteRaw(span, value.Length, offset: offset);
value.CopyTo(span.Slice(offset, value.Length));
offset += value.Length;
return WriteCrlf(span, offset);
}
internal void WriteSha1AsHex(byte[]? value)
{
var writer = _writer;
if (value is null)
{
writer.Write(NullBulkString);
}
else if (value.Length == ResultProcessor.ScriptLoadProcessor.Sha1HashLength)
{
// $40\r\n = 5
// {40 bytes}\r\n = 42
var span = writer.GetSpan(47);
span[0] = (byte)'$';
span[1] = (byte)'4';
span[2] = (byte)'0';
span[3] = (byte)'\r';
span[4] = (byte)'\n';
int offset = 5;
for (int i = 0; i < value.Length; i++)
{
var b = value[i];
span[offset++] = ToHexNibble(b >> 4);
span[offset++] = ToHexNibble(b & 15);
}
span[offset++] = (byte)'\r';
span[offset++] = (byte)'\n';
writer.Advance(offset);
}
else
{
throw new InvalidOperationException("Invalid SHA1 length: " + value.Length);
}
}
internal static byte ToHexNibble(int value)
{
return value < 10 ? (byte)('0' + value) : (byte)('a' - 10 + value);
}
}