Skip to content

Commit aa5ffd0

Browse files
committed
Performance optimization
1 parent 1617300 commit aa5ffd0

File tree

124 files changed

+5653
-5982
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+5653
-5982
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ PublishProfiles/
1212
*.cache
1313
*.docstates
1414
_ReSharper.*
15+
*.[Rr]e[Ss]harper
16+
*.DotSettings.user
1517
nuget.exe
1618
*k10.csproj
1719
*.psess

src/DotNetCore.CAP.Dashboard/RouteActionProvider.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public RouteActionProvider(HttpRequest request, HttpResponse response, RouteData
4141
[HttpGet("/stats")]
4242
public async Task Stats()
4343
{
44-
var result = await MonitoringApi.GetStatistics();
44+
var result = await MonitoringApi.GetStatisticsAsync();
4545
SetServersCount(result);
4646
await _response.WriteAsJsonAsync(result);
4747

@@ -203,7 +203,7 @@ public async Task PublishedList()
203203
PageSize = pageSize
204204
};
205205

206-
var result = await MonitoringApi.Messages(queryDto);
206+
var result = await MonitoringApi.GetMessagesAsync(queryDto);
207207

208208
await _response.WriteAsJsonAsync(result);
209209
}
@@ -230,7 +230,7 @@ public async Task ReceivedList()
230230
PageSize = pageSize
231231
};
232232

233-
var result = await MonitoringApi.Messages(queryDto);
233+
var result = await MonitoringApi.GetMessagesAsync(queryDto);
234234

235235
await _response.WriteAsJsonAsync(result);
236236
}

src/DotNetCore.CAP.InMemoryStorage/IMonitoringApi.InMemory.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal class InMemoryMonitoringApi : IMonitoringApi
2525
return Task.FromResult<MediumMessage?>(InMemoryStorage.ReceivedMessages.Values.FirstOrDefault(x => x.DbId == id.ToString(CultureInfo.InvariantCulture)));
2626
}
2727

28-
public Task<StatisticsDto> GetStatistics()
28+
public Task<StatisticsDto> GetStatisticsAsync()
2929
{
3030
var stats = new StatisticsDto
3131
{
@@ -47,7 +47,7 @@ public Task<IDictionary<DateTime, int>> HourlySucceededJobs(MessageType type)
4747
return GetHourlyTimelineStats(type, nameof(StatusName.Succeeded));
4848
}
4949

50-
public Task<PagedQueryResult<MessageDto>> Messages(MessageQueryDto queryDto)
50+
public Task<PagedQueryResult<MessageDto>> GetMessagesAsync(MessageQueryDto queryDto)
5151
{
5252
if (queryDto.MessageType == MessageType.Publish)
5353
{

src/DotNetCore.CAP.MongoDB/CAP.MongoDBCapOptionsExtension.cs

+21-22
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,32 @@
88
using Microsoft.Extensions.Options;
99
using MongoDB.Driver;
1010

11-
namespace DotNetCore.CAP.MongoDB
11+
namespace DotNetCore.CAP.MongoDB;
12+
13+
// ReSharper disable once InconsistentNaming
14+
public class MongoDBCapOptionsExtension : ICapOptionsExtension
1215
{
13-
// ReSharper disable once InconsistentNaming
14-
public class MongoDBCapOptionsExtension : ICapOptionsExtension
15-
{
16-
private readonly Action<MongoDBOptions> _configure;
16+
private readonly Action<MongoDBOptions> _configure;
1717

18-
public MongoDBCapOptionsExtension(Action<MongoDBOptions> configure)
19-
{
20-
_configure = configure;
21-
}
18+
public MongoDBCapOptionsExtension(Action<MongoDBOptions> configure)
19+
{
20+
_configure = configure;
21+
}
2222

23-
public void AddServices(IServiceCollection services)
24-
{
25-
services.AddSingleton<CapStorageMarkerService>();
23+
public void AddServices(IServiceCollection services)
24+
{
25+
services.AddSingleton<CapStorageMarkerService>();
2626

27-
services.AddSingleton<IDataStorage, MongoDBDataStorage>();
28-
services.AddSingleton<IStorageInitializer, MongoDBStorageInitializer>();
27+
services.AddSingleton<IDataStorage, MongoDBDataStorage>();
28+
services.AddSingleton<IStorageInitializer, MongoDBStorageInitializer>();
2929

30-
services.Configure(_configure);
30+
services.Configure(_configure);
3131

32-
//Try to add IMongoClient if does not exists
33-
services.TryAddSingleton<IMongoClient>(x =>
34-
{
35-
var options = x.GetRequiredService<IOptions<MongoDBOptions>>().Value;
36-
return new MongoClient(options.DatabaseConnection);
37-
});
38-
}
32+
//Try to add IMongoClient if does not exists
33+
services.TryAddSingleton<IMongoClient>(x =>
34+
{
35+
var options = x.GetRequiredService<IOptions<MongoDBOptions>>().Value;
36+
return new MongoClient(options.DatabaseConnection);
37+
});
3938
}
4039
}
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
// Copyright (c) .NET Core Community. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
namespace DotNetCore.CAP.MongoDB
4+
namespace DotNetCore.CAP.MongoDB;
5+
6+
// ReSharper disable once InconsistentNaming
7+
public class MongoDBOptions
58
{
6-
// ReSharper disable once InconsistentNaming
7-
public class MongoDBOptions
8-
{
9-
/// <summary>
10-
/// Gets or sets the database name to use when creating database objects.
11-
/// Default value: "cap"
12-
/// </summary>
13-
public string DatabaseName { get; set; } = "cap";
9+
/// <summary>
10+
/// Gets or sets the database name to use when creating database objects.
11+
/// Default value: "cap"
12+
/// </summary>
13+
public string DatabaseName { get; set; } = "cap";
1414

15-
/// <summary>
16-
/// MongoDB database connection string.
17-
/// Default value: "mongodb://localhost:27017"
18-
/// </summary>
19-
public string DatabaseConnection { get; set; } = "mongodb://localhost:27017";
15+
/// <summary>
16+
/// MongoDB database connection string.
17+
/// Default value: "mongodb://localhost:27017"
18+
/// </summary>
19+
public string DatabaseConnection { get; set; } = "mongodb://localhost:27017";
2020

21-
/// <summary>
22-
/// MongoDB received message collection name.
23-
/// Default value: "received"
24-
/// </summary>
25-
public string ReceivedCollection { get; set; } = "cap.received";
21+
/// <summary>
22+
/// MongoDB received message collection name.
23+
/// Default value: "received"
24+
/// </summary>
25+
public string ReceivedCollection { get; set; } = "cap.received";
2626

27-
/// <summary>
28-
/// MongoDB published message collection name.
29-
/// Default value: "published"
30-
/// </summary>
31-
public string PublishedCollection { get; set; } = "cap.published";
27+
/// <summary>
28+
/// MongoDB published message collection name.
29+
/// Default value: "published"
30+
/// </summary>
31+
public string PublishedCollection { get; set; } = "cap.published";
3232

33-
internal string Version { get; set; } = default!;
34-
}
33+
internal string Version { get; set; } = default!;
3534
}

src/DotNetCore.CAP.MongoDB/CAP.Options.Extensions.cs

+16-17
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,28 @@
66
using DotNetCore.CAP.MongoDB;
77

88
// ReSharper disable once CheckNamespace
9-
namespace Microsoft.Extensions.DependencyInjection
9+
namespace Microsoft.Extensions.DependencyInjection;
10+
11+
public static class CapOptionsExtensions
1012
{
11-
public static class CapOptionsExtensions
13+
public static CapOptions UseMongoDB(this CapOptions options)
1214
{
13-
public static CapOptions UseMongoDB(this CapOptions options)
14-
{
15-
return options.UseMongoDB(x => { });
16-
}
15+
return options.UseMongoDB(x => { });
16+
}
1717

18-
public static CapOptions UseMongoDB(this CapOptions options, string connectionString)
19-
{
20-
return options.UseMongoDB(x => { x.DatabaseConnection = connectionString; });
21-
}
18+
public static CapOptions UseMongoDB(this CapOptions options, string connectionString)
19+
{
20+
return options.UseMongoDB(x => { x.DatabaseConnection = connectionString; });
21+
}
2222

23-
public static CapOptions UseMongoDB(this CapOptions options, Action<MongoDBOptions> configure)
24-
{
25-
if (configure == null) throw new ArgumentNullException(nameof(configure));
23+
public static CapOptions UseMongoDB(this CapOptions options, Action<MongoDBOptions> configure)
24+
{
25+
if (configure == null) throw new ArgumentNullException(nameof(configure));
2626

27-
configure += x => x.Version = options.Version;
27+
configure += x => x.Version = options.Version;
2828

29-
options.RegisterExtension(new MongoDBCapOptionsExtension(configure));
29+
options.RegisterExtension(new MongoDBCapOptionsExtension(configure));
3030

31-
return options;
32-
}
31+
return options;
3332
}
3433
}

src/DotNetCore.CAP.MongoDB/ICapTransaction.MongoDB.cs

+62-60
Original file line numberDiff line numberDiff line change
@@ -9,81 +9,83 @@
99
using MongoDB.Driver;
1010

1111
// ReSharper disable once CheckNamespace
12-
namespace DotNetCore.CAP
12+
namespace DotNetCore.CAP;
13+
14+
public class MongoDBCapTransaction : CapTransactionBase
1315
{
14-
public class MongoDBCapTransaction : CapTransactionBase
16+
public MongoDBCapTransaction(IDispatcher dispatcher)
17+
: base(dispatcher)
1518
{
16-
public MongoDBCapTransaction(IDispatcher dispatcher)
17-
: base(dispatcher)
18-
{
19-
}
19+
}
2020

21-
public override void Commit()
22-
{
23-
Debug.Assert(DbTransaction != null);
21+
public override void Commit()
22+
{
23+
Debug.Assert(DbTransaction != null);
2424

25-
if (DbTransaction is IClientSessionHandle session) session.CommitTransaction();
25+
if (DbTransaction is IClientSessionHandle session) session.CommitTransaction();
2626

27-
Flush();
28-
}
27+
Flush();
28+
}
2929

30-
public override async Task CommitAsync(CancellationToken cancellationToken = default)
31-
{
32-
Debug.Assert(DbTransaction != null);
30+
public override async Task CommitAsync(CancellationToken cancellationToken = default)
31+
{
32+
Debug.Assert(DbTransaction != null);
3333

34-
if (DbTransaction is IClientSessionHandle session) await session.CommitTransactionAsync(cancellationToken);
34+
if (DbTransaction is IClientSessionHandle session)
35+
await session.CommitTransactionAsync(cancellationToken).ConfigureAwait(false);
3536

36-
Flush();
37-
}
37+
Flush();
38+
}
3839

39-
public override void Rollback()
40-
{
41-
Debug.Assert(DbTransaction != null);
40+
public override void Rollback()
41+
{
42+
Debug.Assert(DbTransaction != null);
4243

43-
if (DbTransaction is IClientSessionHandle session) session.AbortTransaction();
44-
}
44+
if (DbTransaction is IClientSessionHandle session) session.AbortTransaction();
45+
}
4546

46-
public override async Task RollbackAsync(CancellationToken cancellationToken = default)
47-
{
48-
Debug.Assert(DbTransaction != null);
47+
public override async Task RollbackAsync(CancellationToken cancellationToken = default)
48+
{
49+
Debug.Assert(DbTransaction != null);
50+
51+
if (DbTransaction is IClientSessionHandle session)
52+
await session.AbortTransactionAsync(cancellationToken).ConfigureAwait(false);
53+
}
54+
55+
public override void Dispose()
56+
{
57+
(DbTransaction as IClientSessionHandle)?.Dispose();
58+
DbTransaction = null;
59+
}
60+
}
61+
62+
public static class CapTransactionExtensions
63+
{
64+
public static ICapTransaction Begin(this ICapTransaction transaction,
65+
IClientSessionHandle dbTransaction, bool autoCommit = false)
66+
{
67+
if (!dbTransaction.IsInTransaction) dbTransaction.StartTransaction();
4968

50-
if (DbTransaction is IClientSessionHandle session) await session.AbortTransactionAsync(cancellationToken);
51-
}
69+
transaction.DbTransaction = dbTransaction;
70+
transaction.AutoCommit = autoCommit;
5271

53-
public override void Dispose()
54-
{
55-
(DbTransaction as IClientSessionHandle)?.Dispose();
56-
DbTransaction = null;
57-
}
72+
return transaction;
5873
}
5974

60-
public static class CapTransactionExtensions
75+
/// <summary>
76+
/// Start the CAP transaction
77+
/// </summary>
78+
/// <param name="client">The <see cref="IMongoClient" />.</param>
79+
/// <param name="publisher">The <see cref="ICapPublisher" />.</param>
80+
/// <param name="autoCommit">Whether the transaction is automatically committed when the message is published</param>
81+
/// <returns>The <see cref="IClientSessionHandle" /> of MongoDB transaction session object.</returns>
82+
public static IClientSessionHandle StartTransaction(this IMongoClient client,
83+
ICapPublisher publisher, bool autoCommit = false)
6184
{
62-
public static ICapTransaction Begin(this ICapTransaction transaction,
63-
IClientSessionHandle dbTransaction, bool autoCommit = false)
64-
{
65-
if (!dbTransaction.IsInTransaction) dbTransaction.StartTransaction();
66-
67-
transaction.DbTransaction = dbTransaction;
68-
transaction.AutoCommit = autoCommit;
69-
70-
return transaction;
71-
}
72-
73-
/// <summary>
74-
/// Start the CAP transaction
75-
/// </summary>
76-
/// <param name="client">The <see cref="IMongoClient" />.</param>
77-
/// <param name="publisher">The <see cref="ICapPublisher" />.</param>
78-
/// <param name="autoCommit">Whether the transaction is automatically committed when the message is published</param>
79-
/// <returns>The <see cref="IClientSessionHandle" /> of MongoDB transaction session object.</returns>
80-
public static IClientSessionHandle StartTransaction(this IMongoClient client,
81-
ICapPublisher publisher, bool autoCommit = false)
82-
{
83-
var clientSessionHandle = client.StartSession();
84-
publisher.Transaction.Value = ActivatorUtilities.CreateInstance<MongoDBCapTransaction>(publisher.ServiceProvider);
85-
var capTrans = publisher.Transaction.Value.Begin(clientSessionHandle, autoCommit);
86-
return new CapMongoDbClientSessionHandle(capTrans);
87-
}
85+
var clientSessionHandle = client.StartSession();
86+
publisher.Transaction.Value =
87+
ActivatorUtilities.CreateInstance<MongoDBCapTransaction>(publisher.ServiceProvider);
88+
var capTrans = publisher.Transaction.Value.Begin(clientSessionHandle, autoCommit);
89+
return new CapMongoDbClientSessionHandle(capTrans);
8890
}
8991
}

0 commit comments

Comments
 (0)