V 1.1.5 修改并发限制逻辑和位置
This commit is contained in:
parent
7f269c8b04
commit
b5ed6a3b1c
@ -61,8 +61,8 @@ namespace LMS.service.Controllers
|
|||||||
// 判断请求任务的状态,判断是不是需要释放
|
// 判断请求任务的状态,判断是不是需要释放
|
||||||
if (res.StatusCode != 200 && res.StatusCode != 201)
|
if (res.StatusCode != 200 && res.StatusCode != 201)
|
||||||
{
|
{
|
||||||
// 释放
|
// 标记释放
|
||||||
_usageTracker.ReleaseConcurrencyPermit(requestToken);
|
HttpContext.Items["ReleaseConcurrencyPermit"] = true;
|
||||||
_logger.LogInformation($"请求失败,Token并发许可已释放: {token}, 状态码: {res.StatusCode}");
|
_logger.LogInformation($"请求失败,Token并发许可已释放: {token}, 状态码: {res.StatusCode}");
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -75,22 +75,13 @@ namespace LMS.service.Controllers
|
|||||||
description = "提交成功",
|
description = "提交成功",
|
||||||
result = 1320098173412546
|
result = 1320098173412546
|
||||||
});
|
});
|
||||||
if (result == null)
|
if (result == null || (result.code != 1 && result.code != 22))
|
||||||
{
|
{
|
||||||
// 失败
|
HttpContext.Items["ReleaseConcurrencyPermit"] = true;
|
||||||
_usageTracker.ReleaseConcurrencyPermit(requestToken);
|
_logger.LogInformation($"业务逻辑失败,返回结果为空或者返回请求码不是 1 或 22, 标记释放Token并发许可: {token}");
|
||||||
_logger.LogInformation($"请求失败,返回的请求体为空,Token并发许可已释放: {token}, 状态码: {res.StatusCode}");
|
|
||||||
return res;
|
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());
|
await _taskConcurrencyManager.CreateTaskAsync(requestToken, result.result.ToString());
|
||||||
|
|
||||||
|
|||||||
@ -129,20 +129,25 @@ namespace LMS.service.Extensions.Attributes
|
|||||||
context.HttpContext.Items["UseToken"] = tokenConfig.UseToken;
|
context.HttpContext.Items["UseToken"] = tokenConfig.UseToken;
|
||||||
context.HttpContext.Items["RequestToken"] = _token;
|
context.HttpContext.Items["RequestToken"] = _token;
|
||||||
|
|
||||||
|
// 再执行请求之前就新增使用计数,再请求之后,判断是不是成功请求,如果失败就释放
|
||||||
|
tokenService.IncrementUsage(_token);
|
||||||
|
|
||||||
// 执行 Action
|
// 执行 Action
|
||||||
var executedContext = await next();
|
var executedContext = await next();
|
||||||
|
|
||||||
// 6. 更新使用计数 (仅成功请求)
|
// 检查是否需要释放许可
|
||||||
if (executedContext.HttpContext.Response.StatusCode < 400)
|
var shouldRelease = executedContext.HttpContext.Items.ContainsKey("ReleaseConcurrencyPermit");
|
||||||
{
|
|
||||||
tokenService.IncrementUsage(_token);
|
|
||||||
|
|
||||||
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
|
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)
|
catch (Exception ex)
|
||||||
|
|||||||
@ -12,13 +12,11 @@ using Microsoft.Extensions.Options;
|
|||||||
using Qiniu.Http;
|
using Qiniu.Http;
|
||||||
using Qiniu.IO;
|
using Qiniu.IO;
|
||||||
using Qiniu.IO.Model;
|
using Qiniu.IO.Model;
|
||||||
using Qiniu.RS;
|
|
||||||
using Qiniu.Util;
|
using Qiniu.Util;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using static LMS.Common.Enums.ResponseCodeEnum;
|
using static LMS.Common.Enums.ResponseCodeEnum;
|
||||||
using static LMS.Repository.DTO.FileUploadDto;
|
using static LMS.Repository.DTO.FileUploadDto;
|
||||||
using static LMS.Repository.FileUpload.FileRequestReturn;
|
using static LMS.Repository.FileUpload.FileRequestReturn;
|
||||||
using Options = LMS.Repository.DB.Options;
|
|
||||||
|
|
||||||
namespace LMS.service.Service.FileUploadService
|
namespace LMS.service.Service.FileUploadService
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user