RiskControlCore.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.ContainsKey(key))
  35. {
  36. _calls[key] += 1;
  37. }
  38. else
  39. {
  40. _calls[key] = 1;
  41. }
  42. string cache_key = $"RiskControl:{key}:calls:{flag}";
  43. await RedisKit.IncrByAsync(cache_key);
  44. await RedisKit.ExpireAsync(cache_key, expire);
  45. }
  46. internal static async Task CallsIncrByAsync(TkChannelEnum channel, int accountId, string flag, double expire)
  47. {
  48. string key = $"{channel}:{accountId}:{flag}";
  49. if (_calls.ContainsKey(key))
  50. {
  51. _calls[key] += 1;
  52. }
  53. else
  54. {
  55. _calls[key] = 1;
  56. }
  57. string cache_key = $"RiskControl:{key}:calls:{flag}";
  58. await RedisKit.IncrByAsync(cache_key);
  59. await RedisKit.ExpireAsync(cache_key, expire);
  60. }
  61. internal static async Task SetCallsAsync(TkChannelEnum channel, int accountId, string flag, int val)
  62. {
  63. string key = $"{channel}:{accountId}:{flag}";
  64. if (_calls.ContainsKey(key))
  65. {
  66. _calls[key] = val;
  67. }
  68. else
  69. {
  70. _calls[key] = val;
  71. }
  72. string cache_key = $"RiskControl:{key}:calls:{flag}";
  73. await RedisHelper.IncrByAsync(cache_key);
  74. await RedisHelper.ExpireAsync(cache_key, 3 * 86400);
  75. }
  76. public static async Task<int> GetAllNodesCallsAsync(TkChannelEnum channel, int accountId, string flag)
  77. {
  78. var key = $"{channel}:{accountId}:{flag}";
  79. try
  80. {
  81. var tasks = EndPointCore.List()
  82. .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
  83. .Select(async node =>
  84. {
  85. try
  86. {
  87. var redisServer = EndPointCore.GetRedisServer(node);
  88. if (string.IsNullOrEmpty(redisServer)) return 0;
  89. var cacheKey = $"RiskControl:{key}:calls:{flag}";
  90. using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
  91. return await scope.Client.GetAsync<int>(cacheKey);
  92. }
  93. catch (Exception ex) { }
  94. return 0;
  95. });
  96. var results = await Task.WhenAll(tasks);
  97. var totalCalls = results.Sum();
  98. _calls.TryAdd(key, totalCalls);
  99. return totalCalls;
  100. }
  101. catch (Exception)
  102. {
  103. // TODO: 添加日志
  104. return 0;
  105. }
  106. }
  107. internal static async Task SetTkEndpointCallsAsync(int accountId, int ep_id, string flag, int val)
  108. {
  109. string key = $"tk_endpoint:{accountId}:{ep_id}:{flag}";
  110. if (_calls.ContainsKey(key))
  111. {
  112. _calls[key] = val;
  113. }
  114. else
  115. {
  116. _calls[key] = val;
  117. }
  118. string cache_key = $"RiskControl:{key}:calls:{flag}";
  119. await RedisHelper.IncrByAsync(cache_key);
  120. await RedisHelper.ExpireAsync(cache_key, 3 * 86400);
  121. }
  122. public static async Task<int> GetAllNodesTkEndpointCallsAsync(int accountId, int ep_id, string flag)
  123. {
  124. string key = $"tk_endpoint:{accountId}:{ep_id}:{flag}";
  125. try
  126. {
  127. var tasks = EndPointCore.List()
  128. .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
  129. .Select(async node =>
  130. {
  131. try
  132. {
  133. var redisServer = EndPointCore.GetRedisServer(node);
  134. if (string.IsNullOrEmpty(redisServer)) return 0;
  135. string cacheKey = $"RiskControl:{key}:calls:{flag}";
  136. using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
  137. return await scope.Client.GetAsync<int>(cacheKey);
  138. }
  139. catch (Exception ex) { }
  140. return 0;
  141. });
  142. var results = await Task.WhenAll(tasks);
  143. var totalCalls = results.Sum();
  144. _calls.TryAdd(key, totalCalls);
  145. return totalCalls;
  146. }
  147. catch (Exception)
  148. {
  149. // TODO: 添加日志
  150. return 0;
  151. }
  152. }
  153. public static int GetCalls(TkChannelEnum channel, int accountId, string flag)
  154. {
  155. try
  156. {
  157. string key = $"{channel}:{accountId}:{flag}";
  158. if (_calls.ContainsKey(key)) return _calls[key];
  159. string cache_key = $"RiskControl:{key}:calls:{flag}";
  160. int num = RedisHelper.Get<int>(cache_key);
  161. _calls.TryAdd(key, num);
  162. return num;
  163. }
  164. catch (Exception ex) { return 0; }
  165. }
  166. internal static async Task SaveIncomeAmtAsync(TkChannelEnum channel, string accountName, decimal income_amt)
  167. {
  168. string key = $"{channel}:{accountName}:{DateTime.Now:yyyyMMdd}";
  169. lock (_incomeAmt)
  170. {
  171. if (!_incomeAmt.TryAdd(key, income_amt))
  172. _incomeAmt[key] = income_amt;
  173. }
  174. string cache_key = $"RiskControl:{key}:income_amt";
  175. try
  176. {
  177. var tasks = EndPointCore.List()
  178. .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
  179. .Select(async node =>
  180. {
  181. try
  182. {
  183. var redisServer = EndPointCore.GetRedisServer(node);
  184. if (string.IsNullOrEmpty(redisServer)) return false;
  185. using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
  186. return await scope.Client.SetAsync(cache_key, income_amt, 3 * 86400);
  187. }
  188. catch (Exception ex)
  189. {
  190. return false;
  191. }
  192. });
  193. await Task.WhenAll(tasks);
  194. }
  195. catch (Exception)
  196. {
  197. }
  198. }
  199. public static decimal GetIncomeAmt(TkChannelEnum channel, string accountName)
  200. {
  201. try
  202. {
  203. string key = $"{channel}:{accountName}:{DateTime.Now:yyyyMMdd}";
  204. if (_incomeAmt.ContainsKey(key)) return _incomeAmt[key];
  205. string cache_key = $"RiskControl:{key}:income_amt";
  206. return RedisHelper.Get<decimal>(cache_key);
  207. }
  208. catch (Exception ex) { return 0; }
  209. }
  210. }
  211. }