Skip to content

Commit

Permalink
change CreateLock
Browse files Browse the repository at this point in the history
  • Loading branch information
fanliang11 committed Aug 13, 2019
1 parent ab9076c commit be778fe
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/Surging.Core/Surging.Core.Consul/ConsulServiceRouteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,26 @@ private async Task<List<IDistributedLock>> CreateLock()
var clients = await _consulClientProvider.GetClients();
foreach (var client in clients)
{
var distributedLock = await client.AcquireLock(new LockOptions($"lock_{_configInfo.RoutePath}")
var key = $"lock_{_configInfo.RoutePath}";
var writeResult = await client.KV.Get(key);
if (writeResult.Response != null)
{
SessionTTL = TimeSpan.FromSeconds(_configInfo.LockDelay),
LockTryOnce = true,
LockWaitTime = TimeSpan.FromSeconds(_configInfo.LockDelay)
}, _configInfo.LockDelay == 0 ?
default :
new CancellationTokenSource(TimeSpan.FromSeconds(_configInfo.LockDelay)).Token);

result.Add(distributedLock);
var distributedLock = await client.AcquireLock(key);
result.Add(distributedLock);
}
else
{
var distributedLock = await client.AcquireLock(new LockOptions($"lock_{_configInfo.RoutePath}")
{
SessionTTL = TimeSpan.FromSeconds(_configInfo.LockDelay),
LockTryOnce = true,
LockWaitTime = TimeSpan.FromSeconds(_configInfo.LockDelay)
}, _configInfo.LockDelay == 0 ?
default :
new CancellationTokenSource(TimeSpan.FromSeconds(_configInfo.LockDelay)).Token);
result.Add(distributedLock);
}

}
return result;
}
Expand Down

0 comments on commit be778fe

Please sign in to comment.