Skip to content

Commit

Permalink
[fix]修正UI线程卡死问题,在同步调用异步时,取消捕获上下文。同时进行本机压测,并不存在连接数大增的问题。相关讨论:#140#139
Browse files Browse the repository at this point in the history
… 以及 #138
  • Loading branch information
nnhy committed Nov 25, 2024
1 parent da2497c commit 17c82ca
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion NewLife.Redis/NewLife.Redis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="11.0.2024.1115" />
<PackageReference Include="NewLife.Core" Version="11.0.2024.1125-beta0803" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 9 additions & 2 deletions NewLife.Redis/Queues/RedisReliableQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,23 @@ public Int32 Acknowledge(params String[] keys)
public RedisDelayQueue<T> InitDelay()
{
if (_delay == null)
{
lock (this)
if (_delay == null)
_delay = new RedisDelayQueue<T>(Redis, $"{Key}:Delay");
{
_delay ??= new RedisDelayQueue<T>(Redis, $"{Key}:Delay");
}
}
if (_delayTask == null || _delayTask.IsCompleted)
{
lock (this)
{
if (_delayTask == null || _delayTask.IsCompleted)
{
_source = new CancellationTokenSource();
_delayTask = Task.Run(() => _delay.TransferAsync(this, null, _source.Token));
}
}
}

return _delay;
}
Expand Down
10 changes: 5 additions & 5 deletions NewLife.Redis/Redis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ public virtual RedisClient StartPipeline()
// 管道处理不需要重试
try
{
return rds.StopPipeline(requireResult).Result;
return rds.StopPipeline(requireResult).ConfigureAwait(false).GetAwaiter().GetResult();
}
finally
{
Expand Down Expand Up @@ -1062,8 +1062,8 @@ protected override Int32 GetTimesPerThread(Boolean rand, Int32 batch)
/// <param name="batch">批量操作</param>
protected override Int64 BenchInc(String[] keys, Int64 times, Int32 threads, Boolean rand, Int32 batch)
{
//if (rand && batch > 10) times /= 10;
return base.BenchRemove(keys, times, threads, rand, batch);
if (rand && batch > 10) times /= 10;
return base.BenchInc(keys, times, threads, rand, batch);
}

/// <summary>删除测试</summary>
Expand All @@ -1075,8 +1075,8 @@ protected override Int64 BenchInc(String[] keys, Int64 times, Int32 threads, Boo
/// <returns></returns>
protected override Int64 BenchRemove(String[] keys, Int64 times, Int32 threads, Boolean rand, Int32 batch)
{
if (rand && batch > 10) times /= 10;
return base.BenchInc(keys, times, threads, rand, batch);
if (rand && batch > 10) times *= 10;
return base.BenchRemove(keys, times, threads, rand, batch);
}
#endregion

Expand Down
10 changes: 6 additions & 4 deletions NewLife.Redis/RedisClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ protected override void Dispose(Boolean disposing)
};

var uri = Server;
await tc.ConnectAsync(uri.Address, uri.Port);
var addrs = uri.GetAddresses();
DefaultSpan.Current?.AppendTag($"addrs={addrs.Join()} port={uri.Port}");
await tc.ConnectAsync(addrs, uri.Port);

Client = tc;
ns = tc.GetStream();
Expand Down Expand Up @@ -394,7 +396,7 @@ private async Task CheckSelect(String? cmd)
/// <summary>重置。干掉历史残留数据</summary>
public void Reset()
{
var ns = GetStreamAsync(false).Result;
var ns = GetStreamAsync(false).ConfigureAwait(false).GetAwaiter().GetResult();
if (ns == null) return;

// 干掉历史残留数据
Expand Down Expand Up @@ -588,7 +590,7 @@ private Int32 GetCommandSize(String cmd, Object?[]? args)
return default;
}

var rs = ExecuteAsync(cmd, args).Result;
var rs = ExecuteAsync(cmd, args).ConfigureAwait(false).GetAwaiter().GetResult();
if (rs == null) return default;
if (rs is TResult rs2) return rs2;
if (TryChangeType(rs, typeof(TResult), out var target)) return (TResult?)target;
Expand All @@ -603,7 +605,7 @@ private Int32 GetCommandSize(String cmd, Object?[]? args)
/// <returns></returns>
public virtual Boolean TryExecute<TResult>(String cmd, Object?[] args, out TResult? value)
{
var rs = ExecuteAsync(cmd, args).Result;
var rs = ExecuteAsync(cmd, args).ConfigureAwait(false).GetAwaiter().GetResult();
if (rs is TResult rs2)
{
value = rs2;
Expand Down
2 changes: 1 addition & 1 deletion Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="11.0.2024.1115" />
<PackageReference Include="NewLife.Core" Version="11.0.2024.1125-beta0803" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion XUnitTest/XUnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NewLife.Core" Version="11.0.2024.1115" />
<PackageReference Include="NewLife.Core" Version="11.0.2024.1125-beta0803" />
<PackageReference Include="NewLife.UnitTest" Version="1.0.2024.1006" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
Expand Down

0 comments on commit 17c82ca

Please sign in to comment.