Skip to content

Add redis topic prefixing #8229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/HotChocolate/Core/src/Subscriptions.Redis/RedisPubSub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ internal sealed class RedisPubSub : DefaultPubSub
private readonly string _completed;
private readonly int _topicBufferCapacity;
private readonly TopicBufferFullMode _topicBufferFullMode;
private readonly string? _topicPrefix;

public RedisPubSub(
IConnectionMultiplexer connection,
IMessageSerializer serializer,
Expand All @@ -25,6 +27,7 @@ public RedisPubSub(
_topicBufferCapacity = options.TopicBufferCapacity;
_topicBufferFullMode = options.TopicBufferFullMode;
_completed = serializer.CompleteMessage;
_topicPrefix = options.TopicPrefix;
}

protected override async ValueTask OnSendAsync<TMessage>(
Expand All @@ -37,26 +40,36 @@ protected override async ValueTask OnSendAsync<TMessage>(
// The object returned from GetSubscriber is a cheap pass-thru object that does not need
// to be stored.
var subscriber = _connection.GetSubscriber();
await subscriber.PublishAsync(formattedTopic, serialized).ConfigureAwait(false);
await subscriber.PublishAsync(GetPrefixedTopic(formattedTopic), serialized).ConfigureAwait(false);
}

protected override async ValueTask OnCompleteAsync(string formattedTopic)
{
// The object returned from GetSubscriber is a cheap pass-thru object that does not need
// to be stored.
var subscriber = _connection.GetSubscriber();
await subscriber.PublishAsync(formattedTopic, _completed).ConfigureAwait(false);
await subscriber.PublishAsync(GetPrefixedTopic(formattedTopic), _completed).ConfigureAwait(false);
}

protected override DefaultTopic<TMessage> OnCreateTopic<TMessage>(
string formattedTopic,
int? bufferCapacity,
TopicBufferFullMode? bufferFullMode)
=> new RedisTopic<TMessage>(
formattedTopic,
GetPrefixedTopic(formattedTopic),
_connection,
_serializer,
bufferCapacity ?? _topicBufferCapacity,
bufferFullMode ?? _topicBufferFullMode,
DiagnosticEvents);

private string GetPrefixedTopic(string topic)
{
if (string.IsNullOrWhiteSpace(_topicPrefix))
{
return topic;
}

return $"{_topicPrefix}{topic}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using HotChocolate.Execution;
using HotChocolate.Execution.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Squadron;
using StackExchange.Redis;
using Xunit.Abstractions;

namespace HotChocolate.Subscriptions.Redis;

public class RedisTopicPrefixIntegrationTests(RedisResource redisResource, ITestOutputHelper output)
: SubscriptionIntegrationTestBase(output), IClassFixture<RedisResource>
{
private const string TopicPrefix = "prefix:";

[Fact]
public override Task Subscribe_Infer_Topic()
=> base.Subscribe_Infer_Topic();

[Fact]
public override Task Subscribe_Static_Topic()
=> base.Subscribe_Static_Topic();

[Fact]
public override Task Subscribe_Topic_With_Arguments()
=> base.Subscribe_Topic_With_Arguments();

[Fact]
public override Task Subscribe_Topic_With_Arguments_2_Subscriber()
=> base.Subscribe_Topic_With_Arguments_2_Subscriber();

[Fact]
public override Task Subscribe_Topic_With_Arguments_2_Topics()
=> base.Subscribe_Topic_With_Arguments_2_Topics();

[Fact]
public override Task Subscribe_Topic_With_2_Arguments()
=> base.Subscribe_Topic_With_2_Arguments();

[Fact]
public override Task Subscribe_And_Complete_Topic()
=> base.Subscribe_And_Complete_Topic();

[Fact]
public override Task Subscribe_And_Complete_Topic_With_ValueTypeMessage()
=> base.Subscribe_And_Complete_Topic_With_ValueTypeMessage();

[Fact]
public async Task Subscribe_Should_Create_Channel_With_Prefix()
{
using var cts = new CancellationTokenSource(Timeout);
await using var services = CreateServer<Subscription>();

await using var result = await services.ExecuteRequestAsync(
"subscription { onMessage }",
cancellationToken: cts.Token);

var activeChannels = await GetActiveChannelsAsync();

Assert.Contains(activeChannels, channel => channel.ToString()!.StartsWith(TopicPrefix));
}

private async Task<RedisResult[]> GetActiveChannelsAsync()
{
return (RedisResult[])(await redisResource.GetConnection().GetDatabase().ExecuteAsync("PUBSUB", "CHANNELS"))!;
}

protected override void ConfigurePubSub(IRequestExecutorBuilder graphqlBuilder)
=> graphqlBuilder.AddRedisSubscriptions(_ => redisResource.GetConnection(), new SubscriptionOptions { TopicPrefix = TopicPrefix });
}
Loading