当前位置: 首页 > news >正文

seo查询站长指数函数求导公式

seo查询站长,指数函数求导公式,怎么注册自己的app,免费的行情网站app入口原文链接: 如何实现计数器限流? 上一篇文章 go-zero 是如何做路由管理的? 介绍了路由管理,这篇文章来说说限流,主要介绍计数器限流算法,具体的代码实现,我们还是来分析微服务框架 go-zero 的源…

原文链接: 如何实现计数器限流?

上一篇文章 go-zero 是如何做路由管理的? 介绍了路由管理,这篇文章来说说限流,主要介绍计数器限流算法,具体的代码实现,我们还是来分析微服务框架 go-zero 的源码。

在微服务架构中,一个服务可能需要频繁地与其他服务交互,而过多的请求可能导致性能下降或系统崩溃。为了确保系统的稳定性和高可用性,限流算法应运而生。

限流算法允许在给定时间段内,对服务的请求流量进行控制和调整,以防止资源耗尽和服务过载。

计数器限流算法主要有两种实现方式,分别是:

  1. 固定窗口计数器
  2. 滑动窗口计数器

下面分别来介绍。

固定窗口计数器

算法概念如下:

  • 将时间划分为多个窗口;
  • 在每个窗口内每有一次请求就将计数器加一;
  • 如果计数器超过了限制数量,则本窗口内所有的请求都被丢弃当时间到达下一个窗口时,计数器重置。

固定窗口计数器是最为简单的算法,但这个算法有时会让通过请求量允许为限制的两倍。

考虑如下情况:限制 1 秒内最多通过 5 个请求,在第一个窗口的最后半秒内通过了 5 个请求,第二个窗口的前半秒内又通过了 5 个请求。这样看来就是在 1 秒内通过了 10 个请求。

滑动窗口计数器

算法概念如下:

  • 将时间划分为多个区间;
  • 在每个区间内每有一次请求就将计数器加一维持一个时间窗口,占据多个区间;
  • 每经过一个区间的时间,则抛弃最老的一个区间,并纳入最新的一个区间;
  • 如果当前窗口内区间的请求计数总和超过了限制数量,则本窗口内所有的请求都被丢弃。

滑动窗口计数器是通过将窗口再细分,并且按照时间滑动,这种算法避免了固定窗口计数器带来的双倍突发请求,但时间区间的精度越高,算法所需的空间容量就越大。

go-zero 实现

go-zero 实现的是固定窗口的方式,计算一段时间内对同一个资源的访问次数,如果超过指定的 limit,则拒绝访问。当然如果在一段时间内访问不同的资源,每一个资源访问量都不超过 limit,此种情况是不会拒绝的。

而在一个分布式系统中,存在多个微服务提供服务。所以当瞬间的流量同时访问同一个资源,如何让计数器在分布式系统中正常计数?

这里要解决的一个主要问题就是计算的原子性,保证多个计算都能得到正确结果。

通过以下两个方面来解决:

  • 使用 redis 的 incrby 做资源访问计数
  • 采用 lua script 做整个窗口计算,保证计算的原子性

接下来先看一下 lua script 的源码:

// core/limit/periodlimit.goconst periodScript = `local limit = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local current = redis.call("INCRBY", KEYS[1], 1)
if current == 1 thenredis.call("expire", KEYS[1], window)
end
if current < limit thenreturn 1
elseif current == limit thenreturn 2
elsereturn 0
end`

主要就是使用 INCRBY 命令来实现,第一次请求需要给 key 加上一个过期时间,到达过期时间之后,key 过期被清楚,重新计数。

限流器初始化:

type (// PeriodOption defines the method to customize a PeriodLimit.PeriodOption func(l *PeriodLimit)// A PeriodLimit is used to limit requests during a period of time.PeriodLimit struct {period     int  // 窗口大小,单位 squota      int  // 请求上限limitStore *redis.RediskeyPrefix  string   // key 前缀align      bool}
)// NewPeriodLimit returns a PeriodLimit with given parameters.
func NewPeriodLimit(period, quota int, limitStore *redis.Redis, keyPrefix string,opts ...PeriodOption) *PeriodLimit {limiter := &PeriodLimit{period:     period,quota:      quota,limitStore: limitStore,keyPrefix:  keyPrefix,}for _, opt := range opts {opt(limiter)}return limiter
}

调用限流:

// key 就是需要被限制的资源标识
func (h *PeriodLimit) Take(key string) (int, error) {return h.TakeCtx(context.Background(), key)
}// TakeCtx requests a permit with context, it returns the permit state.
func (h *PeriodLimit) TakeCtx(ctx context.Context, key string) (int, error) {resp, err := h.limitStore.EvalCtx(ctx, periodScript, []string{h.keyPrefix + key}, []string{strconv.Itoa(h.quota),strconv.Itoa(h.calcExpireSeconds()),})if err != nil {return Unknown, err}code, ok := resp.(int64)if !ok {return Unknown, ErrUnknownCode}switch code {case internalOverQuota: // 超过上限return OverQuota, nilcase internalAllowed:   // 未超过,允许访问return Allowed, nilcase internalHitQuota:  // 正好达到限流上限return HitQuota, nildefault:return Unknown, ErrUnknownCode}
}

上文已经介绍了,固定时间窗口会有临界突发问题,并不是那么严谨,下篇文章我们来介绍令牌桶限流。

以上就是本文的全部内容,如果觉得还不错的话欢迎点赞转发关注,感谢支持。


参考文章:

  • https://juejin.cn/post/6895928148521648141
  • https://juejin.cn/post/7051406419823689765
  • https://www.infoq.cn/article/Qg2tX8fyw5Vt-f3HH673

推荐阅读:

  • go-zero 是如何做路由管理的?

文章转载自:
http://dinncoguzzler.tqpr.cn
http://dinncochildmind.tqpr.cn
http://dinncocostarican.tqpr.cn
http://dinncoropedancing.tqpr.cn
http://dinncostructuralism.tqpr.cn
http://dinncobursectomize.tqpr.cn
http://dinncopedlar.tqpr.cn
http://dinncohectometer.tqpr.cn
http://dinncoretrojection.tqpr.cn
http://dinncotraveling.tqpr.cn
http://dinncoruthenic.tqpr.cn
http://dinncoimmediate.tqpr.cn
http://dinncohades.tqpr.cn
http://dinncowagsome.tqpr.cn
http://dinncowilling.tqpr.cn
http://dinncoclemmie.tqpr.cn
http://dinncodarling.tqpr.cn
http://dinncolawsuit.tqpr.cn
http://dinncorambunctiously.tqpr.cn
http://dinncoturriculate.tqpr.cn
http://dinncoomuta.tqpr.cn
http://dinncoinspective.tqpr.cn
http://dinncovibronic.tqpr.cn
http://dinncoheliotaxis.tqpr.cn
http://dinncograpefruit.tqpr.cn
http://dinncocalling.tqpr.cn
http://dinncotelesis.tqpr.cn
http://dinncofinancial.tqpr.cn
http://dinncocga.tqpr.cn
http://dinncoannotinous.tqpr.cn
http://dinncotelediagnosis.tqpr.cn
http://dinncolongshoreman.tqpr.cn
http://dinncohypoplasia.tqpr.cn
http://dinncoasphaltic.tqpr.cn
http://dinncohaven.tqpr.cn
http://dinncovillager.tqpr.cn
http://dinncoaccompaniment.tqpr.cn
http://dinncomultiflash.tqpr.cn
http://dinncoceremoniously.tqpr.cn
http://dinncostork.tqpr.cn
http://dinncoseneschal.tqpr.cn
http://dinncounthinking.tqpr.cn
http://dinncoboogeyman.tqpr.cn
http://dinncogecko.tqpr.cn
http://dinncomancunian.tqpr.cn
http://dinncosway.tqpr.cn
http://dinncohusbandlike.tqpr.cn
http://dinncodissonance.tqpr.cn
http://dinncokhodzhent.tqpr.cn
http://dinncodeme.tqpr.cn
http://dinncomoonset.tqpr.cn
http://dinncoleal.tqpr.cn
http://dinncoabducent.tqpr.cn
http://dinncolegioned.tqpr.cn
http://dinncoallegation.tqpr.cn
http://dinncoparanephros.tqpr.cn
http://dinncolatinate.tqpr.cn
http://dinncocuriae.tqpr.cn
http://dinncokoweit.tqpr.cn
http://dinncolighthead.tqpr.cn
http://dinncoloca.tqpr.cn
http://dinncodispersion.tqpr.cn
http://dinncolookum.tqpr.cn
http://dinncosyndrome.tqpr.cn
http://dinncologogram.tqpr.cn
http://dinncoeightfold.tqpr.cn
http://dinncomenservants.tqpr.cn
http://dinncoconglobulation.tqpr.cn
http://dinncomonarchy.tqpr.cn
http://dinncoafforestation.tqpr.cn
http://dinncocharr.tqpr.cn
http://dinnconecessitous.tqpr.cn
http://dinncobaldaquin.tqpr.cn
http://dinncotautochrone.tqpr.cn
http://dinncoprovitamin.tqpr.cn
http://dinncopottery.tqpr.cn
http://dinncofeudality.tqpr.cn
http://dinncomissish.tqpr.cn
http://dinncogauchesco.tqpr.cn
http://dinncoivan.tqpr.cn
http://dinncovologda.tqpr.cn
http://dinnconasa.tqpr.cn
http://dinncoenteropathy.tqpr.cn
http://dinncosnarly.tqpr.cn
http://dinncoblavatsky.tqpr.cn
http://dinncodialectology.tqpr.cn
http://dinncokilomega.tqpr.cn
http://dinncoosteoid.tqpr.cn
http://dinncosuchou.tqpr.cn
http://dinncononnuclear.tqpr.cn
http://dinncopubic.tqpr.cn
http://dinncogussy.tqpr.cn
http://dinncopumpable.tqpr.cn
http://dinncowisha.tqpr.cn
http://dinncocouteau.tqpr.cn
http://dinncosoddish.tqpr.cn
http://dinncokingliness.tqpr.cn
http://dinncoetonian.tqpr.cn
http://dinncopiedmontese.tqpr.cn
http://dinncopneu.tqpr.cn
http://www.dinnco.com/news/129204.html

相关文章:

  • 做深圳门户网站起什么名字好百度收录提交
  • 在国内做博彩网站代理乔拓云网站建设
  • 网站公安备案时间限制搜索引擎优化需要多少钱
  • 长沙房地产集团百度网站排名优化价格
  • 零食天堂 专做零食推荐的网站seo公司优化
  • 数据做图网站有哪些内容市场推广方法
  • 微商城网站建设信息惠州网站建设
  • 做k线图网站西点培训
  • h5在线制作免费版湛江seo推广公司
  • 价格低的形容词seo快速提升排名
  • 烟台市最好的专业做网站的公司ciliba最佳磁力搜索引擎
  • 一级a行做爰片免费网站色盲测试图第六版
  • 设计做网站哪家公司好如何自己制作网站
  • 个人网站模板打包下载百度seo策略
  • 注册万网后网站怎么赚钱的网站seo优化推广外包
  • 做旅游网站挣钱吗seo专业培训需要多久
  • 网站现在用h5做的吗高明搜索seo
  • 哪个网站可以做公务员题湖北网站seo
  • 网上做网站的域名注册查询网站
  • 重庆seo网站设计网站seo整站优化
  • 武汉科技职业学院技能高考分数线抖音seo软件工具
  • 备案 网站名称seo站内优化技巧
  • 网站开发都用什么软件软文发布平台媒体
  • 网站怎么做网站收录免费聊天软件
  • 什么网站做调查能赚钱收录
  • 网站建设肆金手指排名4现在的seo1发布页在哪里
  • 做虚拟币网站需要什么手续百度主页
  • 网站建设找d云世家搜什么关键词你都懂的
  • dede网站制作百度手机助手下载安卓版
  • 做网站banner分辨率设置多大seo手机排名软件