在上一篇文章中,博主带领大家一起深入了解 ConcurrencyLimiter 这个中间件,正当我得意洋洋地向 Catcher Wong 大佬吹嘘这一点小收获时,大佬一脸嫌弃地说,一个单机版的方案有什么好得意的啊。大佬言下之意,显然是指,这个中间件在分布式环境中毫无用武之地。其实,你只需要稍微想一下,就能想明白这个问题。毕竟,它只是通过SeamphoreSlim
控制线程数量而已,一旦放到分布式环境中,这个并发控制就被大大地削弱。所以,在今天这篇文章中,博主会带领大家一起“探案 ” ASP.NET Core 中的限流中间件 AspNetCoreRateLimite ,希望大家可以从中感悟到不一样的东西。对我而言,这可能是人到中年的焦虑感所催生出来的一种源动力,同时亦是为了不让那些订阅专栏的同学失望。
关于“限流 ”这个话题,我个人以为,它可以引申出非常多的东西,譬如“熔断”和“限流”,其实可以看作是同一类问题的“一体两面”。最早接触熔断,是源于 Spring Cloud 中的 Hystrix ,它其实是指当服务不可用的时候,客户端应该采取什么样的措施去应对,实际使用中我们可能会考虑重试、超时、降级等策略。相应地,当服务端在面对来自客户端的异常流量时,就产生了“限流”这个概念,“限流”可以是线程隔离( 线程数 + 队列大小限制),可以是信号量隔离( 设置最大并发请求数目),可以是限制QPS。这里,我们讨论的主要是第三种,而实现限流的常见算法主要有 计数器算法、 漏桶算法和 令牌桶算法。这里,AspNetCoreRateLimit 这个中间件,则主要使用了 计数器算法**,并配合 IMemoryCache
和 IDistributedCache
分别实现了基于内存和基于分布式缓存的持久化逻辑。
源代码解读 首先,使用者通过配置定义了一个或者多个规则,这些规则决定了每个客户端在访问特定终结点时,一段时间内可以访问的最大次数。 RateLimitMiddleware 通过注入的IRateLimitProcessor 来匹配规则,然后依次判断每个规则是否达到了限流条件。一旦达到限流条件,中间件会改变 HTTP 响应的状态码、响应头、返回值,告知使用者已达到最大调用次数。而针对每一种 IRateLimitProcessor ,主要通过ProcessRequestAsync()
方法来实现计数,如果上一次的请求对应的时间戳 + 规则中时间间隔 >= 当前时间,则说明请求没有过期,此时,就需要给这个计数增加1。好了,现在我们来针对 AspNetCoreRateLimit 中的核心部件逐个进行解读。
RateLimitProcessor RateLimitProcessor
,是一个抽象类,实现了IRateLimitProcessor
接口,公开的方法有 3 个:ProcessRequestAsync()
、IsWhitelisted()
和 GetRateLimitHeaders()
。在此基础上,派生出ClientRateLimitProcessor
和IpRateLimitProcessor
两个子类。两者最大的不同在于,其所依赖的Store
不同,前者为IClientPolicyStore
,后者IIpPolicyStore
,它们都实现了同一个接口IRateLimitStore
:
1 2 3 4 5 6 7 8 9 public interface IRateLimitStore<T> { Task<bool > ExistsAsync (string id, CancellationToken cancellationToken = default ) ; Task<T> GetAsync (string id, CancellationToken cancellationToken = default ) ; Task RemoveAsync (string id, CancellationToken cancellationToken = default ) ; Task SetAsync (string id, T entry, TimeSpan? expirationTime = null , CancellationToken cancellationToken = default ) ;}
可以注意到,这些都是典型的基于键-值的存储,所以,不管是基于内存的IMemeryCache,还是基于分布式缓存的IDistributedCache,都可以做到无缝切换。不同的Processor,本质上是它们生成缓存键的方式不同,例如,IpRateLimitProcessor
是用一个前缀来表示一组IP,而ClientRateLimitProcessor
则是用通过客户端前缀和客户端Id来作为区分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 public async Task<IEnumerable<RateLimitRule>> GetMatchingRulesAsync( ClientRequestIdentity identity, CancellationToken cancellationToken = default ) { var policies = await _policyStore.GetAsync( $"{_options.IpPolicyPrefix} " , cancellationToken ); var rules = new List<RateLimitRule>(); if (policies?.IpRules?.Any() == true ) { var matchPolicies = policies.IpRules .Where(r => IpParser.ContainsIp(r.Ip, identity.ClientIp)); foreach (var item in matchPolicies) { rules.AddRange(item.Rules); } } return GetMatchingRules(identity, rules); } public async Task<IEnumerable<RateLimitRule>> GetMatchingRulesAsync( ClientRequestIdentity identity, CancellationToken cancellationToken = default ) { var policy = await _policyStore.GetAsync( $"{_options.ClientPolicyPrefix} _{identity.ClientId} " , cancellationToken ); return GetMatchingRules(identity, policy?.Rules); }
对于RateLimitProcessor
而言,其实现思路是,通过CounterKeyBuilder
及其子类来生成计数器标识(CounterId ),然后再通过AsyncKeyLock
来实现计数,最终通过IRateLimitCounterStore
来实现存储:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 public virtual async Task<RateLimitCounter> ProcessRequestAsync ( ClientRequestIdentity requestIdentity, RateLimitRule rule, CancellationToken cancellationToken = default ) { var counter = new RateLimitCounter { Timestamp = DateTime.UtcNow, Count = 1 }; var counterId = BuildCounterKey(requestIdentity, rule); using (await AsyncLock.WriterLockAsync(counterId).ConfigureAwait(false )) { var entry = await _counterStore.GetAsync(counterId, cancellationToken); if (entry.HasValue) { if (entry.Value.Timestamp + rule.PeriodTimespan.Value >= DateTime.UtcNow) { var totalCount = entry.Value.Count + _config.RateIncrementer?.Invoke() ?? 1 ; counter = new RateLimitCounter { Timestamp = entry.Value.Timestamp, Count = totalCount }; } } await _counterStore.SetAsync( counterId, counter, rule.PeriodTimespan.Value, cancellationToken ); } return counter; }
AsyncKeyLock 在分析RateLimitProcessor
类的时候,我们提到了AsyncKeyLock
。对于AsyncKeyLock
的实现,我个人认为这是整个中间件的精华,因为这里出现了,和SeamphoreSlim
一样经典的东西,这里用到了自旋锁SpinLock
。我个人理解,SpinLock
约等于 Interlocked
+ 内核级别的while。这部分代码本身并不复杂,难就难在这样一个精妙的想法上面。其中,AsyncKeyLockDoorman 这个类的实现,应该是参考了微软的一篇博客—— Building Async Coordination Primitives, Part 7: AsyncReaderWriterLock ,因为ReaderLockAsync()
、WriterLockAsync()
、ReaderRelease()
和 WriterRelease()
这 4 个关键方法完全一样。结合限流这个场景来看,它是典型的“多写 ”场景,因为如果是相同的请求,那么就会产生相同的计数器标识(CounterId ),所以,这个AsyncLockDoorman
这个类所定义的上下文边界,其实是“一读多写 ”的问题,所以,我们可以注意到,它里面定义了一个“写”操作的队列_waitingWriters
,一个“读操作”的_waitingReader
:
1 2 3 4 5 6 7 8 9 10 11 12 public AsyncKeyLockDoorman (Action<AsyncKeyLockDoorman> reset ) { _waitingWriters = new Queue<TaskCompletionSource<Releaser>>(); _waitingReader = new TaskCompletionSource<Releaser>(); _status = 0 ; _readerReleaser = Task.FromResult(new Releaser(this , false )); _writerReleaser = Task.FromResult(new Releaser(this , true )); _reset = reset; }
对于“写”操作而言,当一个新的写入者希望进来的时候,如果此时锁没有被别人占用,那么这个新的写入者会获得这个锁,状态值m_status
会被修改为-1。反之,如果此时这个锁已经被别人占用了,那么这个新的写入者将会进入等待队列。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public Task<Releaser> WriterLockAsync ( ) { lock (_waitingWriters) { if (_status == 0 ) { _status = -1 ; return _writerReleaser; } else { var waiter = new TaskCompletionSource<Releaser>(); _waitingWriters.Enqueue(waiter); return waiter.Task; } } }
对于“读”操作而言,我们来思考这样一个问题,什么时候“读”操作会被允许呢?答案是这一时刻没有写入者正在“写”或者“等”,因为如果不这样的话,就会发生我们平常所说的“脏读 ”,所以,这种情况下,就必须强迫“读取者”去等待写入者“空闲”下来。此时,不难理解ReadLockAsync()
的实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public Task<Releaser> ReaderLockAsync ( ) { lock (_waitingWriters) { if (_status >= 0 && _waitingWriters.Count == 0 ) { ++_status; return _readerReleaser; } else { ++_readersWaiting; return _waitingReader.Task.ContinueWith(t => t.Result); } } }
现在,让我们把视线拉回到AsyncKeyLock
,它负责维护一组AsyncKeyLockDoorman
,其内部部通过一个字典来维护CounterId
和AsyncKeyLockDoorman
间的关系。与此同时,为了减少创建·AsyncKeyLockDoorman·带来的性能损耗,它使用一个栈来存储AsyncKeyLockDoorman
。每次获取AsyncKeyLockDoorman
的过程,本质上就是为指定的Key
分配AsyncKeyLockDoorman
的过程,同时会更新其引用数RefCount
。相应地,释放AsyncKeyLockDoorman
的过程,本质上就是减少其引用数RefCount
,从字典中移除指定Key
,“归还 ”对象池的过程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 private static AsyncKeyLockDoorman GetDoorman (string key ) { AsyncKeyLockDoorman doorman; bool lockTaken = false ; try { _spinLock.Enter(ref lockTaken); if (!Keys.TryGetValue(key, out doorman)) { doorman = (Pool.Count > 0 ) ? Pool.Pop() : new AsyncKeyLockDoorman(ReleaseDoorman); doorman.Key = key; Keys.Add(key, doorman); } doorman.RefCount++; } finally { if (lockTaken) { _spinLock.Exit(); } } return doorman; } private static void ReleaseDoorman (AsyncKeyLockDoorman doorman ) { bool lockTaken = false ; try { _spinLock.Enter(ref lockTaken); if (--doorman.RefCount == 0 ) { Keys.Remove(doorman.Key); if (Pool.Count < MaxPoolSize) { doorman.Key = null ; Pool.Push(doorman); } } } finally { if (lockTaken) { _spinLock.Exit(); } } }
RateLimitMiddleware OK,到这里,我们再回过头去看源代码解读 这里的内容,大概就可以串起来整合中间件的调用链路,Middleware
->RateLimteProcessor
->AsyncKeyLock
->AsyncKeyLockDoorman
,坦白来讲,我一直没能想明白为什么要用SpinLock
?难道仅仅是为了减少等待时间、提高性能吗?经过精简,我们发现,整个中间件的Invoke()
方法,大致要经历下面几个阶段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 public async Task Invoke (HttpContext context ) { if (_options == null ) { await _next.Invoke(context); return ; } var identity = await ResolveIdentityAsync(context); if (_processor.IsWhitelisted(identity)) { await _next.Invoke(context); return ; } var rulesDict = new Dictionary<RateLimitRule, RateLimitCounter>(); var rules = await _processor.GetMatchingRulesAsync( identity, context.RequestAborted ); foreach (var rule in rules) { var rateLimitCounter = await _processor.ProcessRequestAsync( identity, rule, context.RequestAborted ); if (rule.Limit > 0 ) { if (rateLimitCounter.Timestamp + rule.PeriodTimespan.Value < DateTime.UtcNow) { continue ; } if (rateLimitCounter.Count > rule.Limit) { var retryAfter = rateLimitCounter.Timestamp.RetryAfterFrom(rule); await ReturnQuotaExceededResponse(context, rule, retryAfter); return ; } } else { await ReturnQuotaExceededResponse( context, rule, int .MaxValue.ToString(System.Globalization.CultureInfo.InvariantCulture) ); } } await _next.Invoke(context); }
本文小结 作为 并发限制 这一篇的“姊妹篇”,这一篇的难度相对上一篇堪称“高山仰止”,主要的难点是 SpinLock 、“一读多写”的异步读写锁 AsyncKeyLock 以及 AsyncKeyLockDoorman 。如果大家感兴趣的话,可以去搜索一下 AsyncKeyLock 这个关键字,大家就会发现在好多开源项目 中都能找到类似的代码片段,莫非这是某种神奇的算法吗?阅读源代码,其实是一个无法“立竿见影”的学习方法,有时候我们要通过叙述或者表达来输出我们对待一件事物的看法。这是因为,我们自以为是的“学会”和真正的“学会”,这两者间可能千差万别,就像我最近在用 ABP vNext 搭建一个小项目,阅读文档的时候,眼睛觉得它“学会”了,而实际需要需要扩展或者替换 ABP 的实体/服务的时候。我的手会告诉我,它真的“不会”。做一个知难行易的“调包”侠也许会非常容易,可正因为如此,你要凸显自我就会非常困难。世上的事情,“夫夷以近,则游者众;险以远,则至者少。而世之奇伟、瑰怪,非常之观,常在于险远,而人之所罕至焉,故非有志者不能至也 ”,哪怕就是增长一下见识呢,你说对吧……