diff --git a/LMS.service/Controllers/MJPackageController.cs b/LMS.service/Controllers/MJPackageController.cs index 51d3efb..e2e509b 100644 --- a/LMS.service/Controllers/MJPackageController.cs +++ b/LMS.service/Controllers/MJPackageController.cs @@ -61,8 +61,8 @@ namespace LMS.service.Controllers // 判断请求任务的状态,判断是不是需要释放 if (res.StatusCode != 200 && res.StatusCode != 201) { - // 释放 - _usageTracker.ReleaseConcurrencyPermit(requestToken); + // 标记释放 + HttpContext.Items["ReleaseConcurrencyPermit"] = true; _logger.LogInformation($"请求失败,Token并发许可已释放: {token}, 状态码: {res.StatusCode}"); return res; } @@ -75,22 +75,13 @@ namespace LMS.service.Controllers description = "提交成功", result = 1320098173412546 }); - if (result == null) + if (result == null || (result.code != 1 && result.code != 22)) { - // 失败 - _usageTracker.ReleaseConcurrencyPermit(requestToken); - _logger.LogInformation($"请求失败,返回的请求体为空,Token并发许可已释放: {token}, 状态码: {res.StatusCode}"); + HttpContext.Items["ReleaseConcurrencyPermit"] = true; + _logger.LogInformation($"业务逻辑失败,返回结果为空或者返回请求码不是 1 或 22, 标记释放Token并发许可: {token}"); return res; } - else - { - if (result.code != 1 && result.code != 22) - { - _usageTracker.ReleaseConcurrencyPermit(requestToken); - _logger.LogInformation($"请求失败,code: {result.code},Token并发许可已释放: {token}, 状态码: {res.StatusCode}"); - return res; - } - } + // 开始写入任务 await _taskConcurrencyManager.CreateTaskAsync(requestToken, result.result.ToString()); diff --git a/LMS.service/Extensions/Attributes/RateLimitAttribute.cs b/LMS.service/Extensions/Attributes/RateLimitAttribute.cs index 0979834..4cf3e75 100644 --- a/LMS.service/Extensions/Attributes/RateLimitAttribute.cs +++ b/LMS.service/Extensions/Attributes/RateLimitAttribute.cs @@ -129,20 +129,25 @@ namespace LMS.service.Extensions.Attributes context.HttpContext.Items["UseToken"] = tokenConfig.UseToken; context.HttpContext.Items["RequestToken"] = _token; + // 再执行请求之前就新增使用计数,再请求之后,判断是不是成功请求,如果失败就释放 + tokenService.IncrementUsage(_token); + // 执行 Action var executedContext = await next(); - // 6. 更新使用计数 (仅成功请求) - if (executedContext.HttpContext.Response.StatusCode < 400) - { - tokenService.IncrementUsage(_token); + // 检查是否需要释放许可 + var shouldRelease = executedContext.HttpContext.Items.ContainsKey("ReleaseConcurrencyPermit"); - var duration = BeijingTimeExtension.GetBeijingTime() - _startTime; - logger.LogInformation($"请求处理成功: Token={_token}, 状态码={executedContext.HttpContext.Response.StatusCode}, 耗时={duration.TotalMilliseconds}ms"); + // 需要释放,直接释放并发许可 + if (executedContext.HttpContext.Response.StatusCode >= 400 || shouldRelease) + { + usageTracker.ReleaseConcurrencyPermit(_token); + logger.LogInformation($"请求失败,Token并发许可已释放: {_token}, 状态码={executedContext.HttpContext.Response.StatusCode}"); } else { - logger.LogWarning($"请求处理失败: Token={_token}, 状态码={executedContext.HttpContext.Response.StatusCode}"); + var duration = BeijingTimeExtension.GetBeijingTime() - _startTime; + logger.LogInformation($"请求处理成功: Token={_token}, 状态码={executedContext.HttpContext.Response.StatusCode}, 耗时={duration.TotalMilliseconds}ms"); } } catch (Exception ex) diff --git a/LMS.service/Service/FileUploadService/QiniuUploadService.cs b/LMS.service/Service/FileUploadService/QiniuUploadService.cs index f992533..c60afc1 100644 --- a/LMS.service/Service/FileUploadService/QiniuUploadService.cs +++ b/LMS.service/Service/FileUploadService/QiniuUploadService.cs @@ -12,13 +12,11 @@ using Microsoft.Extensions.Options; using Qiniu.Http; using Qiniu.IO; using Qiniu.IO.Model; -using Qiniu.RS; using Qiniu.Util; using System.Security.Cryptography; using static LMS.Common.Enums.ResponseCodeEnum; using static LMS.Repository.DTO.FileUploadDto; using static LMS.Repository.FileUpload.FileRequestReturn; -using Options = LMS.Repository.DB.Options; namespace LMS.service.Service.FileUploadService {