Skip to content

Commit

Permalink
Merge pull request #681 from Cysharp/feature/UseConstantMarkerBytes
Browse files Browse the repository at this point in the history
Make marker response bytes a constant
  • Loading branch information
mayuki authored Sep 28, 2023
2 parents 3a8b9c2 + 4582cb5 commit dfaded1
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions src/MagicOnion.Server/Hubs/StreamingHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ namespace MagicOnion.Server.Hubs;
public abstract class StreamingHubBase<THubInterface, TReceiver> : ServiceBase<THubInterface>, IStreamingHub<THubInterface, TReceiver>
where THubInterface : IStreamingHub<THubInterface, TReceiver>
{
static protected readonly Task<Nil> NilTask = Task.FromResult(Nil.Default);
static protected readonly ValueTask CompletedTask = new ValueTask();
protected static readonly Task<Nil> NilTask = Task.FromResult(Nil.Default);
protected static readonly ValueTask CompletedTask = new ValueTask();

static readonly Metadata ResponseHeaders = new Metadata()
{
{ "x-magiconion-streaminghub-version", "2" },
};

// response: [messageId, methodId, response]
// HACK: If the ID of the message is `-1`, the client will ignore the message.
static readonly byte[] MarkerResponseBytes = { 0x93, 0xff, 0x00, 0x0c }; // MsgPack: [-1, 0, nil]

public HubGroupRepository Group { get; private set; } = default!; /* lateinit */

internal StreamingServiceContext<byte[], byte[]> StreamingServiceContext
Expand Down Expand Up @@ -146,23 +150,7 @@ async Task HandleMessageAsync()

// Write a marker that is the beginning of the stream.
// NOTE: To prevent buffering by AWS ALB or reverse-proxy.
static byte[] BuildMarkerResponse()
{
using (var buffer = ArrayPoolBufferWriter.RentThreadStaticWriter())
{
var writer = new MessagePackWriter(buffer);

// response: [messageId, methodId, response]
// HACK: If the ID of the message is `-1`, the client will ignore the message.
writer.WriteArrayHeader(3);
writer.Write(-1);
writer.Write(0);
writer.WriteNil();
writer.Flush();
return buffer.WrittenSpan.ToArray();
}
}
await writer.WriteAsync(BuildMarkerResponse());
await writer.WriteAsync(MarkerResponseBytes);

var handlers = StreamingHubHandlerRepository.GetHandlers(Context.MethodHandler);

Expand Down

0 comments on commit dfaded1

Please sign in to comment.