-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from Cysharp/dotnet6_add_nats
.NET 6 and Nats support
- Loading branch information
Showing
34 changed files
with
521 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,7 @@ services: | |
image: "redis:6.2.1" | ||
ports: | ||
- "6379:6379" | ||
nats: | ||
image: nats | ||
ports: | ||
- 4222:4222 |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<!-- NuGet Packaging --> | ||
<PackageTags>pubsub;eventaggregator</PackageTags> | ||
<Description>Nats IDistributedPublisher/Subscriber provider for MessagePipe.</Description> | ||
<SignAssembly>true</SignAssembly> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="Icon.png" Pack="true" PackagePath="/" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AlterNats" Version="0.0.2" /> | ||
<PackageReference Include="MessagePack" Version="2.3.85" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MessagePipe\MessagePipe.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace MessagePipe.Nats; | ||
|
||
public sealed class MessagePipeNatsOptions | ||
{ | ||
public NatsConnectionFactory NatsConnectionFactory { get; } | ||
|
||
public MessagePipeNatsOptions(NatsConnectionFactory connectionFactory) | ||
{ | ||
NatsConnectionFactory = connectionFactory; | ||
} | ||
} |
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,28 @@ | ||
using AlterNats; | ||
|
||
namespace MessagePipe.Nats; | ||
|
||
public class NatsConnectionFactory | ||
{ | ||
readonly NatsOptions options; | ||
NatsConnection? connection; | ||
|
||
public NatsConnectionFactory() : this(NatsOptions.Default) { } | ||
|
||
public NatsConnectionFactory(NatsOptions options) | ||
{ | ||
this.options = options; | ||
} | ||
|
||
public async ValueTask<NatsConnection> GetConnectionAsync() | ||
{ | ||
connection ??= new NatsConnection(options); | ||
|
||
if (connection.ConnectionState == NatsConnectionState.Closed) | ||
{ | ||
await connection.ConnectAsync(); | ||
} | ||
|
||
return connection; | ||
} | ||
} |
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 AlterNats; | ||
|
||
namespace MessagePipe.Nats; | ||
|
||
public sealed class NatsPublisher<TKey, TMessage> : IDistributedPublisher<TKey, TMessage> | ||
{ | ||
readonly NatsConnectionFactory connectionFactory; | ||
|
||
public NatsPublisher( | ||
NatsConnectionFactory connectionFactory) | ||
{ | ||
this.connectionFactory = connectionFactory; | ||
} | ||
|
||
public async ValueTask PublishAsync(TKey key, TMessage message, CancellationToken cancellationToken = new CancellationToken()) | ||
{ | ||
var natsKey = GetNatsKey(key); | ||
|
||
if (natsKey == null) throw new ArgumentNullException(nameof(key)); | ||
|
||
var connection = await connectionFactory.GetConnectionAsync(); | ||
await connection.PublishAsync(natsKey.Value, message); | ||
} | ||
|
||
NatsKey? GetNatsKey(TKey key) | ||
{ | ||
switch (key) | ||
{ | ||
case NatsKey natsKey: | ||
return natsKey; | ||
case string s: | ||
return new NatsKey(s); | ||
default: | ||
var k = key?.ToString(); | ||
return k != null ? new NatsKey(k) : null; | ||
} | ||
} | ||
} |
Oops, something went wrong.