1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-31 18:27:49 +01:00

connection counter

This commit is contained in:
Kyle Spearrin 2018-08-23 15:48:40 -04:00
parent 43e5f300a7
commit d458d77511
5 changed files with 115 additions and 13 deletions

View 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;
}
}
}

View 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>();
}
}
}

View 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);
}
}
}

View File

@ -8,6 +8,13 @@ namespace Bit.Notifications
[Authorize("Application")]
public class NotificationsHub : Microsoft.AspNetCore.SignalR.Hub
{
private readonly ConnectionCounter _connectionCounter;
public NotificationsHub(ConnectionCounter connectionCounter)
{
_connectionCounter = connectionCounter;
}
public override async Task OnConnectedAsync()
{
var currentContext = new CurrentContext();
@ -16,6 +23,7 @@ namespace Bit.Notifications
{
await Groups.AddToGroupAsync(Context.ConnectionId, $"Organization_{org.Id}");
}
_connectionCounter.Increment();
await base.OnConnectedAsync();
}
@ -27,6 +35,7 @@ namespace Bit.Notifications
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"Organization_{org.Id}");
}
_connectionCounter.Decrement();
await base.OnDisconnectedAsync(exception);
}
}

View File

@ -31,12 +31,6 @@ namespace Bit.Notifications
// Settings
var globalSettings = services.AddGlobalSettingsServices(Configuration);
// Repositories
services.AddSqlServerRepositories(globalSettings);
// Context
services.AddScoped<CurrentContext>();
// Identity
services.AddIdentityAuthenticationServices(globalSettings, Environment, config =>
{
@ -63,15 +57,20 @@ namespace Bit.Notifications
services.AddSignalR();
}
services.AddSingleton<IUserIdProvider, SubjectUserIdProvider>();
services.AddSingleton<ConnectionCounter>();
// Mvc
services.AddMvc();
// Hosted Services
if(!globalSettings.SelfHosted &&
CoreHelpers.SettingHasValue(globalSettings.Notifications?.ConnectionString))
if(!globalSettings.SelfHosted)
{
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();
}
// Default Middleware
app.UseDefaultMiddleware(env);
// Add Cors
app.UseCors(policy => policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());