RiskControlCore.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using System.Threading.Channels;
  10. using YunhuiKit;
  11. namespace molilian.core
  12. {
  13. public partial class RiskControlCore
  14. {
  15. private static Dictionary<string, int> _calls = new();
  16. private static Dictionary<string, decimal> _incomeAmt = new();
  17. public static void Refresh()
  18. {
  19. _incomeAmt = [];
  20. }
  21. public static void ResetCalls()
  22. {
  23. _calls = [];
  24. }
  25. internal static async Task CallsIncrByAsync(TkChannelEnum channel, int accountId)
  26. {
  27. await CallsIncrByAsync(channel, accountId, DateTime.Now.ToString("yyyyMMddHHmm"), 300);
  28. await CallsIncrByAsync(channel, accountId, DateTime.Now.ToString("yyyyMMddHH"), 14400);
  29. await CallsIncrByAsync(channel, accountId, DateTime.Now.ToString("yyyyMMdd"));
  30. }
  31. internal static async Task CallsIncrByAsync(TkChannelEnum channel, int accountId, string flag, int expire = 259200)
  32. {
  33. string key = $"{channel}:{accountId}:{flag}";
  34. if (_calls.TryGetValue(key, out int value))
  35. {
  36. _calls[key] = ++value;
  37. }
  38. else
  39. {
  40. _calls[key] = 1;
  41. }
  42. string cache_key = $":RiskControl:calls:{key}";
  43. await RedisKit.IncrByAsync(cache_key);
  44. await RedisKit.ExpireAsync(cache_key, expire);
  45. }
  46. internal static async Task SetCallsAsync(TkChannelEnum channel, int accountId, string flag, int val)
  47. {
  48. string key = $"{channel}:{accountId}:{flag}";
  49. _calls.TryAdd(key, val);
  50. string cache_key = $":RiskControl:calls:{key}";
  51. await RedisHelper.SetAsync(cache_key, val);
  52. await RedisHelper.ExpireAsync(cache_key, 3 * 86400);
  53. }
  54. public static async Task<int> GetAllNodesCallsAsync(TkChannelEnum channel, int accountId, string flag)
  55. {
  56. var key = $"{channel}:{accountId}:{flag}";
  57. try
  58. {
  59. var tasks = EndPointCore.List()
  60. .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
  61. .Select(async node =>
  62. {
  63. try
  64. {
  65. var redisServer = EndPointCore.GetRedisServer(node);
  66. if (string.IsNullOrEmpty(redisServer)) return 0;
  67. var cacheKey = $":RiskControl:calls:{key}";
  68. using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
  69. int count = await scope.Client.GetAsync<int>(cacheKey);
  70. return count;
  71. }
  72. catch (Exception ex) { }
  73. return 0;
  74. });
  75. var results = await Task.WhenAll(tasks);
  76. var totalCalls = results.Sum();
  77. _calls.TryAdd(key, totalCalls);
  78. return totalCalls;
  79. }
  80. catch (Exception)
  81. {
  82. // TODO: 添加日志
  83. return 0;
  84. }
  85. }
  86. internal static async Task SetTkEndpointCallsAsync(int accountId, int ep_id, string flag, int val)
  87. {
  88. string key = $"tk_endpoint:{accountId}:{ep_id}:{flag}";
  89. if (_calls.ContainsKey(key))
  90. {
  91. _calls[key] = val;
  92. }
  93. else
  94. {
  95. _calls[key] = val;
  96. }
  97. string cache_key = $":RiskControl:calls:{key}";
  98. await RedisHelper.IncrByAsync(cache_key);
  99. await RedisHelper.ExpireAsync(cache_key, 3 * 86400);
  100. }
  101. public static async Task<int> GetAllNodesTkEndpointCallsAsync(int accountId, int ep_id, string flag)
  102. {
  103. string key = $"tk_endpoint:{accountId}:{ep_id}:{flag}";
  104. try
  105. {
  106. var tasks = EndPointCore.List()
  107. .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
  108. .Select(async node =>
  109. {
  110. try
  111. {
  112. var redisServer = EndPointCore.GetRedisServer(node);
  113. if (string.IsNullOrEmpty(redisServer)) return 0;
  114. string cacheKey = $":RiskControl:calls:{key}";
  115. using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
  116. return await scope.Client.GetAsync<int>(cacheKey);
  117. }
  118. catch (Exception ex) { }
  119. return 0;
  120. });
  121. var results = await Task.WhenAll(tasks);
  122. var totalCalls = results.Sum();
  123. _calls.TryAdd(key, totalCalls);
  124. return totalCalls;
  125. }
  126. catch (Exception)
  127. {
  128. // TODO: 添加日志
  129. return 0;
  130. }
  131. }
  132. public static int GetCalls(TkChannelEnum channel, int accountId, string flag)
  133. {
  134. try
  135. {
  136. string key = $"{channel}:{accountId}:{flag}";
  137. if (_calls.ContainsKey(key)) return _calls[key];
  138. string cache_key = $"RiskControl:calls:{key}";
  139. int num = RedisHelper.Get<int>(cache_key);
  140. _calls.TryAdd(key, num);
  141. return num;
  142. }
  143. catch (Exception ex) { return 0; }
  144. }
  145. internal static async Task SaveIncomeAmtAsync(TkChannelEnum channel, string accountName, decimal income_amt)
  146. {
  147. string key = $"{channel}:{accountName}:{DateTime.Now:yyyyMMdd}";
  148. lock (_incomeAmt)
  149. {
  150. if (!_incomeAmt.TryAdd(key, income_amt))
  151. _incomeAmt[key] = income_amt;
  152. }
  153. string cache_key = $"RiskControl:{key}:income_amt";
  154. try
  155. {
  156. var tasks = EndPointCore.List()
  157. .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
  158. .Select(async node =>
  159. {
  160. try
  161. {
  162. var redisServer = EndPointCore.GetRedisServer(node);
  163. if (string.IsNullOrEmpty(redisServer)) return false;
  164. using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
  165. return await scope.Client.SetAsync(cache_key, income_amt, 3 * 86400);
  166. }
  167. catch (Exception ex)
  168. {
  169. return false;
  170. }
  171. });
  172. await Task.WhenAll(tasks);
  173. }
  174. catch (Exception)
  175. {
  176. }
  177. }
  178. public static decimal GetIncomeAmt(TkChannelEnum channel, string accountName)
  179. {
  180. try
  181. {
  182. string key = $"{channel}:{accountName}:{DateTime.Now:yyyyMMdd}";
  183. if (_incomeAmt.ContainsKey(key)) return _incomeAmt[key];
  184. string cache_key = $":RiskControl:{key}:income_amt";
  185. return RedisHelper.Get<decimal>(cache_key);
  186. }
  187. catch (Exception ex) { return 0; }
  188. }
  189. }
  190. }