Skip to content
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

Develop #50

Merged
merged 4 commits into from
Dec 9, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Castle.DynamicProxy;
using Nerosoft.Euonia.Core;
using Nerosoft.Euonia.Validation;

namespace Nerosoft.Euonia.Application;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ internal static void SendAll<TMessage>(ReadOnlySpan<object> pairs, int i, TMessa
// has exactly 2 * i elements (due to this slicing), and each loop iteration processes a pair.
// The loops ends when the initial reference reaches the end, and that's incremented by 2 at
// the end of each iteration. The target being a span, obviously means the length is constant.
var slice = pairs.Slice(0, 2 * i);
var slice = pairs[..(2 * i)];

ref var sliceStart = ref MemoryMarshal.GetReference(slice);
ref var sliceEnd = ref Unsafe.Add(ref sliceStart, slice.Length);
Expand Down
53 changes: 23 additions & 30 deletions Source/Euonia.Bus/Core/ServiceBus.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Nerosoft.Euonia.Modularity;
using Nerosoft.Euonia.Modularity;

namespace Nerosoft.Euonia.Bus;

Expand All @@ -10,7 +9,7 @@ public sealed class ServiceBus : IBus
{
private readonly IDispatcher _dispatcher;
private readonly IMessageConvention _convention;
private readonly IServiceAccessor _serviceAccessor;
private readonly IRequestContextAccessor _requestAccessor;

/// <summary>
/// Initializes a new instance of the <see cref="ServiceBus"/> class.
Expand All @@ -28,11 +27,11 @@ public ServiceBus(IBusFactory factory, IMessageConvention convention)
/// </summary>
/// <param name="factory"></param>
/// <param name="convention"></param>
/// <param name="serviceAccessor"></param>
public ServiceBus(IBusFactory factory, IMessageConvention convention, IServiceAccessor serviceAccessor)
/// <param name="requestAccessor"></param>
public ServiceBus(IBusFactory factory, IMessageConvention convention, IRequestContextAccessor requestAccessor)
: this(factory, convention)
{
_serviceAccessor = serviceAccessor;
_requestAccessor = requestAccessor;
}

/// <inheritdoc />
Expand All @@ -46,7 +45,7 @@ public Task PublishAsync<TMessage>(TMessage message, PublishOptions options, Act
throw new MessageTypeException("The message type is not an event type.");
}

var context = GetRequestContext();
var context = _requestAccessor?.Context;

var channelName = options.Channel ?? MessageCache.Default.GetOrAddChannel<TMessage>();
var pack = new RoutedMessage<TMessage>(message, channelName)
Expand All @@ -69,7 +68,7 @@ public Task SendAsync<TMessage>(TMessage message, SendOptions options, Action<Me
throw new MessageTypeException("The message type is not a queue type.");
}

var context = GetRequestContext();
var context = _requestAccessor?.Context;

var channelName = options.Channel ?? MessageCache.Default.GetOrAddChannel<TMessage>();
var pack = new RoutedMessage<TMessage>(message, channelName)
Expand All @@ -96,7 +95,7 @@ public Task<TResult> SendAsync<TMessage, TResult>(TMessage message, SendOptions
throw new MessageTypeException("The message type is not a queue type.");
}

var context = GetRequestContext();
var context = _requestAccessor?.Context;

var channelName = options.Channel ?? MessageCache.Default.GetOrAddChannel<TMessage>();
var pack = new RoutedMessage<TMessage, TResult>(message, channelName)
Expand All @@ -110,21 +109,21 @@ public Task<TResult> SendAsync<TMessage, TResult>(TMessage message, SendOptions
metadataSetter?.Invoke(pack.Metadata);

return _dispatcher.SendAsync(pack, cancellationToken)
.ContinueWith(task =>
{
task.WaitAndUnwrapException();
var result = task.Result;
callback?.Invoke(result);
return result;
}, cancellationToken);
.ContinueWith(task =>
{
task.WaitAndUnwrapException();
var result = task.Result;
callback?.Invoke(result);
return result;
}, cancellationToken);
}

/// <inheritdoc />
public Task<TResult> SendAsync<TResult>(IQueue<TResult> message, SendOptions options, Action<MessageMetadata> metadataSetter = null, Action<TResult> callback = null, CancellationToken cancellationToken = default)
{
options ??= new SendOptions();

var context = GetRequestContext();
var context = _requestAccessor?.Context;

var channelName = options.Channel ?? MessageCache.Default.GetOrAddChannel(message.GetType());
var pack = new RoutedMessage<IQueue<TResult>, TResult>(message, channelName)
Expand All @@ -138,18 +137,12 @@ public Task<TResult> SendAsync<TResult>(IQueue<TResult> message, SendOptions opt
metadataSetter?.Invoke(pack.Metadata);

return _dispatcher.SendAsync(pack, cancellationToken)
.ContinueWith(task =>
{
task.WaitAndUnwrapException();
var result = task.Result;
callback?.Invoke(result);
return result;
}, cancellationToken);
}

private RequestContext GetRequestContext()
{
var contextAccessor = _serviceAccessor?.ServiceProvider.GetService<IRequestContextAccessor>();
return contextAccessor?.Context;
.ContinueWith(task =>
{
task.WaitAndUnwrapException();
var result = task.Result;
callback?.Invoke(result);
return result;
}, cancellationToken);
}
}
5 changes: 5 additions & 0 deletions Source/Euonia.Bus/Serialization/NewtonsoftJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ public async Task<byte[]> SerializeAsync<T>(T message, CancellationToken cancell
JsonSerializer.Create().Serialize(jsonWriter, message);

await jsonWriter.FlushAsync(cancellationToken);

#if NET8_0_OR_GREATER
await writer.FlushAsync(cancellationToken);
#else
await writer.FlushAsync();
#endif

return stream.ToArray();
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Euonia.Bus/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void AddServiceBus(this IServiceCollection services, Action<BusCon
return context;
});
services.TryAddSingleton<IMessageConvention>(configurator.ConventionBuilder.Convention);
services.AddSingleton<IBus, ServiceBus>();
services.TryAddScoped<IBus, ServiceBus>();
services.AddHostedService<RecipientActivator>();
}
}
3 changes: 1 addition & 2 deletions Source/Euonia.Business/Core/EditableObject.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Nerosoft.Euonia.Core;
using Nerosoft.Euonia.Reflection;
using Nerosoft.Euonia.Reflection;

namespace Nerosoft.Euonia.Business;

Expand Down
4 changes: 2 additions & 2 deletions Source/Euonia.Caching.Redis/RedisCacheHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public class RedisCacheHandle<TValue> : BaseCacheHandle<TValue>
end
return result";

private readonly IDictionary<ScriptType, LoadedLuaScript> _shaScripts = new Dictionary<ScriptType, LoadedLuaScript>();
private readonly IDictionary<ScriptType, LuaScript> _luaScripts = new Dictionary<ScriptType, LuaScript>();
private readonly Dictionary<ScriptType, LoadedLuaScript> _shaScripts = new();
private readonly Dictionary<ScriptType, LuaScript> _luaScripts = new();
private readonly CacheManagerConfiguration _managerConfiguration;
private readonly RedisValueConverter _valueConverter;
private readonly RedisConnectionManager _connection;
Expand Down
2 changes: 1 addition & 1 deletion Source/Euonia.Caching.Redis/RedisConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class RedisConfigurationBuilder
private bool _allowAdmin;
private int _connectionTimeout = 5000;
private int _database;
private readonly IList<ServerEndPoint> _endpoints = new List<ServerEndPoint>();
private readonly List<ServerEndPoint> _endpoints = new();
private bool _isSsl;
private readonly string _key;
private string _password;
Expand Down
2 changes: 1 addition & 1 deletion Source/Euonia.Caching.Redis/RedisConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Nerosoft.Euonia.Caching.Redis;

internal class RedisConnectionManager
{
private static readonly IDictionary<string, IConnectionMultiplexer> _connections = new Dictionary<string, IConnectionMultiplexer>();
private static readonly Dictionary<string, IConnectionMultiplexer> _connections = new();
private static readonly object _connectLock = new();

private readonly string _connectionString;
Expand Down
14 changes: 4 additions & 10 deletions Source/Euonia.Caching/BaseCacheManager.GetOrAdd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public bool TryGetOrAdd(string key, Func<string, TValue> valueFactory, out TValu
return true;
}

value = default(TValue);
value = default;
return false;
}

Expand All @@ -93,7 +93,7 @@ public bool TryGetOrAdd(string key, string region, Func<string, string, TValue>
return true;
}

value = default(TValue);
value = default;
return false;
}

Expand Down Expand Up @@ -130,10 +130,7 @@ private bool TryGetOrAddInternal(string key, string region, Func<string, string,
}

// changed logic to invoke the factory only once in case of retries
if (newItem == null)
{
newItem = valueFactory(key, region);
}
newItem ??= valueFactory(key, region);

if (newItem == null)
{
Expand Down Expand Up @@ -165,10 +162,7 @@ private CacheItem<TValue> GetOrAddInternal(string key, string region, Func<strin
}

// changed logic to invoke the factory only once in case of retries
if (newItem == null)
{
newItem = valueFactory(key, region);
}
newItem ??= valueFactory(key, region);

// Throw explicit to me more consistent. Otherwise it would throw later eventually...
if (newItem == null)
Expand Down
4 changes: 2 additions & 2 deletions Source/Euonia.Caching/BaseCacheManager.Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private TValue AddOrUpdateInternal(CacheItem<TValue> item, Func<TValue, TValue>
while (tries <= maxRetries);

// exceeded max retries, failing the operation... (should not happen in 99,99% of the cases though, better throw?)
return default(TValue);
return default;
}

/// <inheritdoc />
Expand Down Expand Up @@ -143,7 +143,7 @@ private bool UpdateInternal(BaseCacheHandle<TValue>[] handles,
CheckDisposed();

// assign null
value = default(TValue);
value = default;

if (handles.Length == 0)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Euonia.Caching/Default/CacheContextTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class CacheContextTask<T> : IDisposable
/// <summary>
/// The tokens
/// </summary>
private IList<IVolatileToken> _tokens;
private List<IVolatileToken> _tokens;

/// <summary>
/// Initializes a new instance of the <see cref="CacheContextTask{T}"/> class.
Expand Down
2 changes: 1 addition & 1 deletion Source/Euonia.Caching/Default/DefaultCacheSignal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DefaultCacheSignal : ICacheSignal
/// <summary>
/// The tokens
/// </summary>
private readonly IDictionary<object, VolatileToken> _tokens = new Dictionary<object, VolatileToken>();
private readonly Dictionary<object, VolatileToken> _tokens = new();

/// <summary>
/// Triggers the specified signal.
Expand Down
6 changes: 3 additions & 3 deletions Source/Euonia.Caching/Internal/BaseCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public virtual TValue Get(string key)
return item.Value;
}

return default(TValue);
return default;
}

/// <summary>
Expand All @@ -214,7 +214,7 @@ public virtual TValue Get(string key, string region)
return item.Value;
}

return default(TValue);
return default;
}

/// <summary>
Expand Down Expand Up @@ -474,7 +474,7 @@ protected static TOut GetCasted<TOut>(object value)
{
if (value == null)
{
return default(TOut);
return default;
}

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ public ValueTask AddAsync(Func<ValueTask> dispose)
/// Creates a new disposable that executes <paramref name="dispose"/> when disposed.
/// </summary>
/// <param name="dispose">The delegate to execute when disposed. May not be <c>null</c>.</param>
public static AsyncAnonymousDisposable Create(Func<ValueTask> dispose) => new AsyncAnonymousDisposable(dispose);
public static AsyncAnonymousDisposable Create(Func<ValueTask> dispose) => new(dispose);
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public ValueTask AddAsync(IAsyncDisposable disposable)
/// Creates a disposable that disposes a collection of disposables.
/// </summary>
/// <param name="disposables">The disposables to dispose.</param>
public static CollectionDisposable Create(params IDisposable[] disposables) => new CollectionDisposable(disposables);
public static CollectionDisposable Create(params IDisposable[] disposables) => new(disposables);

/// <summary>
/// Creates a disposable that disposes a collection of disposables.
/// </summary>
/// <param name="disposables">The disposables to dispose.</param>
public static CollectionDisposable Create(IEnumerable<IDisposable> disposables) => new CollectionDisposable(disposables);
public static CollectionDisposable Create(IEnumerable<IDisposable> disposables) => new(disposables);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public sealed class AsyncNoopDisposable : IAsyncDisposable
/// <summary>
/// Does nothing.
/// </summary>
public ValueTask DisposeAsync() => new ValueTask();
public ValueTask DisposeAsync() => new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class AsyncSingleDisposable<T> : IAsyncDisposable
/// </summary>
private readonly AsyncBoundActionField<T> _context;

private readonly TaskCompletionSource<object> _tcs = new TaskCompletionSource<object>(TaskCreationOptions.DenyChildAttach);
private readonly TaskCompletionSource<object> _tcs = new(TaskCreationOptions.DenyChildAttach);

/// <summary>
/// Creates a disposable for the specified context.
Expand Down
2 changes: 1 addition & 1 deletion Source/Euonia.Core/Reflection/PropertyAccessorCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Nerosoft.Euonia.Reflection;
public static class PropertyAccessorCache<T> where T : class
{
// ReSharper disable once StaticMemberInGenericType
private static readonly IDictionary<string, LambdaExpression> _cache = new Dictionary<string, LambdaExpression>();
private static readonly Dictionary<string, LambdaExpression> _cache = new();

static PropertyAccessorCache()
{
Expand Down
Loading