gpt4 book ai didi

c# - 如何确保线程安全的 Web API 基础 Controller 方法

转载 作者:行者123 更新时间:2023-11-30 15:21:30 25 4
gpt4 key购买 nike

我有一个 Web API 服务,它将接受访问两个 SQL Server 数据库的不同客户端的许多并发请求,所有 Controller (大约 65 个)都有一个 BaseApiController,在下面找到通用方法。

protected async Task<IEnumerable<TObject>> GetSyncData<TObject>(
Guid clientId,
string clientSyncTable,
Func<Task<IEnumerable<TObject>>> initialDataFunc,
Func<ISyncMetadataClient, Task<IEnumerable<TObject>>> sinceLastSyncDataFunc,
Func<ISyncMetadataClient, Task<bool>> insertClientSyncMetadata,
Func<IEnumerable<ISyncObject>, IEnumerable<TSyncMetadata>> getSyncMetadataFromSyncObjects,
Func<IEnumerable<TObject>, Task<IEnumerable<TObject>>> getExistingObjectsWithSyncMetadata) where TObject : ISyncObject
{
// Get the client sync metadata.
ISyncMetadataClient syncMetadataClient =
await SyncDataService.ClientSyncMetadata.GetFirstAsync(new { ClientId = clientId, SyncTable = clientSyncTable })
.ConfigureAwait(false);
// No metadata for this client and this resource. Use a lock or a better thread safe practice?
if (syncMetadataClient == null)
{
// Client first time syncing now, return initial data.
IEnumerable<TObject> data = await initialDataFunc().ConfigureAwait(false);
if (data.Any())
{
await CheckForMetadataMissingAndAppendAny(getSyncMetadataFromSyncObjects, getExistingObjectsWithSyncMetadata, data)
.ConfigureAwait(false);
// Here I cought exception trying to insert duplicate key.
await insertClientSyncMetadata(new SyncMetadataClient
{
SyncMetadataClientId = Guid.NewGuid(),
ClientId = clientId,
LastSyncUtcDateTime = DateTime.UtcNow,
SyncTable = clientSyncTable
}).ConfigureAwait(false);
}
return data;
}

// We have ClientSyncMetadata return all data since LastSyncDateTime.
IEnumerable<TObject> sinceLastSyncData = await sinceLastSyncDataFunc(syncMetadataClient).ConfigureAwait(false);
return sinceLastSyncData;
}

private async Task CheckForMetadataMissingAndAppendAny<TObject>(
Func<IEnumerable<ISyncObject>, IEnumerable<TSyncMetadata>> getSyncMetadataFromSyncObjects,
Func<IEnumerable<TObject>, Task<IEnumerable<TObject>>> getExistingObjectsWithSyncMetadata,
IEnumerable<TObject> data)
where TObject : ISyncObject
{
// Get the records that have metadata
IEnumerable<TObject> existingDataWithSyncMetadata = await getExistingObjectsWithSyncMetadata(data).ConfigureAwait(false);
// Select the records that has no metadata
IEnumerable<TObject> dataWithNoMetadata;
if (!existingDataWithSyncMetadata.Any())
{
dataWithNoMetadata = data.Where(p => p.CorrelationId == Guid.Empty).ToList();
}
else
{
dataWithNoMetadata = data.Where(p => !existingDataWithSyncMetadata.Any(item => p.CorrelationId == item.CorrelationId)).ToList();
}
// Unit of work transaction to insert all metadata in the database
IEnumerable<TSyncMetadata> syncMetadataItems = getSyncMetadataFromSyncObjects(dataWithNoMetadata.Select(p => p as ISyncObject)).ToList();
if (syncMetadataItems.Any())
{
TransactSyncMetadataCommand.AddRange(syncMetadataItems);
await TransactSyncMetadataCommand.ExecuteAsync().ConfigureAwait(false);
}
}

异常信息:

Cannot insert duplicate key row in object 'dbo.SyncMetadataClient' with unique index 'SyncMetadataClient_ix00'. The duplicate key value is (70db3459-32ad-4557-9fac-405b82a5a349, Users).

上述消息意味着 if (syncMetadataClient == null) 由一个或多个线程处理,而另一个线程正在将记录插入数据库。

我正在发布我正在使用的事务类,我在想我是否应该对该部分而不是 BaseApiController 进行线程安全,你觉得呢?

public async Task<TransactionResult> ExecuteAsync()
{
bool anyToInsert = DataToInsert.Any();
bool anyToUpdate = DataToUpdate.Any();
bool anyToDelete = DataToDelete.Any();
TransactionResult result = new TransactionResult();

if (anyToInsert ||
anyToUpdate ||
anyToDelete)
{
using (Connection)
using (IDbTransaction transaction = Connection.BeginTransaction())
{
try
{
// Add, Update and Delete stuff...

transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
result.Error = ex;
return result;
}
finally
{
DataToInsert.Clear();
DataToUpdate.Clear();
DataToDelete.Clear();
}
}
}
return result;
}

关于确保线程安全的良好做法有什么建议,还是我应该锁定资源访问代码然后继续?

最佳答案

我相信您可以从使用某种“命名”锁中受益,例如每个 ClientId + SyncTable 组合(或只是一个 ClientId)的不同锁。

这将帮助您以更灵活的方式处理并发,而不仅仅是为任何客户端请求锁定同一对象。

[更新] 如评论中所述,it is not allowed to use await inside a lock statement ,因此我用一种使用 SemaphoreSlim 的方法更新了答案。

您可以使用 ConcurrentDictionary 实现此方法:

private static ConcurrentDictionary<string, SemaphoreSlim> Locks = new ConcurrentDictionary<string, SemaphoreSlim>();

在你的方法中:

var lockKey = clientId + clientSyncTable;
var sem = Locks.GetOrAdd(lockKey, x => new SemaphoreSlim(1));

await sem.WaitAsync();
try {
ISyncMetadataClient syncMetadataClient =
await SyncDataService.ClientSyncMetadata.GetFirstAsync(
new {
ClientId = clientId,
SyncTable = clientSyncTable
}).ConfigureAwait(false);

if (syncMetadataClient == null)
{
//your other logic
}
}
finally {
sem.Release();
}

// other code

GetOrAdd 是一个原子操作,并且总是会返回一个您可能锁定的对象。

当然,这种方法只有在您的应用程序只有一个实例时才有效。

如果您出于任何原因将您的应用程序复制到多个实例中(例如虚拟化、云等),那么您的问题将再次出现。在这种情况下,我建议您考虑使用分布式锁机制(Redis 是实现此目的的一个很好的选择),保持您的逻辑不变。

分布式实例

这是一个使用 StackExchange.Redis 的分布式方法的小例子,假设 cacheStackExchange.Redis.IDatabaseAsync 的实例:

var timeout = TimeSpan.FromSeconds(5);
var lockKey = clientId + clientSyncTable;
RedisValue lockId = Guid.NewGuid().ToString();

if(await cache.LockTakeAsync(lockKey, lockId, timeout)) { //note that obtaining a distributed lock may fail
try {
ISyncMetadataClient syncMetadataClient =
await SyncDataService.ClientSyncMetadata.GetFirstAsync(
new {
ClientId = clientId,
SyncTable = clientSyncTable
}).ConfigureAwait(false);

if (syncMetadataClient == null)
{
//your other logic
}
} finally {
await cache.LockReleaseAsync(key, lockId); //release the same lock
}
}

关于c# - 如何确保线程安全的 Web API 基础 Controller 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37495156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com