Skip to content

Commit f1fa2ed

Browse files
committed
use Channel.TryWrite when re-queuing messages
1 parent 91d2e78 commit f1fa2ed

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/HotChocolate/Core/src/Subscriptions.Postgres/PostgresChannelWriter.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System.Data;
21
using System.Threading.Channels;
32
using HotChocolate.Subscriptions.Diagnostics;
43
using Npgsql;
4+
using NpgsqlTypes;
55
using static HotChocolate.Subscriptions.Postgres.PostgresResources;
66

77
namespace HotChocolate.Subscriptions.Postgres;
@@ -136,9 +136,20 @@ private async Task HandleMessage(NpgsqlConnection connection, CancellationToken
136136
_diagnosticEvents.ProviderInfo(msg);
137137

138138
// if we cannot send the message we put it back into the channel
139+
// however as the channel is bounded, we might not able to requeue the message and will be forced to drop them if they can't be written
140+
var failedCount = 0;
141+
139142
foreach (var message in messages)
140143
{
141-
await _channel.Writer.WriteAsync(message, CancellationToken.None);
144+
if (!_channel.Writer.TryWrite(message))
145+
{
146+
failedCount++;
147+
}
148+
}
149+
150+
if (failedCount > 0)
151+
{
152+
_diagnosticEvents.ProviderInfo(string.Format(ChannelWriter_FailedToRequeueMessage, failedCount));
142153
}
143154
}
144155
}

src/HotChocolate/Core/src/Subscriptions.Postgres/Properties/PostgresResources.Designer.cs

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/HotChocolate/Core/src/Subscriptions.Postgres/Properties/PostgresResources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,7 @@
5555
<data name="PostgresMessageEnvelope_PayloadTooLarge" xml:space="preserve">
5656
<value>Payload is too long to we written to Postgres. Serialized message is {0} bytes but limit is {1} bytes</value>
5757
</data>
58+
<data name="ChannelWriter_FailedToRequeueMessage" xml:space="preserve">
59+
<value>The postgres writer was unable to requeue messages. {0} messages have been lost</value>
60+
</data>
5861
</root>

0 commit comments

Comments
 (0)