From 18a9a7d44f0d3293e80a2eb750f46f12d33aba36 Mon Sep 17 00:00:00 2001 From: huangxiaoqiang <1247017146@qq.com> Date: 星期三, 16 四月 2025 10:53:48 +0800 Subject: [PATCH] 1 --- CodeManagement/WMS/WIDESEA_WMSServer/WIDESEA_Common/HttpClient/HttpsClient.cs | 34 +++++++++++++++++++++++++++++++--- 1 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CodeManagement/WMS/WIDESEA_WMSServer/WIDESEA_Common/HttpClient/HttpsClient.cs b/CodeManagement/WMS/WIDESEA_WMSServer/WIDESEA_Common/HttpClient/HttpsClient.cs index c30c9aa..5850eab 100644 --- a/CodeManagement/WMS/WIDESEA_WMSServer/WIDESEA_Common/HttpClient/HttpsClient.cs +++ b/CodeManagement/WMS/WIDESEA_WMSServer/WIDESEA_Common/HttpClient/HttpsClient.cs @@ -5,11 +5,24 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Timers; +using Timer = System.Timers.Timer; namespace WIDESEA_Common; public class HttpsClient { + private static readonly Timer cleanupTimer; + + static HttpsClient() + { + // 璁剧疆瀹氭椂鍣紝姣�10鍒嗛挓娓呯悊涓�娆equestTracker鐨勬暟鎹� + cleanupTimer = new Timer(10 * 60 * 1000); + cleanupTimer.Elapsed += CleanupRequestTracker; + cleanupTimer.AutoReset = true; + cleanupTimer.Enabled = true; + } + private static readonly LogFactory LogFactory = new LogFactory(); // 灏佽涓�涓敤HttpClient鍙戦�丟ET璇锋眰鐨勬柟娉曟湁鍙傛暟 @@ -44,14 +57,15 @@ // 鐢ㄤ簬杩借釜姣忎釜璇锋眰鐨勮皟鐢ㄦ鏁板拰鏈�鍚庤姹傛椂闂淬�� private static readonly Dictionary<string, (int Count, DateTime LastRequestTime)> requestTracker = new(); + // 灏佽涓�涓敤HttpClient鍙戦�丳ost璇锋眰鐨勬柟娉曟湁鍙傛暟 public static async Task<string> PostAsync(string url, Dictionary<string, object> parameters) { // 鍒涘缓涓�涓柊鐨勫瓧鍏革紝鎺掗櫎 RequestTime 鍜� SessionId - var filteredParameters = parameters.Where(p => p.Key != "RequestTime"&& p.Key != "SessionId").ToDictionary(p => p.Key, p => p.Value); + var filteredParameters = parameters.Where(p => p.Key != "RequestTime" && p.Key != "SessionId").ToDictionary(p => p.Key, p => p.Value); string requestKey = $"{url}:{JsonConvert.SerializeObject(filteredParameters)}"; - // 妫�鏌ヨ姹傛鏁板拰鏃堕棿闄愬埗 + // 妫�鏌ヨ姹傛鏁板拰鏃堕棿闄愬埗 if (requestTracker.TryGetValue(requestKey, out var requestInfo)) { if (requestInfo.Count >= 5 && DateTime.Now < requestInfo.LastRequestTime.AddMinutes(3)) @@ -60,7 +74,7 @@ Console.WriteLine("璇锋眰娆℃暟宸茶揪鍒伴檺鍒讹紝璇风◢鍚庡啀璇曘��"); Console.WriteLine($"鎺ュ彛鍦板潃锛歿url}"); Console.WriteLine($"璇锋眰鏁版嵁锛歿JsonConvert.SerializeObject(filteredParameters, Formatting.Indented)}"); - throw new Exception($"鎺ュ彛鍦板潃锛歿url}---璇锋眰娆℃暟宸茶揪鍒伴檺鍒讹紝璇风◢鍚庡啀璇曘��"); + throw new Exception($"鎺ュ彛鍦板潃锛歿url}---璇锋眰娆℃暟宸茶揪鍒伴檺鍒讹紝璇风◢鍚庡啀璇曘��"); } } @@ -137,4 +151,18 @@ } } + // 娓呯悊requestTracker鐨勬柟娉� + private static void CleanupRequestTracker(object sender, ElapsedEventArgs e) + { + // 鑾峰彇褰撳墠鏃堕棿 + DateTime now = DateTime.Now; + + // 閬嶅巻骞舵竻鐞唕equestTracker涓秴杩�10鍒嗛挓鐨勮褰� + var keysToRemove = requestTracker.Keys.Where(key => now > requestTracker[key].LastRequestTime.AddMinutes(10)).ToList(); + + foreach (var key in keysToRemove) + { + requestTracker.Remove(key); + } + } } \ No newline at end of file -- Gitblit v1.9.3