如何确保线程安全的Web API基本控制器方法

时间:2016-05-28 04:22:06

标签: c# multithreading asp.net-web-api thread-safety

我有一个Web API服务,它将接受访问两个SQL Server数据库的不同客户端的许多并发请求,所有控制器(大约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);
            }
        }

异常消息:

  

无法在对象&#39; dbo.SyncMetadataClient&#39;中插入重复的键行。   具有唯一索引&#39; SyncMetadataClient_ix00&#39;。重复键值   是(70db3459-32ad-4557-9fac-405b82a5a349,用户)。

上述消息表示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;
        }

有关确保线程安全的良好实践的建议,还是我应该锁定资源访问代码并继续前进?

1 个答案:

答案 0 :(得分:3)

我相信你可以从使用某种名为&#34;&#34;锁,例如每个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
    }
}