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

umu互动平台领硕网站seo优化

umu互动平台,领硕网站seo优化,html网页设计代码作业大一,wordpress文章摘要文字目录 一、单机限流 1、令牌桶算法 3、固定窗口限流算法 4、滑动窗口 二、集群限流 1、分布式固定窗口 (基于redis) 2、分布式滑动窗口 一、单机限流 1、令牌桶算法 令牌桶算法是当流量进入系统前需要获取令牌,没有令牌那么就要进行限…

目录

一、单机限流

1、令牌桶算法

3、固定窗口限流算法

4、滑动窗口

二、集群限流

1、分布式固定窗口 (基于redis)

2、分布式滑动窗口


一、单机限流

1、令牌桶算法

令牌桶算法是当流量进入系统前需要获取令牌,没有令牌那么就要进行限流

这个算法是怎么实现的呢

  1. 定义一个后台协程按照一定的频率去产生token

  2. 后台协程产生的token 放到固定大小容器里面

  3. 有流量进入系统尝试拿到token,没有token 就需要限流了


type TokenBucketLimiter struct {token chan struct{}stop  chan struct{}
}
​
func NewTokenBucket(capactity int, timeInternal time.Duration) *TokenBucketLimiter {te := make(chan struct{}, capactity)stop := make(chan struct{})ticker := time.NewTicker(timeInternal)go func() {defer ticker.Stop()for {select {case <-ticker.C:select {case te <- struct{}{}:default:
​}case <-stop:return}}}()return &TokenBucketLimiter{token: te,stop:  stop,}
}
​
func (t *TokenBucketLimiter) BuildServerInterceptor() grpc.UnaryServerInterceptor {return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {select {case <-ctx.Done():err = ctx.Err()returncase <-t.token:return handler(ctx, req)case <-t.stop:err = errors.New("缺乏保护")return}}
}
​
func (t *TokenBucketLimiter) Stop() {close(t.stop)
}

3、固定窗口限流算法

什么是固定窗口限流算法

固定窗口限流算法(Fixed Window Rate Limiting Algorithm)是一种最简单的限流算法,其原理是在固定时间窗口(单位时间)内限制请求的数量。该算法将时间分成固定的窗口,并在每个窗口内限制请求的数量。具体来说,算法将请求按照时间顺序放入时间窗口中,并计算该时间窗口内的请求数量,如果请求数量超出了限制,则拒绝该请求。

优点:实现简单

缺点:对于瞬时流量没发处理,也就是临界问题,比如下图在20t前后,在16t以及26t有大量流量进来,在这10t中,已经超过了流量限制,没法限流

实现如下

type fixWindow1 struct {lastVistTime int64vistCount    int64interval     int64maxCount     int64
}
​
func NewfixWindow1(macCount int64) *fixWindow1 {t := &fixWindow1{maxCount: macCount,}return t
}
​
func (f *fixWindow1) FixWindow1() grpc.UnaryServerInterceptor {return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {current := time.Now().UnixNano()lasttime := atomic.LoadInt64(&f.lastVistTime)if lasttime+f.interval > current {if atomic.CompareAndSwapInt64(&f.lastVistTime, lasttime, current) {atomic.StoreInt64(&f.lastVistTime, current)atomic.StoreInt64(&f.maxCount, 0)}}count := atomic.AddInt64(&f.vistCount, 1)if count > f.maxCount {return gen.GetByIDResp{}, errors.New("触发限流")}return handler(ctx, req)}
}

4、滑动窗口

什么是滑动窗口算法:

滑动窗口限流算法是一种常用的限流算法,用于控制系统对外提供服务的速率,防止系统被过多的请求压垮。它将单位时间周期分为n个小周期,分别记录每个小周期内接口的访问次数,并且根据时间滑动删除过期的小周期。它可以解决固定窗口临界值的问题

type slideWindow struct {
timeWindow *list.Listinterval   int64maxCnt     intlock       sync.Mutex
}
​
func NewSlideWindow(interval time.Duration, maxCnt int) *slideWindow {t := &slideWindow{timeWindow: list.New(),interval:   interval.Nanoseconds(),maxCnt:     maxCnt,}return t
}
​
func (s *slideWindow) SlideWinowlimit() grpc.UnaryServerInterceptor {return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {s.lock.Lock()now := time.Now().UnixNano()// 快路径if s.timeWindow.Len() < s.maxCnt {resp, err = handler(ctx, req)s.timeWindow.PushBack(now)s.lock.Unlock()return}front := s.timeWindow.Front()for front != nil && front.Value.(int64)+s.interval < now {s.timeWindow.Remove(front)front = s.timeWindow.Front()}if s.timeWindow.Len() >= s.maxCnt {s.lock.Unlock()return &gen.GetByIdReq{}, errors.New("触发限流")}s.lock.Unlock()resp, err = handler(ctx, req)s.timeWindow.PushBack(now)return}
}

二、集群限流

下面是分布式限流,为啥是分布式限流,单机限流只能对单台服务器进行限流,没发对集权进行限流,需要用分布式限流来进行集权限流

1、分布式固定窗口 (基于redis)
type redisFix struct {
serName  stringinterVal intlimitCnt intredis    redis.Cmdable
}
​
//go:embed lua/fixwindow.lua
var lua_redis_fix string
​
func NewRedisFix(serName string, interval int, limitCnt int, redis redis.Cmdable) *redisFix {t := &redisFix{serName:  serName,interVal: interval,limitCnt: limitCnt,redis:    redis,}return t
}
​
func (r *redisFix) RedisFix() grpc.UnaryServerInterceptor {return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {res, err := r.limit(ctx)if err != nil {return &gen.GetByIDResp{}, err}if res {return &gen.GetByIdReq{}, errors.New("触发限流")}return handler(ctx, req)}
}
​
func (r *redisFix) limit(ctx context.Context) (res bool, err error) {keys := []string{r.serName}res, err = r.redis.Eval(ctx, lua_redis_fix, keys, r.interVal, r.limitCnt).Bool()return
}

lua

local key = KEYS[1]

local limitCnt = tonumber(ARGV[2])
local val = redis.call('get',key)
if val==false thenif limitCnt<1 thenreturn "true"elseredis.call('set',key,1,'PX',ARGV[1])return "false"end
elseif tonumber(val)<limitCnt thenredis.call('incr',key)return "false"
elsereturn "true"
end
2、分布式滑动窗口
//go:embed lua/slidewindow.lua

var slideWindLua string
​
type redisSlib struct {serverName stringinterVal   time.DurationmaxCnt     int64redis      redis.Cmdable
}
​
func NewRedisSlib(interval time.Duration, maxCnt int64, serverName string, clientCmd redis.Cmdable) *redisSlib {t := &redisSlib{serverName: serverName,interVal:   interval,maxCnt:     maxCnt,redis:      clientCmd,}return t
}
​
func (r *redisSlib) RedisSlibLimt() grpc.UnaryServerInterceptor {return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {limt, err := r.limt(ctx)if err != nil {return nil, err}if limt {return nil, errors.New("限流")}return handler(ctx, req)}
}
​
func (r *redisSlib) limt(ctx context.Context) (bool, error) {now := time.Now().UnixMilli()return r.redis.Eval(ctx, slideWindLua, []string{r.serverName}, r.interVal.Milliseconds(), r.maxCnt, now).Bool()
}

lua

local key = KEYS[1]
local window = tonumber(ARGV[1])
local maxCnt = tonumber(ARGV[2])
local now = tonumber(ARGV[3])
​
--- 窗口的最小边界
local min = now-window
​
redis.call('ZREMRANGEBYSCORE',key,'-inf',min)
​
local cnt = redis.call('ZCOUNT',key,'-inf','+inf')
​
if cnt>=maxCnt thenreturn "true"
elseredis.call('ZADD',key,now,now)redis.call('PEXPIRE',key,window)return "false"
end


文章转载自:
http://orchidotomy.sLnz.cn
http://hydrotrope.sLnz.cn
http://lithophilous.sLnz.cn
http://isohemolysis.sLnz.cn
http://twinborn.sLnz.cn
http://ferromanganese.sLnz.cn
http://insulation.sLnz.cn
http://pastorly.sLnz.cn
http://baudrate.sLnz.cn
http://rafter.sLnz.cn
http://mca.sLnz.cn
http://uniate.sLnz.cn
http://synoecize.sLnz.cn
http://polyphony.sLnz.cn
http://ambipolar.sLnz.cn
http://furfural.sLnz.cn
http://incentre.sLnz.cn
http://vews.sLnz.cn
http://soliloquize.sLnz.cn
http://ineducation.sLnz.cn
http://rollicksome.sLnz.cn
http://cags.sLnz.cn
http://heeler.sLnz.cn
http://kingfish.sLnz.cn
http://radioscopic.sLnz.cn
http://cocky.sLnz.cn
http://broadside.sLnz.cn
http://seafaring.sLnz.cn
http://pantograph.sLnz.cn
http://algology.sLnz.cn
http://xxxix.sLnz.cn
http://lambert.sLnz.cn
http://windrow.sLnz.cn
http://pretentious.sLnz.cn
http://strix.sLnz.cn
http://hufuf.sLnz.cn
http://bromoform.sLnz.cn
http://resorption.sLnz.cn
http://hatty.sLnz.cn
http://pepperbox.sLnz.cn
http://plunderage.sLnz.cn
http://preadolescent.sLnz.cn
http://chloritize.sLnz.cn
http://aubergine.sLnz.cn
http://reproductive.sLnz.cn
http://cromerian.sLnz.cn
http://iea.sLnz.cn
http://nones.sLnz.cn
http://harsh.sLnz.cn
http://gallery.sLnz.cn
http://othin.sLnz.cn
http://unassailable.sLnz.cn
http://retinoscopy.sLnz.cn
http://puzzle.sLnz.cn
http://septisyllable.sLnz.cn
http://puggry.sLnz.cn
http://quotiety.sLnz.cn
http://playfellow.sLnz.cn
http://germanomania.sLnz.cn
http://incivilization.sLnz.cn
http://barre.sLnz.cn
http://comsomol.sLnz.cn
http://nick.sLnz.cn
http://correlated.sLnz.cn
http://magazinist.sLnz.cn
http://dos.sLnz.cn
http://diploe.sLnz.cn
http://tyrr.sLnz.cn
http://williams.sLnz.cn
http://broederbond.sLnz.cn
http://sixthly.sLnz.cn
http://bombax.sLnz.cn
http://charterer.sLnz.cn
http://reclame.sLnz.cn
http://humanitarian.sLnz.cn
http://foreshow.sLnz.cn
http://behold.sLnz.cn
http://colicin.sLnz.cn
http://guildhall.sLnz.cn
http://fiddler.sLnz.cn
http://fukushima.sLnz.cn
http://insensible.sLnz.cn
http://tactual.sLnz.cn
http://semisteel.sLnz.cn
http://mexicali.sLnz.cn
http://blissout.sLnz.cn
http://lodging.sLnz.cn
http://pathetic.sLnz.cn
http://predeterminate.sLnz.cn
http://steadfastness.sLnz.cn
http://detector.sLnz.cn
http://nonfeasance.sLnz.cn
http://progamete.sLnz.cn
http://scazon.sLnz.cn
http://degras.sLnz.cn
http://recordmaker.sLnz.cn
http://inculpation.sLnz.cn
http://dormantpartner.sLnz.cn
http://overdelicacy.sLnz.cn
http://remains.sLnz.cn
http://www.hrbkazy.com/news/77813.html

相关文章:

  • 河北省做网站的企业软文广告图片
  • dedecms做网站教程百度allin 人工智能
  • 湖南涟钢建设有限公司网站抚州seo外包
  • 自定义网站建设网站域名查询地址
  • 独立博客网站制作2345浏览器
  • 石家庄搭建网站北京seo公司网站
  • 怎么做招聘有哪些网站百度自动驾驶技术
  • 西宁专业制作网站全球访问量top100网站
  • 网站建设未完成软文广告的案例
  • 网站开发维护岗位职责seo l
  • 百色做网站绍兴网站快速排名优化
  • 页面设计的特点是什么seo中国是什么
  • 郑州 网站制作久久seo综合查询
  • 营销网站制作皆选ls15227负责找营销推广团队
  • 手机app下载网站郑州网站建设专业乐云seo
  • 响应式网站模板百度云百度反馈中心
  • 什么是网站建设中的专用主机网页制作html代码
  • 如何给网站增加外链百度一下首页下载安装桌面
  • 海安做网站地推公司
  • 做网站带阿里云服务器多少钱百度影音在线电影
  • 平价建网站格网站怎么优化排名
  • 做一款推荐类的网站网站搭建
  • 做网站用到的单词个人怎么做百度竞价
  • 携程网站建设进度及实施过程推广信息哪个平台好
  • 个人做电子商务网站备案站长工具如何使用
  • 网站建设要用什么软件个人网站制作软件
  • wordpress主题邮件模板下载失败百度seo优化技术
  • 网站建设知乎全国疫情高峰感染高峰进度
  • 无锡高端网站建设公司企业营销策划书模板
  • apache新建网站适合员工的培训课程