zoukankan      html  css  js  c++  java
  • [GO]go redis实现滑动窗口限流-redis版

    上一篇是单机当前进程的滑动窗口限流 , 这一个是使用go redis list结构实现的滑动窗口限流 , 原理都一样 , 但是支持分布式

    原理可以参考上一篇介绍

    func LimitFreqs(queueName string, count uint, timeWindow int64) bool {
        currTime := time.Now().Unix()
        length := uint(ListLen(queueName))
        if length < count {
            ListPush(queueName, currTime)
            return true
        }
        //队列满了,取出最早访问的时间
        earlyTime, _ := strconv.ParseInt(ListIndex(queueName, int64(length)-1), 10, 64)
        //说明最早期的时间还在时间窗口内,还没过期,所以不允许通过
        if currTime-earlyTime <= timeWindow {
            return false
        } else {
            //说明最早期的访问应该过期了,去掉最早期的
            ListPop(queueName)
            ListPush(queueName, currTime)
        }
        return true
    }
  • 相关阅读:
    APP Https双向认证抓包
    剖析XSS
    php连接mysql
    linux去掉某一字符开头的行
    memcached+php客户端
    memcached-repcached
    memcached+memadmin
    Linux GPT分区
    Linux查看文件夹大小
    linux挂载windwos共享文件
  • 原文地址:https://www.cnblogs.com/taoshihan/p/14134840.html
Copyright © 2011-2022 走看看