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

Made use of AsyncKeyedLock library that will clean up semaphores after use #327

Open
wants to merge 1 commit into
base: master
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
1 change: 1 addition & 0 deletions DotNetifyLib.SignalR/DotNetifyLib.SignalR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AsyncKeyedLock" Version="6.4.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="3.1.0" />
Expand Down
136 changes: 70 additions & 66 deletions DotNetifyLib.SignalR/Forwarding/DotNetifyHubForwarderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,86 +14,90 @@ You may obtain a copy of the License at
limitations under the License.
*/

using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsyncKeyedLock;
using DotNetify.Client;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Nito.AsyncEx;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;

namespace DotNetify.Forwarding
{
/// <summary>
/// Provides objects for forwarding hub messages to another server.
/// </summary>
public interface IDotNetifyHubForwarderFactory
{
Task InvokeInstanceAsync(string serverUrl, ForwardingOptions config, Func<DotNetifyHubForwarder, Task> invoke);
}
/// <summary>
/// Provides objects for forwarding hub messages to another server.
/// </summary>
public interface IDotNetifyHubForwarderFactory
{
Task InvokeInstanceAsync(string serverUrl, ForwardingOptions config, Func<DotNetifyHubForwarder, Task> invoke);
}

/// <summary>
/// This class produces hub forwarder objects for forwarding hub messages to another server.
/// </summary>
public class DotNetifyHubForwarderFactory : IDotNetifyHubForwarderFactory
{
private readonly ConcurrentDictionary<DotNetifyHubForwarder, SemaphoreSlim> _semaphores = new ConcurrentDictionary<DotNetifyHubForwarder, SemaphoreSlim>();
private readonly ConcurrentDictionary<string, AsyncCollection<DotNetifyHubForwarder>> _hubForwarders = new ConcurrentDictionary<string, AsyncCollection<DotNetifyHubForwarder>>();
private readonly IHubContext<DotNetifyHub> _globalHubContext;
private readonly IDotNetifyHubProxyFactory _hubProxyFactory;
/// <summary>
/// This class produces hub forwarder objects for forwarding hub messages to another server.
/// </summary>
public class DotNetifyHubForwarderFactory : IDotNetifyHubForwarderFactory
{
private readonly AsyncKeyedLocker<DotNetifyHubForwarder> _semaphores = new AsyncKeyedLocker<DotNetifyHubForwarder>(o =>
{
o.PoolSize = 20;
o.PoolInitialFill = 1;
});
private readonly ConcurrentDictionary<string, AsyncCollection<DotNetifyHubForwarder>> _hubForwarders = new ConcurrentDictionary<string, AsyncCollection<DotNetifyHubForwarder>>();
private readonly IHubContext<DotNetifyHub> _globalHubContext;
private readonly IDotNetifyHubProxyFactory _hubProxyFactory;

public DotNetifyHubForwarderFactory(IHubContext<DotNetifyHub> globalHubContext, IDotNetifyHubProxyFactory hubProxyFactory)
{
_globalHubContext = globalHubContext;
_hubProxyFactory = hubProxyFactory;
}
public DotNetifyHubForwarderFactory(IHubContext<DotNetifyHub> globalHubContext, IDotNetifyHubProxyFactory hubProxyFactory)
{
_globalHubContext = globalHubContext;
_hubProxyFactory = hubProxyFactory;
}

public async Task InvokeInstanceAsync(string serverUrl, ForwardingOptions config, Func<DotNetifyHubForwarder, Task> invoke)
{
var pool = GetConnectionPool(serverUrl, config);
var hubForwarder = await pool.TakeAsync();
public async Task InvokeInstanceAsync(string serverUrl, ForwardingOptions config, Func<DotNetifyHubForwarder, Task> invoke)
{
var pool = GetConnectionPool(serverUrl, config);
var hubForwarder = await pool.TakeAsync();

var semaphore = _semaphores.GetOrAdd(hubForwarder, _ => new SemaphoreSlim(1));
await semaphore.WaitAsync();
await hubForwarder.StartAsync();
using (await _semaphores.LockAsync(hubForwarder).ConfigureAwait(false))
{
await hubForwarder.StartAsync();

try
{
await invoke(hubForwarder);
}
catch (Exception ex)
{
throw new ForwardingException(ex.Message, ex);
}
finally
{
semaphore.Release();
_ = pool.AddAsync(hubForwarder);
}
}
try
{
await invoke(hubForwarder);
}
catch (Exception ex)
{
throw new ForwardingException(ex.Message, ex);
}
finally
{
_ = pool.AddAsync(hubForwarder);
}
}
}

private AsyncCollection<DotNetifyHubForwarder> GetConnectionPool(string serverUrl, ForwardingOptions config)
{
return _hubForwarders.GetOrAdd(serverUrl, url =>
{
var newForwarders = Enumerable.Range(1, config.ConnectionPoolSize).Select(x => CreateInstance(serverUrl, config));
return new AsyncCollection<DotNetifyHubForwarder>(new ConcurrentQueue<DotNetifyHubForwarder>(newForwarders));
});
}
private AsyncCollection<DotNetifyHubForwarder> GetConnectionPool(string serverUrl, ForwardingOptions config)
{
return _hubForwarders.GetOrAdd(serverUrl, url =>
{
var newForwarders = Enumerable.Range(1, config.ConnectionPoolSize).Select(x => CreateInstance(serverUrl, config));
return new AsyncCollection<DotNetifyHubForwarder>(new ConcurrentQueue<DotNetifyHubForwarder>(newForwarders));
});
}

private DotNetifyHubForwarder CreateInstance(string serverUrl, ForwardingOptions config)
{
var hubProxy = _hubProxyFactory.GetInstance();
private DotNetifyHubForwarder CreateInstance(string serverUrl, ForwardingOptions config)
{
var hubProxy = _hubProxyFactory.GetInstance();

if (config.UseMessagePack)
(hubProxy as DotNetifyHubProxy).ConnectionBuilder = builder => builder.AddMessagePackProtocol();
else
(hubProxy as DotNetifyHubProxy).ConnectionBuilder = builder => builder.AddJsonProtocol();
if (config.UseMessagePack)
(hubProxy as DotNetifyHubProxy).ConnectionBuilder = builder => builder.AddMessagePackProtocol();
else
(hubProxy as DotNetifyHubProxy).ConnectionBuilder = builder => builder.AddJsonProtocol();

hubProxy.Init(null, serverUrl);
return new DotNetifyHubForwarder(hubProxy, new DotNetifyHubResponse(_globalHubContext));
}
}
hubProxy.Init(null, serverUrl);
return new DotNetifyHubForwarder(hubProxy, new DotNetifyHubResponse(_globalHubContext));
}
}
}
Loading