search
HomeBackend DevelopmentGolangHow to implement user daily limit in Go

How to implement user daily limit in Go

Jan 10, 2022 pm 03:55 PM
gogo-zeromicroservices

This article is written by the golang tutorial column to introduce how to implement the user's daily limit in Go. I hope it will be helpful to friends in need!

Implement the user’s daily limit in Go (for example, you can only receive benefits three times a day)

If you write a bug management system and use this PeriodLimit, you can limit each tester to only submit one bug to you per day. Is work much easier? :P

The essential reason why microservice architecture is so popular nowadays is to reduce the overall complexity of the system, evenly distribute system risks to subsystems to maximize the stability of the system, and split it into different systems through domain division. After the subsystems are installed, each subsystem can be independently developed, tested, and released, and the R&D rhythm and efficiency can be significantly improved.

But it also brings problems, such as: the calling link is too long, the complexity of the deployment architecture increases, and various middleware needs to support distributed scenarios. In order to ensure the normal operation of microservices, service governance is indispensable, which usually includes: current limiting, downgrading, and circuit breaker.

Current limiting refers to limiting the frequency of interface calls to avoid exceeding the load limit and bringing down the system. For example:

  • E-commerce Flash Sale Scenario

  • API current limit for different merchants

Commonly used The current limiting algorithms are:

  • Fixed time window current limiting
  • Sliding time window current limiting
  • Leaky bucket current limiting
  • Token bucket limited Flow

This article mainly explains the fixed time window current limiting algorithm.

Working Principle

Starting from a certain point in time, each request comes with a request count of 1. At the same time, it is judged whether the number of requests in the current time window exceeds the limit. If it exceeds the limit, it will be rejected. The request is then cleared when the next time window begins waiting for the request.

How to implement user daily limit in Go

Advantages and Disadvantages

Advantages

Easy to implement It is efficient and is especially suitable for limiting scenarios such as a user can only post 10 articles a day, can only send SMS verification codes 5 times, and can only try to log in 5 times. Such scenarios are very common in actual business.

Disadvantages

The disadvantage of fixed time window current limiting is that it cannot handle critical section request burst scenarios.

Assume that the current limit is 100 requests every 1 second, and the user initiates 200 requests within 1 second starting from the middle 500ms. At this time, all 200 requests can be passed. This is inconsistent with our expectation of limiting the current to 100 times per second. The root cause is that the fine-grainedness of the current limit is too coarse.

How to implement user daily limit in Go

go-zero code implementation

##core/limit/periodlimit.go

Go-zero uses redis expiration time to simulate a fixed time window.

redis lua script:

-- KYES[1]:限流器key-- ARGV[1]:qos,单位时间内最多请求次数-- ARGV[2]:单位限流窗口时间-- 请求最大次数,等于p.quotalocal limit = tonumber(ARGV[1])-- 窗口即一个单位限流周期,这里用过期模拟窗口效果,等于p.permitlocal window = tonumber(ARGV[2])-- 请求次数+1,获取请求总数local current = redis.call("INCRBY",KYES[1],1)-- 如果是第一次请求,则设置过期时间并返回 成功if current == 1 then
  redis.call("expire",KYES[1],window)
  return 1-- 如果当前请求数量小于limit则返回 成功elseif current limit则返回 失败else
  return 0end
Fixed time window current limiter definition

type (
  // PeriodOption defines the method to customize a PeriodLimit.
  // go中常见的option参数模式
  // 如果参数非常多,推荐使用此模式来设置参数
  PeriodOption func(l *PeriodLimit)

  // A PeriodLimit is used to limit requests during a period of time.
  // 固定时间窗口限流器
  PeriodLimit struct {
    // 窗口大小,单位s
    period     int
    // 请求上限
    quota      int
    // 存储
    limitStore *redis.Redis
    // key前缀
    keyPrefix  string
    // 线性限流,开启此选项后可以实现周期性的限流
    // 比如quota=5时,quota实际值可能会是5.4.3.2.1呈现出周期性变化
    align      bool
  }
)
Pay attention to the align parameter, align= When true, the request upper limit will change periodically.

For example, when quota=5, the actual quota may be 5.4.3.2.1, showing periodic changes

Current limiting logic

In fact, the current limiting logic is above The lua script is implemented. It should be noted that the return value

    0: indicates an error, such as redis failure or overload
  • 1: allowed
  • 2: allowed However, the upper limit has been reached in the current window. If you are running a batch business, you can sleep and wait for the next window (the author has considered it very carefully)
  • 3: Rejection
  • // Take requests a permit, it returns the permit state.
    // 执行限流
    // 注意一下返回值:
    // 0:表示错误,比如可能是redis故障、过载
    // 1:允许
    // 2:允许但是当前窗口内已到达上限
    // 3:拒绝
    func (h *PeriodLimit) Take(key string) (int, error) {
      // 执行lua脚本
      resp, err := h.limitStore.Eval(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, nil
      case internalAllowed:
        return Allowed, nil
      case internalHitQuota:
        return HitQuota, nil
      default:
        return Unknown, ErrUnknownCode
      }
    }
This fixed window current limit may be used to limit, for example, a user can only send verification code text messages 5 times a day. At this time, we need to correspond to the Chinese time zone (GMT 8), and in fact, the current limit time should start from zero o'clock. At this time, we need Additional alignment (set align to true).

// 计算过期时间也就是窗口时间大小
// 如果align==true
// 线性限流,开启此选项后可以实现周期性的限流
// 比如quota=5时,quota实际值可能会是5.4.3.2.1呈现出周期性变化
func (h *PeriodLimit) calcExpireSeconds() int {
  if h.align {
    now := time.Now()
    _, offset := now.Zone()
    unix := now.Unix() + int64(offset)
    return h.period - int(unix%int64(h.period))
  }

  return h.period
}

Project address

github.com/zeromicro/go-zero

Welcome to use

go-zero and star support us!

The above is the detailed content of How to implement user daily limit in Go. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:learnku. If there is any infringement, please contact admin@php.cn delete
Learn Go Binary Encoding/Decoding: Working with the 'encoding/binary' PackageLearn Go Binary Encoding/Decoding: Working with the 'encoding/binary' PackageMay 08, 2025 am 12:13 AM

Go uses the "encoding/binary" package for binary encoding and decoding. 1) This package provides binary.Write and binary.Read functions for writing and reading data. 2) Pay attention to choosing the correct endian (such as BigEndian or LittleEndian). 3) Data alignment and error handling are also key to ensure the correctness and performance of the data.

Go: Byte Slice Manipulation with the Standard 'bytes' PackageGo: Byte Slice Manipulation with the Standard 'bytes' PackageMay 08, 2025 am 12:09 AM

The"bytes"packageinGooffersefficientfunctionsformanipulatingbyteslices.1)Usebytes.Joinforconcatenatingslices,2)bytes.Bufferforincrementalwriting,3)bytes.Indexorbytes.IndexByteforsearching,4)bytes.Readerforreadinginchunks,and5)bytes.SplitNor

Go encoding/binary package: Optimizing performance for binary operationsGo encoding/binary package: Optimizing performance for binary operationsMay 08, 2025 am 12:06 AM

Theencoding/binarypackageinGoiseffectiveforoptimizingbinaryoperationsduetoitssupportforendiannessandefficientdatahandling.Toenhanceperformance:1)Usebinary.NativeEndianfornativeendiannesstoavoidbyteswapping.2)BatchReadandWriteoperationstoreduceI/Oover

Go bytes package: short reference and tipsGo bytes package: short reference and tipsMay 08, 2025 am 12:05 AM

Go's bytes package is mainly used to efficiently process byte slices. 1) Using bytes.Buffer can efficiently perform string splicing to avoid unnecessary memory allocation. 2) The bytes.Equal function is used to quickly compare byte slices. 3) The bytes.Index, bytes.Split and bytes.ReplaceAll functions can be used to search and manipulate byte slices, but performance issues need to be paid attention to.

Go bytes package: practical examples for byte slice manipulationGo bytes package: practical examples for byte slice manipulationMay 08, 2025 am 12:01 AM

The byte package provides a variety of functions to efficiently process byte slices. 1) Use bytes.Contains to check the byte sequence. 2) Use bytes.Split to split byte slices. 3) Replace the byte sequence bytes.Replace. 4) Use bytes.Join to connect multiple byte slices. 5) Use bytes.Buffer to build data. 6) Combined bytes.Map for error processing and data verification.

Go Binary Encoding/Decoding: A Practical Guide with ExamplesGo Binary Encoding/Decoding: A Practical Guide with ExamplesMay 07, 2025 pm 05:37 PM

Go's encoding/binary package is a tool for processing binary data. 1) It supports small-endian and large-endian endian byte order and can be used in network protocols and file formats. 2) The encoding and decoding of complex structures can be handled through Read and Write functions. 3) Pay attention to the consistency of byte order and data type when using it, especially when data is transmitted between different systems. This package is suitable for efficient processing of binary data, but requires careful management of byte slices and lengths.

Go 'bytes' Package: Compare, Join, Split & MoreGo 'bytes' Package: Compare, Join, Split & MoreMay 07, 2025 pm 05:29 PM

The"bytes"packageinGoisessentialbecauseitoffersefficientoperationsonbyteslices,crucialforbinarydatahandling,textprocessing,andnetworkcommunications.Byteslicesaremutable,allowingforperformance-enhancingin-placemodifications,makingthispackage

Go Strings Package: Essential Functions You Need to KnowGo Strings Package: Essential Functions You Need to KnowMay 07, 2025 pm 04:57 PM

Go'sstringspackageincludesessentialfunctionslikeContains,TrimSpace,Split,andReplaceAll.1)Containsefficientlychecksforsubstrings.2)TrimSpaceremoveswhitespacetoensuredataintegrity.3)SplitparsesstructuredtextlikeCSV.4)ReplaceAlltransformstextaccordingto

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)