| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc.Controllers;
- using Microsoft.AspNetCore.Mvc.Filters;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using dodohold.core;
- using System.Threading.Channels;
- using YunhuiKit;
- namespace molilian.core
- {
- public partial class RiskControlCore
- {
- private static Dictionary<string, int> _calls = new();
- private static Dictionary<string, decimal> _incomeAmt = new();
- public static void Refresh()
- {
- _incomeAmt = [];
- }
- public static void ResetCalls()
- {
- _calls = [];
- }
- internal static async Task CallsIncrByAsync(TkChannelEnum channel, int accountId)
- {
- await CallsIncrByAsync(channel, accountId, DateTime.Now.ToString("yyyyMMdd"));
- await CallsIncrByAsync(channel, accountId, DateTime.Now.ToString("yyyyMMddHH"));
- }
- internal static async Task CallsIncrByAsync(TkChannelEnum channel, int accountId, string flag)
- {
- string key = $"{channel}:{accountId}:{flag}";
- if (_calls.ContainsKey(key))
- {
- _calls[key] += 1;
- }
- else
- {
- _calls[key] = 1;
- }
- string cache_key = $"RiskControl:{key}:calls:{flag}";
- await RedisKit.IncrByAsync(cache_key);
- await RedisKit.ExpireAsync(cache_key, 3 * 86400);
- }
- internal static void CallsIncrBy(TkChannelEnum channel, int accountId)
- {
- CallsIncrBy(channel, accountId, DateTime.Now.ToString("yyyyMMdd"));
- CallsIncrBy(channel, accountId, DateTime.Now.ToString("yyyyMMddHH"));
- }
- internal static void CallsIncrBy(TkChannelEnum channel, int accountId, string flag)
- {
- string key = $"{channel}:{accountId}:{flag}";
- if (_calls.ContainsKey(key))
- {
- _calls[key] += 1;
- }
- else
- {
- _calls[key] = 1;
- }
- string cache_key = $"RiskControl:{key}:calls:{flag}";
- RedisHelper.IncrBy(cache_key);
- RedisHelper.Expire(cache_key, 3 * 86400);
- }
- internal static async Task SetCallsAsync(TkChannelEnum channel, int accountId, string flag, int val)
- {
- string key = $"{channel}:{accountId}:{flag}";
- if (_calls.ContainsKey(key))
- {
- _calls[key] = val;
- }
- else
- {
- _calls[key] = val;
- }
- string cache_key = $"RiskControl:{key}:calls:{flag}";
- await RedisHelper.IncrByAsync(cache_key);
- await RedisHelper.ExpireAsync(cache_key, 3 * 86400);
- }
- public static async Task<int> GetAllNodesCallsAsync(TkChannelEnum channel, int accountId, string flag)
- {
- var key = $"{channel}:{accountId}:{flag}";
- try
- {
- var tasks = EndPointCore.List()
- .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
- .Select(async node =>
- {
- try
- {
- var redisServer = EndPointCore.GetRedisServer(node);
- if (string.IsNullOrEmpty(redisServer)) return 0;
- var cacheKey = $"RiskControl:{key}:calls:{flag}";
- using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
- return await scope.Client.GetAsync<int>(cacheKey);
- }
- catch (Exception ex) { }
- return 0;
- });
- var results = await Task.WhenAll(tasks);
- var totalCalls = results.Sum();
- _calls.TryAdd(key, totalCalls);
- return totalCalls;
- }
- catch (Exception)
- {
- // TODO: 添加日志
- return 0;
- }
- }
- internal static async Task SetTkEndpointCallsAsync(int accountId, int ep_id, string flag, int val)
- {
- string key = $"tk_endpoint:{accountId}:{ep_id}:{flag}";
- if (_calls.ContainsKey(key))
- {
- _calls[key] = val;
- }
- else
- {
- _calls[key] = val;
- }
- string cache_key = $"RiskControl:{key}:calls:{flag}";
- await RedisHelper.IncrByAsync(cache_key);
- await RedisHelper.ExpireAsync(cache_key, 3 * 86400);
- }
-
- public static async Task<int> GetAllNodesTkEndpointCallsAsync(int accountId, int ep_id, string flag)
- {
- string key = $"tk_endpoint:{accountId}:{ep_id}:{flag}";
- try
- {
- var tasks = EndPointCore.List()
- .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
- .Select(async node =>
- {
- try
- {
- var redisServer = EndPointCore.GetRedisServer(node);
- if (string.IsNullOrEmpty(redisServer)) return 0;
- string cacheKey = $"RiskControl:{key}:calls:{flag}";
- using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
- return await scope.Client.GetAsync<int>(cacheKey);
- }
- catch (Exception ex) { }
- return 0;
- });
- var results = await Task.WhenAll(tasks);
- var totalCalls = results.Sum();
- _calls.TryAdd(key, totalCalls);
- return totalCalls;
- }
- catch (Exception)
- {
- // TODO: 添加日志
- return 0;
- }
- }
- public static int GetCalls(TkChannelEnum channel, int accountId, string flag)
- {
- try
- {
- string key = $"{channel}:{accountId}:{flag}";
- if (_calls.ContainsKey(key)) return _calls[key];
- string cache_key = $"RiskControl:{key}:calls:{flag}";
- int num = RedisHelper.Get<int>(cache_key);
- _calls.TryAdd(key, num);
- return num;
- }
- catch (Exception ex) { return 0; }
- }
- internal static async Task SaveIncomeAmtAsync(TkChannelEnum channel, string accountName, decimal income_amt)
- {
- string key = $"{channel}:{accountName}:{DateTime.Now:yyyyMMdd}";
- lock (_incomeAmt)
- {
- if (!_incomeAmt.TryAdd(key, income_amt))
- _incomeAmt[key] = income_amt;
- }
- string cache_key = $"RiskControl:{key}:income_amt";
- try
- {
- var tasks = EndPointCore.List()
- .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
- .Select(async node =>
- {
- try
- {
- var redisServer = EndPointCore.GetRedisServer(node);
- if (string.IsNullOrEmpty(redisServer)) return false;
- using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
- return await scope.Client.SetAsync(cache_key, income_amt, 3 * 86400);
- }
- catch (Exception ex)
- {
- return false;
- }
- });
- await Task.WhenAll(tasks);
- }
- catch (Exception)
- {
- }
- }
- public static decimal GetIncomeAmt(TkChannelEnum channel, string accountName)
- {
- try
- {
- string key = $"{channel}:{accountName}:{DateTime.Now:yyyyMMdd}";
- if (_incomeAmt.ContainsKey(key)) return _incomeAmt[key];
- string cache_key = $"RiskControl:{key}:income_amt";
- return RedisHelper.Get<decimal>(cache_key);
- }
- catch (Exception ex) { return 0; }
- }
- }
- }
|