delay task

This commit is contained in:
Lightczx
2023-07-27 22:59:31 +08:00
parent de9abcfad4
commit 5f38c370c1
3 changed files with 47 additions and 14 deletions

View File

@@ -22,10 +22,10 @@ internal sealed class UniversalAnalyzer : DiagnosticAnalyzer
get get
{ {
return new DiagnosticDescriptor[] return new DiagnosticDescriptor[]
{ {
typeInternalDescriptor, typeInternalDescriptor,
readOnlyStructRefDescriptor, readOnlyStructRefDescriptor,
}.ToImmutableArray(); }.ToImmutableArray();
} }
} }
@@ -90,9 +90,9 @@ internal sealed class UniversalAnalyzer : DiagnosticAnalyzer
private void HandleMethodDeclaration(SyntaxNodeAnalysisContext context) private void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
{ {
MethodDeclarationSyntax methodSyntax = (MethodDeclarationSyntax)context.Node; MethodDeclarationSyntax methodSyntax = (MethodDeclarationSyntax)context.Node;
INamedTypeSymbol? returnTypeSymbol = context.SemanticModel.GetDeclaredSymbol(methodSyntax.ReturnType) as INamedTypeSymbol;
// 跳过异步方法,因为异步方法无法使用 ref in out // 跳过异步方法,因为异步方法无法使用 ref in out
if (methodSyntax.Modifiers.Any(token => token.IsKind(SyntaxKind.AsyncKeyword))) if (methodSyntax.Modifiers.Any(token => token.IsKind(SyntaxKind.AsyncKeyword)) || IsTaskOrValueTask(returnTypeSymbol))
{ {
return; return;
} }
@@ -163,7 +163,7 @@ internal sealed class UniversalAnalyzer : DiagnosticAnalyzer
} }
} }
private bool IsBuiltInType(ITypeSymbol symbol) private static bool IsBuiltInType(ITypeSymbol symbol)
{ {
return symbol.SpecialType switch return symbol.SpecialType switch
{ {
@@ -185,4 +185,23 @@ internal sealed class UniversalAnalyzer : DiagnosticAnalyzer
_ => false, _ => false,
}; };
} }
private static bool IsTaskOrValueTask(INamedTypeSymbol? symbol)
{
if (symbol == null)
{
return false;
}
string typeName = symbol.MetadataName;
if (typeName == "System.Threading.Tasks.Task" ||
typeName == "System.Threading.Tasks.Task`1" ||
typeName == "System.Threading.Tasks.ValueTask" ||
typeName == "System.Threading.Tasks.ValueTask`1")
{
return true;
}
return false;
}
} }

View File

@@ -0,0 +1,18 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Core.Threading;
internal readonly struct Delay
{
/// <summary>
/// 随机延迟
/// </summary>
/// <param name="minMilliSeconds">最小,闭</param>
/// <param name="maxMilliSeconds">最小,开</param>
/// <returns>任务</returns>
public static Task RandomAsync(int minMilliSeconds, int maxMilliSeconds)
{
return Task.Delay((int)(System.Random.Shared.NextDouble() * (maxMilliSeconds - minMilliSeconds)) + minMilliSeconds);
}
}

View File

@@ -161,11 +161,6 @@ internal sealed partial class GachaLogService : IGachaLogService
await gachaLogDbService.DeleteGachaArchiveByIdAsync(archive.InnerId).ConfigureAwait(false); await gachaLogDbService.DeleteGachaArchiveByIdAsync(archive.InnerId).ConfigureAwait(false);
} }
private static Task RandomDelayAsync(CancellationToken token)
{
return Task.Delay(TimeSpan.FromSeconds(Random.Shared.NextDouble() + 1), token);
}
private async Task<ValueResult<bool, GachaArchive?>> FetchGachaLogsAsync(GachaLogQuery query, bool isLazy, IProgress<GachaLogFetchStatus> progress, CancellationToken token) private async Task<ValueResult<bool, GachaArchive?>> FetchGachaLogsAsync(GachaLogQuery query, bool isLazy, IProgress<GachaLogFetchStatus> progress, CancellationToken token)
{ {
GachaLogFetchContext fetchContext = new(serviceProvider, context, isLazy); GachaLogFetchContext fetchContext = new(serviceProvider, context, isLazy);
@@ -214,7 +209,7 @@ internal sealed partial class GachaLogService : IGachaLogService
break; break;
} }
await RandomDelayAsync(token).ConfigureAwait(false); await Delay.RandomAsync(1000, 2000).ConfigureAwait(false);
} }
while (true); while (true);
@@ -225,7 +220,7 @@ internal sealed partial class GachaLogService : IGachaLogService
token.ThrowIfCancellationRequested(); token.ThrowIfCancellationRequested();
fetchContext.SaveItems(); fetchContext.SaveItems();
await RandomDelayAsync(token).ConfigureAwait(false); await Delay.RandomAsync(1000, 2000).ConfigureAwait(false);
} }
return new(!fetchContext.FetchStatus.AuthKeyTimeout, fetchContext.TargetArchive); return new(!fetchContext.FetchStatus.AuthKeyTimeout, fetchContext.TargetArchive);
@@ -283,6 +278,7 @@ internal sealed partial class GachaLogDbService : IGachaLogDbService
internal interface IGachaLogDbService internal interface IGachaLogDbService
{ {
ValueTask DeleteGachaArchiveByIdAsync(Guid archiveId); ValueTask DeleteGachaArchiveByIdAsync(Guid archiveId);
ObservableCollection<GachaArchive> GetGachaArchiveCollection(); ObservableCollection<GachaArchive> GetGachaArchiveCollection();
List<GachaItem> GetGachaItemListByArchiveId(Guid archiveId); List<GachaItem> GetGachaItemListByArchiveId(Guid archiveId);