Skip to content

Commit 3efcc05

Browse files
prvykbadrishc
andauthored
Use RESP3 Null in a lot of places where it's appropriate given right protocol version and fix PUNSUBSCRIBE (microsoft#1125)
* Write RESP3 null in many cases where RESP2 null was sent despite protocol version. * Write a response in punsubscribe where no subscriptions exist --------- Co-authored-by: prvyk <github@privatemail.fastmailbox.net> Co-authored-by: Badrish Chandramouli <badrishc@microsoft.com>
1 parent 68bc044 commit 3efcc05

File tree

7 files changed

+42
-22
lines changed

7 files changed

+42
-22
lines changed

libs/server/Resp/ACLCommands.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,7 @@ private bool NetworkAclGetUser()
432432

433433
if (user is null)
434434
{
435-
while (!RespWriteUtils.TryWriteNull(ref dcurr, dend))
436-
SendAndReset();
435+
WriteNull();
437436
}
438437
else
439438
{

libs/server/Resp/BasicCommands.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,8 +1161,7 @@ private bool NetworkCOMMAND_INFO()
11611161
}
11621162
else
11631163
{
1164-
while (!RespWriteUtils.TryWriteNull(ref dcurr, dend))
1165-
SendAndReset();
1164+
WriteNull();
11661165
}
11671166
}
11681167
}

libs/server/Resp/ClientCommands.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,7 @@ private bool NetworkCLIENTGETNAME()
501501

502502
if (string.IsNullOrEmpty(this.clientName))
503503
{
504-
while (!RespWriteUtils.TryWriteNull(ref dcurr, dend))
505-
SendAndReset();
504+
WriteNull();
506505
}
507506
else
508507
{

libs/server/Resp/Objects/ListCommands.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,7 @@ private bool ListBlockingMove(ArgSlice srcKey, ArgSlice dstKey, ArgSlice srcDir,
407407

408408
if (!result.Found)
409409
{
410-
while (!RespWriteUtils.TryWriteNull(ref dcurr, dend))
411-
SendAndReset();
410+
WriteNull();
412411
}
413412
else
414413
{
@@ -779,8 +778,7 @@ private bool ListMove<TGarnetApi>(ref TGarnetApi storageApi)
779778
}
780779
else
781780
{
782-
while (!RespWriteUtils.TryWriteNull(ref dcurr, dend))
783-
SendAndReset();
781+
WriteNull();
784782
}
785783

786784
break;
@@ -823,8 +821,7 @@ private bool ListRightPopLeftPush<TGarnetApi>(ref TGarnetApi storageApi)
823821
}
824822
else
825823
{
826-
while (!RespWriteUtils.TryWriteNull(ref dcurr, dend))
827-
SendAndReset();
824+
WriteNull();
828825
}
829826

830827
break;
@@ -994,8 +991,7 @@ private unsafe bool ListBlockingPopMultiple()
994991

995992
if (!result.Found)
996993
{
997-
while (!RespWriteUtils.TryWriteNull(ref dcurr, dend))
998-
SendAndReset();
994+
WriteNull();
999995
return true;
1000996
}
1001997

libs/server/Resp/Objects/SortedSetCommands.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,7 @@ private unsafe bool SortedSetMPop<TGarnetApi>(ref TGarnetApi storageApi)
483483
if (pairs == null || pairs.Length == 0)
484484
{
485485
// No elements found
486-
while (!RespWriteUtils.TryWriteNull(ref dcurr, dend))
487-
SendAndReset();
486+
WriteNull();
488487
}
489488
else
490489
{
@@ -1568,8 +1567,7 @@ private unsafe bool SortedSetBlockingPop(RespCommand command)
15681567

15691568
if (!result.Found)
15701569
{
1571-
while (!RespWriteUtils.TryWriteNull(ref dcurr, dend))
1572-
SendAndReset();
1570+
WriteNull();
15731571
}
15741572
else
15751573
{
@@ -1675,8 +1673,7 @@ private unsafe bool SortedSetBlockingMPop()
16751673

16761674
if (!result.Found)
16771675
{
1678-
while (!RespWriteUtils.TryWriteNull(ref dcurr, dend))
1679-
SendAndReset();
1676+
WriteNull();
16801677
return true;
16811678
}
16821679

libs/server/Resp/PubSubCommands.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,9 @@ private bool NetworkUNSUBSCRIBE()
284284
SendAndReset();
285285
while (!RespWriteUtils.TryWriteBulkString("unsubscribe"u8, ref dcurr, dend))
286286
SendAndReset();
287-
while (!RespWriteUtils.TryWriteNull(ref dcurr, dend))
288-
SendAndReset();
287+
288+
WriteNull();
289+
289290
while (!RespWriteUtils.TryWriteInt32(numActiveChannels, ref dcurr, dend))
290291
SendAndReset();
291292
}
@@ -360,6 +361,20 @@ private bool NetworkPUNSUBSCRIBE()
360361
SendAndReset();
361362
}
362363

364+
if (channels.Count == 0)
365+
{
366+
while (!RespWriteUtils.TryWriteArrayLength(3, ref dcurr, dend))
367+
SendAndReset();
368+
369+
while (!RespWriteUtils.TryWriteBulkString("punsubscribe"u8, ref dcurr, dend))
370+
SendAndReset();
371+
372+
WriteNull();
373+
374+
while (!RespWriteUtils.TryWriteInt32(0, ref dcurr, dend))
375+
SendAndReset();
376+
}
377+
363378
if (numActiveChannels == 0)
364379
isSubscriptionSession = false;
365380

libs/server/Resp/RespServerSession.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,21 @@ private void WriteDirectLarge(ReadOnlySpan<byte> src)
11921192
}
11931193
}
11941194

1195+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1196+
public void WriteNull()
1197+
{
1198+
if (respProtocolVersion == 3)
1199+
{
1200+
while (!RespWriteUtils.TryWriteResp3Null(ref dcurr, dend))
1201+
SendAndReset();
1202+
}
1203+
else
1204+
{
1205+
while (!RespWriteUtils.TryWriteNull(ref dcurr, dend))
1206+
SendAndReset();
1207+
}
1208+
}
1209+
11951210
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11961211
private void Send(byte* d)
11971212
{

0 commit comments

Comments
 (0)