-
-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Redis Provider for Subscriptions (#902)
- Loading branch information
1 parent
6e40789
commit 0cf04dd
Showing
26 changed files
with
672 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("HotChocolate.Subscriptions.Tests")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Threading.Tasks; | ||
using StackExchange.Redis; | ||
|
||
namespace HotChocolate.Subscriptions.Redis | ||
{ | ||
public class RedisEventRegistry | ||
: IEventRegistry | ||
, IEventSender | ||
{ | ||
private readonly IConnectionMultiplexer _connection; | ||
private readonly IPayloadSerializer _serializer; | ||
|
||
public RedisEventRegistry( | ||
IConnectionMultiplexer connection, | ||
IPayloadSerializer serializer) | ||
{ | ||
_connection = connection; | ||
_serializer = serializer; | ||
} | ||
|
||
public async Task<IEventStream> SubscribeAsync( | ||
IEventDescription eventDescription) | ||
{ | ||
ISubscriber subscriber = _connection.GetSubscriber(); | ||
|
||
ChannelMessageQueue channel = await subscriber | ||
.SubscribeAsync(eventDescription.ToString()); | ||
|
||
return new RedisEventStream(eventDescription, channel, _serializer); | ||
} | ||
|
||
public async Task SendAsync(IEventMessage message) | ||
{ | ||
ISubscriber subscriber = _connection | ||
.GetSubscriber(); | ||
|
||
string channel = message.Event.ToString(); | ||
byte[] payload = _serializer.Serialize(message.Payload); | ||
|
||
await subscriber.PublishAsync(channel, payload); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using StackExchange.Redis; | ||
|
||
namespace HotChocolate.Subscriptions.Redis | ||
{ | ||
public class RedisEventStream : IEventStream | ||
{ | ||
private readonly IEventDescription _eventDescription; | ||
private readonly ChannelMessageQueue _channel; | ||
private readonly IPayloadSerializer _serializer; | ||
private bool _isCompleted; | ||
|
||
public RedisEventStream( | ||
IEventDescription eventDescription, | ||
ChannelMessageQueue channel, | ||
IPayloadSerializer serializer) | ||
{ | ||
_eventDescription = eventDescription; | ||
_channel = channel; | ||
_serializer = serializer; | ||
} | ||
|
||
public bool IsCompleted => _isCompleted; | ||
|
||
public Task<IEventMessage> ReadAsync() => | ||
ReadAsync(CancellationToken.None); | ||
|
||
public async Task<IEventMessage> ReadAsync( | ||
CancellationToken cancellationToken) | ||
{ | ||
ChannelMessage message = await _channel | ||
.ReadAsync(cancellationToken); | ||
|
||
var payload = _serializer.Deserialize(message.Message); | ||
|
||
return new EventMessage(message.Channel, payload); | ||
} | ||
|
||
public async Task CompleteAsync() | ||
{ | ||
if (!_isCompleted) | ||
{ | ||
await _channel.UnsubscribeAsync(); | ||
_isCompleted = true; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
public void Dispose(bool disposing) | ||
{ | ||
if (!_isCompleted) | ||
{ | ||
if (disposing) | ||
{ | ||
_channel.Unsubscribe(); | ||
} | ||
_isCompleted = true; | ||
} | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Core/Subscriptions.Redis/RedisSubscriptionServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using HotChocolate.Subscriptions.Redis; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using StackExchange.Redis; | ||
|
||
namespace HotChocolate.Subscriptions | ||
{ | ||
public static class RedisSubscriptionServiceCollectionExtensions | ||
{ | ||
public static void AddRedisSubscriptionProvider( | ||
this IServiceCollection services, | ||
ConfigurationOptions options) => | ||
services | ||
.AddRedisSubscriptionProvider<JsonPayloadSerializer>(options); | ||
|
||
public static void AddRedisSubscriptionProvider<TSerializer>( | ||
this IServiceCollection services, | ||
ConfigurationOptions options) | ||
where TSerializer : class, IPayloadSerializer | ||
{ | ||
if (services == null) | ||
{ | ||
throw new ArgumentNullException(nameof(services)); | ||
} | ||
|
||
services | ||
.AddSingleton<IPayloadSerializer, TSerializer>() | ||
.AddSingleton<IConnectionMultiplexer>(sp => | ||
ConnectionMultiplexer.Connect(options)) | ||
.AddSingleton<RedisEventRegistry>() | ||
.AddSingleton<IEventRegistry>(sp => | ||
sp.GetRequiredService<RedisEventRegistry>()) | ||
.AddSingleton<IEventSender>(sp => | ||
sp.GetRequiredService<RedisEventRegistry>()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">netstandard2.0</TargetFrameworks> | ||
<TargetFrameworks Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">netstandard2.0; net461</TargetFrameworks> | ||
<AssemblyName>HotChocolate.Subscriptions.Redis</AssemblyName> | ||
<RootNamespace>HotChocolate.Subscriptions.Redis</RootNamespace> | ||
<PackageId>HotChocolate.Subscriptions.Redis</PackageId> | ||
<Description>Contains a Redis implementation for a Hot Chocolate GraphQL subscription provider.</Description> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<DebugType>portable</DebugType> | ||
<DebugSymbols>true</DebugSymbols> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> | ||
<DebugType>pdbonly</DebugType> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.0" /> | ||
<PackageReference Include="StackExchange.Redis" Version="2.0.601" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Subscriptions\Subscriptions.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Core/Subscriptions.Tests/InMemory/InMemoryEventStreamTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/Core/Subscriptions.Tests/JsonPayloadSerializerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace HotChocolate.Subscriptions | ||
{ | ||
public class JsonPayloadSerializerTests | ||
{ | ||
[Fact] | ||
public void GivenSerializer_WhenSerialize_ContentIsValid() | ||
{ | ||
// arrange | ||
var serializer = new JsonPayloadSerializer(); | ||
var payload = "Foo"; | ||
|
||
// act | ||
var encoded = serializer.Serialize(payload); | ||
|
||
// assert | ||
var expected = new byte[] { 34, 70, 111, 111, 34 }; | ||
Assert.Equal(expected, encoded); | ||
} | ||
|
||
[Fact] | ||
public void GivenSerializer_WhenDeserialize_ContentIsValid() | ||
{ | ||
// arrange | ||
var serializer = new JsonPayloadSerializer(); | ||
var content = new byte[] { 34, 70, 111, 111, 34 }; | ||
|
||
// act | ||
var decoded = serializer.Deserialize(content); | ||
|
||
// assert | ||
var expected = "Foo"; | ||
Assert.Equal(expected, decoded); | ||
} | ||
} | ||
} |
Oops, something went wrong.