-
Notifications
You must be signed in to change notification settings - Fork 173
Feature: NATS Backplane #486
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
stebet
wants to merge
9
commits into
ZiggyCreatures:main
Choose a base branch
from
stebet:nats-backplane-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1936b61
Adding a NATS Backplane
stebet 0df2d54
Merge branch 'main' into nats-backplane-implementation
jodydonetti 1d5a3c5
Update src/ZiggyCreatures.FusionCache.Backplane.NATS/NatsBackplane.cs
jodydonetti d86b553
Update src/ZiggyCreatures.FusionCache.Backplane.NATS/NatsBackplane.cs
jodydonetti 0b7d60c
Merge branch 'main' into nats-backplane-implementation
jodydonetti 649f81b
Switched to SLNX format
jodydonetti 45928e5
Merge branch 'main' into nats-backplane-implementation
jodydonetti 11d5ffa
Disable TreatWarningsAsErrors for now
jodydonetti 67f7050
Added a couple of comments about things to look out for
jodydonetti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
144 changes: 144 additions & 0 deletions
144
src/ZiggyCreatures.FusionCache.Backplane.NATS/NatsBackplane.cs
This file contains hidden or 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,144 @@ | ||
| using System.Buffers; | ||
| using System.Text.Json; | ||
|
|
||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| using NATS.Client.Core; | ||
|
|
||
| using ZiggyCreatures.Caching.Fusion.Internals; | ||
|
|
||
| namespace ZiggyCreatures.Caching.Fusion.Backplane.NATS; | ||
|
|
||
| /// <summary> | ||
| /// A Redis based implementation of a FusionCache backplane. | ||
| /// </summary> | ||
| public partial class NatsBackplane | ||
| : IFusionCacheBackplane | ||
| { | ||
| private BackplaneSubscriptionOptions? _subscriptionOptions; | ||
| private readonly ILogger? _logger; | ||
| private INatsConnection _connection; | ||
| private string _channelName = ""; | ||
| private Func<BackplaneMessage, ValueTask>? _incomingMessageHandlerAsync; | ||
| private INatsSub<NatsMemoryOwner<byte>>? _subscription; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the RedisBackplane class. | ||
jodydonetti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| /// <param name="natsConnection">The NATS connection instance to use.</param> | ||
| /// <param name="logger">The <see cref="ILogger{TCategoryName}"/> instance to use. If null, logging will be completely disabled.</param> | ||
| public NatsBackplane(INatsConnection? natsConnection, ILogger<NatsBackplane>? logger = null) | ||
| { | ||
| _connection = natsConnection ?? throw new ArgumentNullException(nameof(natsConnection)); | ||
|
|
||
| // LOGGING | ||
| if (logger is NullLogger<NatsBackplane>) | ||
| { | ||
| // IGNORE NULL LOGGER (FOR BETTER PERF) | ||
| _logger = null; | ||
| } | ||
| else | ||
| { | ||
| _logger = logger; | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public async ValueTask SubscribeAsync(BackplaneSubscriptionOptions subscriptionOptions) | ||
| { | ||
| if (subscriptionOptions is null) | ||
| throw new ArgumentNullException(nameof(subscriptionOptions)); | ||
|
|
||
| if (subscriptionOptions.ChannelName is null) | ||
| throw new NullReferenceException("The BackplaneSubscriptionOptions.ChannelName cannot be null"); | ||
|
|
||
| if (subscriptionOptions.IncomingMessageHandler is null) | ||
| throw new NullReferenceException("The BackplaneSubscriptionOptions.IncomingMessageHandler cannot be null"); | ||
|
|
||
| if (subscriptionOptions.ConnectHandler is null) | ||
| throw new NullReferenceException("The BackplaneSubscriptionOptions.ConnectHandler cannot be null"); | ||
|
|
||
| if (subscriptionOptions.IncomingMessageHandlerAsync is null) | ||
| throw new NullReferenceException("The BackplaneSubscriptionOptions.IncomingMessageHandlerAsync cannot be null"); | ||
|
|
||
| if (subscriptionOptions.ConnectHandlerAsync is null) | ||
| throw new NullReferenceException("The BackplaneSubscriptionOptions.ConnectHandlerAsync cannot be null"); | ||
|
|
||
| _subscriptionOptions = subscriptionOptions; | ||
|
|
||
| _channelName = _subscriptionOptions.ChannelName; | ||
| if (string.IsNullOrEmpty(_channelName)) | ||
| throw new NullReferenceException("The backplane channel name must have a value"); | ||
|
|
||
| _incomingMessageHandlerAsync = _subscriptionOptions.IncomingMessageHandlerAsync; | ||
| _subscription = await _connection.SubscribeCoreAsync<NatsMemoryOwner<byte>>(_channelName); | ||
| _ = Task.Run(async () => | ||
| { | ||
| while (await _subscription.Msgs.WaitToReadAsync().ConfigureAwait(false)) | ||
| { | ||
| while (_subscription.Msgs.TryRead(out var msg)) | ||
| { | ||
| using (msg.Data) | ||
| { | ||
| if(BackplaneMessage.TryParse(msg.Data.Span, out BackplaneMessage message)) | ||
| { | ||
| await OnMessageAsync(message).ConfigureAwait(false); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| /// <inheritdoc/> | ||
| public void Subscribe(BackplaneSubscriptionOptions options) | ||
| { | ||
| SubscribeAsync(options).AsTask().Wait(); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public async ValueTask UnsubscribeAsync() | ||
| { | ||
| if (_subscription is not null) | ||
| { | ||
| await _subscription.UnsubscribeAsync().ConfigureAwait(false); | ||
| await _subscription.Msgs.Completion; | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void Unsubscribe() | ||
| { | ||
| UnsubscribeAsync().AsTask().Wait(); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public async ValueTask PublishAsync(BackplaneMessage message, FusionCacheEntryOptions options, CancellationToken token = default) | ||
| { | ||
| var writer = new NatsBufferWriter<byte>(); | ||
| message.WriteTo(writer); | ||
| await _connection.PublishAsync(_channelName, writer).ConfigureAwait(false); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void Publish(in BackplaneMessage message, FusionCacheEntryOptions options, CancellationToken token = default) | ||
| { | ||
| PublishAsync(message, options, token).AsTask().Wait(); | ||
| } | ||
|
|
||
| internal async ValueTask OnMessageAsync(BackplaneMessage message) | ||
| { | ||
| var tmp = _incomingMessageHandlerAsync; | ||
| if (tmp is null) | ||
| { | ||
| if (_logger?.IsEnabled(LogLevel.Trace) ?? false) | ||
| _logger.Log(LogLevel.Trace, "FUSION [N={CacheName} I={CacheInstanceId}]: [BP] incoming message handler was null", _subscriptionOptions?.CacheName, _subscriptionOptions?.CacheInstanceId); | ||
| return; | ||
| } | ||
|
|
||
| await tmp(message).ConfigureAwait(false); | ||
| } | ||
| } | ||
25 changes: 25 additions & 0 deletions
25
...iggyCreatures.FusionCache.Backplane.NATS/ZiggyCreatures.FusionCache.Backplane.NATS.csproj
This file contains hidden or 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,25 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFramework>netstandard2.0</TargetFramework> | ||
| <Version>2.1.0</Version> | ||
| <PackageId>ZiggyCreatures.FusionCache.Backplane.NATS</PackageId> | ||
| <Description>FusionCache backplane for NATS based on the NATS.Net library</Description> | ||
| <PackageTags>backplane;nats;synadia;caching;cache;hybrid;hybrid-cache;hybridcache;multi-level;multilevel;fusion;fusioncache;fusion-cache;performance;async;ziggy</PackageTags> | ||
| <RootNamespace>ZiggyCreatures.Caching.Fusion.Backplane.NATS</RootNamespace> | ||
| <GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
| <PackageValidationBaselineVersion>1.0.0</PackageValidationBaselineVersion> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Include="artwork\logo-128x128.png" Pack="true" PackagePath="\" /> | ||
| <None Include="docs\README.md" Pack="true" PackagePath="\" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="NATS.Client.Core" Version="2.6.1" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\ZiggyCreatures.FusionCache\ZiggyCreatures.FusionCache.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
Binary file added
BIN
+5.16 KB
src/ZiggyCreatures.FusionCache.Backplane.NATS/artwork/logo-128x128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions
13
src/ZiggyCreatures.FusionCache.Backplane.NATS/docs/README.md
This file contains hidden or 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,13 @@ | ||
| # FusionCache | ||
|
|
||
|  | ||
|
|
||
| ### FusionCache is an easy to use, fast and robust hybrid cache with advanced resiliency features. | ||
|
|
||
| It was born after years of dealing with all sorts of different types of caches: memory caching, distributed caching, http caching, CDNs, browser cache, offline cache, you name it. So I've tried to put together these experiences and came up with FusionCache. | ||
|
|
||
| Find out [more](https://github.com/ZiggyCreatures/FusionCache). | ||
|
|
||
| ## 📦 This package | ||
|
|
||
| This package is a backplane implementation on [NATS](https://nats.io/) based on the awesome [StackExchange.Redis](https://github.com/StackExchange/StackExchange.Redis) library. |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.