mirror of
https://github.com/bitwarden/server.git
synced 2025-05-21 11:34:31 -05:00
connection counter
This commit is contained in:
parent
43e5f300a7
commit
d458d77511
29
src/Notifications/ConnectionCounter.cs
Normal file
29
src/Notifications/ConnectionCounter.cs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Bit.Notifications
|
||||||
|
{
|
||||||
|
public class ConnectionCounter
|
||||||
|
{
|
||||||
|
private int _count = 0;
|
||||||
|
|
||||||
|
public void Increment()
|
||||||
|
{
|
||||||
|
Interlocked.Increment(ref _count);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Decrement()
|
||||||
|
{
|
||||||
|
Interlocked.Decrement(ref _count);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Reset()
|
||||||
|
{
|
||||||
|
_count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetCount()
|
||||||
|
{
|
||||||
|
return _count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
40
src/Notifications/Jobs/JobsHostedService.cs
Normal file
40
src/Notifications/Jobs/JobsHostedService.cs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Bit.Core.Jobs;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Quartz;
|
||||||
|
|
||||||
|
namespace Bit.Notifications.Jobs
|
||||||
|
{
|
||||||
|
public class JobsHostedService : BaseJobsHostedService
|
||||||
|
{
|
||||||
|
public JobsHostedService(
|
||||||
|
IServiceProvider serviceProvider,
|
||||||
|
ILogger<JobsHostedService> logger,
|
||||||
|
ILogger<JobListener> listenerLogger)
|
||||||
|
: base(serviceProvider, logger, listenerLogger) { }
|
||||||
|
|
||||||
|
public override async Task StartAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var everyFiveMinutesTrigger = TriggerBuilder.Create()
|
||||||
|
.StartNow()
|
||||||
|
.WithCronSchedule("0 */5 * * * ?")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
Jobs = new List<Tuple<Type, ITrigger>>
|
||||||
|
{
|
||||||
|
new Tuple<Type, ITrigger>(typeof(LogConnectionCounterJob), everyFiveMinutesTrigger)
|
||||||
|
};
|
||||||
|
|
||||||
|
await base.StartAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddJobsServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddTransient<LogConnectionCounterJob>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
28
src/Notifications/Jobs/LogConnectionCounterJob.cs
Normal file
28
src/Notifications/Jobs/LogConnectionCounterJob.cs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Bit.Core;
|
||||||
|
using Bit.Core.Jobs;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Quartz;
|
||||||
|
|
||||||
|
namespace Bit.Notifications.Jobs
|
||||||
|
{
|
||||||
|
public class LogConnectionCounterJob : BaseJob
|
||||||
|
{
|
||||||
|
private readonly ConnectionCounter _connectionCounter;
|
||||||
|
|
||||||
|
public LogConnectionCounterJob(
|
||||||
|
ILogger<LogConnectionCounterJob> logger,
|
||||||
|
ConnectionCounter connectionCounter)
|
||||||
|
: base(logger)
|
||||||
|
{
|
||||||
|
_connectionCounter = connectionCounter;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override Task ExecuteJobAsync(IJobExecutionContext context)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(Constants.BypassFiltersEventId,
|
||||||
|
"Connection count: {0}", _connectionCounter.GetCount());
|
||||||
|
return Task.FromResult(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,13 @@ namespace Bit.Notifications
|
|||||||
[Authorize("Application")]
|
[Authorize("Application")]
|
||||||
public class NotificationsHub : Microsoft.AspNetCore.SignalR.Hub
|
public class NotificationsHub : Microsoft.AspNetCore.SignalR.Hub
|
||||||
{
|
{
|
||||||
|
private readonly ConnectionCounter _connectionCounter;
|
||||||
|
|
||||||
|
public NotificationsHub(ConnectionCounter connectionCounter)
|
||||||
|
{
|
||||||
|
_connectionCounter = connectionCounter;
|
||||||
|
}
|
||||||
|
|
||||||
public override async Task OnConnectedAsync()
|
public override async Task OnConnectedAsync()
|
||||||
{
|
{
|
||||||
var currentContext = new CurrentContext();
|
var currentContext = new CurrentContext();
|
||||||
@ -16,6 +23,7 @@ namespace Bit.Notifications
|
|||||||
{
|
{
|
||||||
await Groups.AddToGroupAsync(Context.ConnectionId, $"Organization_{org.Id}");
|
await Groups.AddToGroupAsync(Context.ConnectionId, $"Organization_{org.Id}");
|
||||||
}
|
}
|
||||||
|
_connectionCounter.Increment();
|
||||||
await base.OnConnectedAsync();
|
await base.OnConnectedAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,6 +35,7 @@ namespace Bit.Notifications
|
|||||||
{
|
{
|
||||||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"Organization_{org.Id}");
|
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"Organization_{org.Id}");
|
||||||
}
|
}
|
||||||
|
_connectionCounter.Decrement();
|
||||||
await base.OnDisconnectedAsync(exception);
|
await base.OnDisconnectedAsync(exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,12 +31,6 @@ namespace Bit.Notifications
|
|||||||
// Settings
|
// Settings
|
||||||
var globalSettings = services.AddGlobalSettingsServices(Configuration);
|
var globalSettings = services.AddGlobalSettingsServices(Configuration);
|
||||||
|
|
||||||
// Repositories
|
|
||||||
services.AddSqlServerRepositories(globalSettings);
|
|
||||||
|
|
||||||
// Context
|
|
||||||
services.AddScoped<CurrentContext>();
|
|
||||||
|
|
||||||
// Identity
|
// Identity
|
||||||
services.AddIdentityAuthenticationServices(globalSettings, Environment, config =>
|
services.AddIdentityAuthenticationServices(globalSettings, Environment, config =>
|
||||||
{
|
{
|
||||||
@ -63,15 +57,20 @@ namespace Bit.Notifications
|
|||||||
services.AddSignalR();
|
services.AddSignalR();
|
||||||
}
|
}
|
||||||
services.AddSingleton<IUserIdProvider, SubjectUserIdProvider>();
|
services.AddSingleton<IUserIdProvider, SubjectUserIdProvider>();
|
||||||
|
services.AddSingleton<ConnectionCounter>();
|
||||||
|
|
||||||
// Mvc
|
// Mvc
|
||||||
services.AddMvc();
|
services.AddMvc();
|
||||||
|
|
||||||
// Hosted Services
|
if(!globalSettings.SelfHosted)
|
||||||
if(!globalSettings.SelfHosted &&
|
|
||||||
CoreHelpers.SettingHasValue(globalSettings.Notifications?.ConnectionString))
|
|
||||||
{
|
{
|
||||||
services.AddHostedService<AzureQueueHostedService>();
|
// Hosted Services
|
||||||
|
Jobs.JobsHostedService.AddJobsServices(services);
|
||||||
|
services.AddHostedService<Jobs.JobsHostedService>();
|
||||||
|
if(CoreHelpers.SettingHasValue(globalSettings.Notifications?.ConnectionString))
|
||||||
|
{
|
||||||
|
services.AddHostedService<AzureQueueHostedService>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,9 +104,6 @@ namespace Bit.Notifications
|
|||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default Middleware
|
|
||||||
app.UseDefaultMiddleware(env);
|
|
||||||
|
|
||||||
// Add Cors
|
// Add Cors
|
||||||
app.UseCors(policy => policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
|
app.UseCors(policy => policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user