大淘客网站如何建设,wordpress 个人网站,山东鑫泰建设集团网站,网站开发相关技术限流算法
以固定时间窗口算法和滑动时间窗口算法为例#xff0c;展开两种限流算法的分析。
固定时间窗口算法
在固定的时间窗口内#xff0c;设置允许固定数量的请求进入。如果超过设定的阈值就拒绝请求或者排队。
具体的#xff0c;按照时间划分为若干个时间窗口#…限流算法
以固定时间窗口算法和滑动时间窗口算法为例展开两种限流算法的分析。
固定时间窗口算法
在固定的时间窗口内设置允许固定数量的请求进入。如果超过设定的阈值就拒绝请求或者排队。
具体的按照时间划分为若干个时间窗口每个时间窗口里面的设置的请求数量的阈值都是相同的。一旦某个时间窗口内的请求数量达到阈值就会拒绝新的请求或者进入排队状态。
缺点是无法计算跨相邻时间窗口的请求数量是否达到阈值。
滑动时间窗口算法
在固定时间窗口的基础上时间窗口的起点和终点不再固定而是随着时间推移而不断变化但是每个时间窗口的长度终点-起点始终是固定的也就是说整体会随着时间的推移而不断滑动。
但是每次时间窗口的移动都需要重新统计请求数量会有一些重叠区域重复计算的问题因此可以对时间窗口进行更细粒度的划分增加一些子时间窗口即样本窗口。这样对时间窗口内部的请求数量的计算就会变成对相应的样本窗口内部的请求数量的计算再求和。如果超过阈值同样会被限流。
源码分析
以 Sentinel 内部的 LeapArray 的 currentWindow 方法为例解析如何根据指定时间获取对应的样本窗口。
流程概述
1、将指定时间 / 时间窗口的长度默认500ms再 % 样本窗口总数量得到所属的新的样本窗口对应的下标 n。
2、再通过指定时间 - 指定时间 % 时间窗口的长度得到对应样本窗口的开始时间。
3、从缓存中获取指定下标 n 的旧的样本窗口如果该样本窗口不存在则进行创建并返回。
4、比较新样本窗口的开始时间 t1 与旧样本窗口的开始时间 t2分为三种情况。
如果 t1 t2说明新样本窗口与旧样本窗口是相同的则返回旧样本窗口。如果 t1 t2说明旧样本窗口的状态滞后则重置旧样本窗口的所有指标再使用 LongAdder 计算某一指标并进行更新最后返回更新后的样本窗口。如果 t1 t2说明时间发生了倒流一般不会发生则创建新的样本窗口并返回。
LeapArray
public WindowWrapT currentWindow(long timeMillis) {if (timeMillis 0) {return null;}// 计算指定时间所属的样本窗口的下标int idx calculateTimeIdx(timeMillis);// 计算指定时间所属的样本窗口的起始时间点long windowStart calculateWindowStart(timeMillis);/** Get bucket item at given time from the array.** (1) Bucket is absent, then just create a new bucket and CAS update to circular array.* (2) Bucket is up-to-date, then just return the bucket.* (3) Bucket is deprecated, then reset current bucket and clean all deprecated buckets.*/while (true) {WindowWrapT old array.get(idx);if (old null) {/** B0 B1 B2 NULL B4* ||_______|_______|_______|_______|_______||___* 200 400 600 800 1000 1200 timestamp* ^* time888* bucket is empty, so create new and update** If the old bucket is absent, then we create a new bucket at {code windowStart},* then try to update circular array via a CAS operation. Only one thread can* succeed to update, while other threads yield its time slice.*/WindowWrapT window new WindowWrapT(windowLengthInMs, windowStart, newEmptyBucket(timeMillis));if (array.compareAndSet(idx, null, window)) {return window;} else {Thread.yield();}} else if (windowStart old.windowStart()) {/** B0 B1 B2 B3 B4* ||_______|_______|_______|_______|_______||___* 200 400 600 800 1000 1200 timestamp* ^* time888* startTime of Bucket 3: 800, so its up-to-date** If current {code windowStart} is equal to the start timestamp of old bucket,* that means the time is within the bucket, so directly return the bucket.*/return old;} else if (windowStart old.windowStart()) {/** (old)* B0 B1 B2 NULL B4* |_______||_______|_______|_______|_______|_______||___* ... 1200 1400 1600 1800 2000 2200 timestamp* ^* time1676* startTime of Bucket 2: 400, deprecated, should be reset** If the start timestamp of old bucket is behind provided time, that means* the bucket is deprecated. We have to reset the bucket to current {code windowStart}.* Note that the reset and clean-up operations are hard to be atomic,* so we need a update lock to guarantee the correctness of bucket update.** The update lock is conditional (tiny scope) and will take effect only when* bucket is deprecated, so in most cases it wont lead to performance loss.*/if (updateLock.tryLock()) {try {// 重置所有指标并计算PASS指标return resetWindowTo(old, windowStart);} finally {updateLock.unlock();}} else {Thread.yield();}} else if (windowStart old.windowStart()) {// 注意一般不会出现该情况该情况属于时间倒流return new WindowWrapT(windowLengthInMs, windowStart, newEmptyBucket(timeMillis));}}
}calculateTimeIdx
private int calculateTimeIdx(/*Valid*/ long timeMillis) {// windowLengthInMs默认是500long timeId timeMillis / windowLengthInMs;// array数组的长度默认是2return (int)(timeId % array.length());
}计算指定时间所属的样本窗口的下标。
calculateWindowStart
protected long calculateWindowStart(/*Valid*/ long timeMillis) {return timeMillis - timeMillis % windowLengthInMs;
}计算指定时间所属的样本窗口的开始时间。
resetWindowTo
以 OccupiableBucketLeapArray 为例。
Override
protected WindowWrapMetricBucket resetWindowTo(WindowWrapMetricBucket w, long time) {// 将windowStart设置为指定时间即样本窗口的开始时间w.resetTo(time);// 获取样本窗口的统计数据MetricBucket borrowBucket borrowArray.getWindowValue(time);if (borrowBucket ! null) {// 重置所有指标w.value().reset();// 计算PASS指标w.value().addPass((int)borrowBucket.pass());} else {// 重置所有指标w.value().reset();}return w;
}MetricBucket#reset
public MetricBucket reset() {for (MetricEvent event : MetricEvent.values()) {// 重置所有指标counters[event.ordinal()].reset();}// 初始化minRt属性initMinRt();return this;
}MetricEvent 是一个枚举类包括PASS, BLOCK, EXCEPTION, SUCCESS, RT, OCCUPIED_PASS。
MetricBucket#initMinRt
private void initMinRt() {// 获取 csp.sentinel.statistic.max.rt 属性值默认 5000并初始化minRt属性this.minRt SentinelConfig.statisticMaxRt();
}MetricBucket#addPass
public void addPass(int n) {// 计算PASS指标add(MetricEvent.PASS, n);
}public MetricBucket add(MetricEvent event, long n) {// 底层使用LongAdder计算指标counters[event.ordinal()].add(n);return this;
}
文章转载自: http://www.morning.bzwxr.cn.gov.cn.bzwxr.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.krqhw.cn.gov.cn.krqhw.cn http://www.morning.pjyrl.cn.gov.cn.pjyrl.cn http://www.morning.pngph.cn.gov.cn.pngph.cn http://www.morning.bpmnc.cn.gov.cn.bpmnc.cn http://www.morning.bfysg.cn.gov.cn.bfysg.cn http://www.morning.ymwny.cn.gov.cn.ymwny.cn http://www.morning.lkthj.cn.gov.cn.lkthj.cn http://www.morning.hhboyus.cn.gov.cn.hhboyus.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn http://www.morning.whnps.cn.gov.cn.whnps.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.jcnmy.cn.gov.cn.jcnmy.cn http://www.morning.hxlpm.cn.gov.cn.hxlpm.cn http://www.morning.wklyk.cn.gov.cn.wklyk.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.rgpbk.cn.gov.cn.rgpbk.cn http://www.morning.hfyll.cn.gov.cn.hfyll.cn http://www.morning.qwbtr.cn.gov.cn.qwbtr.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.rytps.cn.gov.cn.rytps.cn http://www.morning.rchsr.cn.gov.cn.rchsr.cn http://www.morning.kqylg.cn.gov.cn.kqylg.cn http://www.morning.ytmx.cn.gov.cn.ytmx.cn http://www.morning.xhqwm.cn.gov.cn.xhqwm.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.zhghd.cn.gov.cn.zhghd.cn http://www.morning.dgsr.cn.gov.cn.dgsr.cn http://www.morning.xdpjs.cn.gov.cn.xdpjs.cn http://www.morning.dmzzt.cn.gov.cn.dmzzt.cn http://www.morning.zlgbx.cn.gov.cn.zlgbx.cn http://www.morning.qrksj.cn.gov.cn.qrksj.cn http://www.morning.ywpcs.cn.gov.cn.ywpcs.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.bxgpy.cn.gov.cn.bxgpy.cn http://www.morning.ckrnq.cn.gov.cn.ckrnq.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn http://www.morning.rtsx.cn.gov.cn.rtsx.cn http://www.morning.qsbcg.cn.gov.cn.qsbcg.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.pbmkh.cn.gov.cn.pbmkh.cn http://www.morning.ghgck.cn.gov.cn.ghgck.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.qbksx.cn.gov.cn.qbksx.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.ktmbr.cn.gov.cn.ktmbr.cn http://www.morning.kdnbf.cn.gov.cn.kdnbf.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.wnjrf.cn.gov.cn.wnjrf.cn http://www.morning.jbkcs.cn.gov.cn.jbkcs.cn http://www.morning.2d1bl5.cn.gov.cn.2d1bl5.cn http://www.morning.jfbpf.cn.gov.cn.jfbpf.cn http://www.morning.bwdnx.cn.gov.cn.bwdnx.cn http://www.morning.wqbbc.cn.gov.cn.wqbbc.cn http://www.morning.rbbgh.cn.gov.cn.rbbgh.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.dbfwq.cn.gov.cn.dbfwq.cn http://www.morning.smzr.cn.gov.cn.smzr.cn http://www.morning.lmmyl.cn.gov.cn.lmmyl.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.mxlwl.cn.gov.cn.mxlwl.cn http://www.morning.ygrkg.cn.gov.cn.ygrkg.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.pclgj.cn.gov.cn.pclgj.cn http://www.morning.qpfmh.cn.gov.cn.qpfmh.cn http://www.morning.mlmwl.cn.gov.cn.mlmwl.cn http://www.morning.kgslc.cn.gov.cn.kgslc.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.ftrpvh.cn.gov.cn.ftrpvh.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn