grate

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2025 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

grate 包

简介

grate 包提供了限流器实现,用于控制请求速率。

功能特性

  • 滑动窗口限流:基于滑动窗口算法
  • 并发安全:线程安全的限流器
  • 灵活配置:可配置速率和窗口大小

安装

go get github.com/snail007/gmc/util/rate

快速开始

package main

import (
    "fmt"
    "time"
    "github.com/snail007/gmc/util/rate"
)

func main() {
    // 创建限流器:每秒最多 10 个请求
    limiter := grate.NewSlidingWindowLimiter(10, time.Second)
    
    for i := 0; i < 20; i++ {
        if limiter.Allow() {
            fmt.Println("Request", i, "allowed")
        } else {
            fmt.Println("Request", i, "rejected")
        }
        time.Sleep(50 * time.Millisecond)
    }
}

相关链接

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SlidingWindowLimiter

type SlidingWindowLimiter struct {
	// contains filtered or unexported fields
}

func NewSlidingWindowLimiter

func NewSlidingWindowLimiter(capacity int, duration time.Duration) *SlidingWindowLimiter

NewSlidingWindowLimiter 创建一个新的滑动窗口限流器

func (*SlidingWindowLimiter) Allow

func (l *SlidingWindowLimiter) Allow() bool

Allow 允许 1 个请求

func (*SlidingWindowLimiter) AllowN

func (l *SlidingWindowLimiter) AllowN(n int32) bool

AllowN 允许 n 个请求

func (*SlidingWindowLimiter) Capacity

func (l *SlidingWindowLimiter) Capacity() int

func (*SlidingWindowLimiter) Duration

func (l *SlidingWindowLimiter) Duration() time.Duration

type TokenBucketLimiter

type TokenBucketLimiter struct {
	*rate.Limiter
	// contains filtered or unexported fields
}

func NewTokenBucketBurstLimiter

func NewTokenBucketBurstLimiter(count int, duration time.Duration, burst int) *TokenBucketLimiter

func NewTokenBucketLimiter

func NewTokenBucketLimiter(count int, duration time.Duration) *TokenBucketLimiter

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL