GoWindGoWind
Home
Framework
GoWind Toolkit
  • Open Source

    • GoWind Admin (Admin Platform)
    • GoWind CMS (Content Management)
    • GoWind UBA (Behavior Analytics)
    • GoWind Shop (E-commerce)
    • GoWind Kanban (Kanban Boards)
    • GoWind Ledger (Personal Finance)
    • GoWind ShortPlay (Short Drama)
    • GoWind Ops (Operations Platform)
    • RushWind (Rust Ecosystem)
  • Open Core (Community + Enterprise)

    • GoWind OA (Office Collaboration)
    • GoWind ERP (Procure-to-Pay)
    • GoWind WMS (Warehousing)
  • Commercial

    • GoWind IM (Instant Messaging)
    • GoWind IoT (IoT Platform)
    • GoWind Edu (AI Education)
    • GoWind MES (Manufacturing Execution)
    • GoWind CDC (Data Pipeline)
    • GoWind SDP (Zero-Trust Gateway)
    • GoWind Quant (Quant Research)
    • GoWind Trade Radar (Export Sourcing)
    • GoWind Game Radar (Game Greenlight)
  • 简体中文
  • English
GitHub
Home
Framework
GoWind Toolkit
  • Open Source

    • GoWind Admin (Admin Platform)
    • GoWind CMS (Content Management)
    • GoWind UBA (Behavior Analytics)
    • GoWind Shop (E-commerce)
    • GoWind Kanban (Kanban Boards)
    • GoWind Ledger (Personal Finance)
    • GoWind ShortPlay (Short Drama)
    • GoWind Ops (Operations Platform)
    • RushWind (Rust Ecosystem)
  • Open Core (Community + Enterprise)

    • GoWind OA (Office Collaboration)
    • GoWind ERP (Procure-to-Pay)
    • GoWind WMS (Warehousing)
  • Commercial

    • GoWind IM (Instant Messaging)
    • GoWind IoT (IoT Platform)
    • GoWind Edu (AI Education)
    • GoWind MES (Manufacturing Execution)
    • GoWind CDC (Data Pipeline)
    • GoWind SDP (Zero-Trust Gateway)
    • GoWind Quant (Quant Research)
    • GoWind Trade Radar (Export Sourcing)
    • GoWind Game Radar (Game Greenlight)
  • 简体中文
  • English
GitHub
  • Introduction

    • GoWind Framework
    • Framework Architecture
  • go-wind Core

    • go-wind Core Framework
    • App Lifecycle Management
    • Context Propagation
    • Transport Abstraction
    • Log Facade Interface
  • go-wind-plugins

    • go-wind-plugins Plugin Overview
    • Plugin Configuration System
    • Plugin Registration Mechanism (SPI)
    • Logging Adapter Plugins
    • Transport Protocol Plugins
    • Message Middleware Plugins
    • Encoding Plugins
    • Security and Authentication Plugins
    • Distributed Tracing Plugins
    • Cache Plugins
    • Object Storage Plugins (OSS)
    • Rate Limiting Plugins
    • Metrics Plugins
    • AI Plugins
    • Workflow Plugins
    • Database and Cache Plugins
    • Bootstrap Integration and Gateway Plugins
    • Compression and Serialization Plugins
    • Template Rendering and Validation Plugins
  • go-wind-bootstrap

    • go-wind-bootstrap: The Declarative Bootstrap
    • The Bootstrap Configuration System
    • The Bootstrap SPI Mechanism
    • Declarative Middleware Orchestration
    • Bootstrap Command-Line Integration
    • Bootstrap Practical Examples
  • go-wind-flow Workflow Engine

    • GoWind Flow Process Engine
  • Tutorials

    • Quick Start Tutorial
    • Custom Plugin Development Tutorial
    • Multi-Protocol Listening Tutorial
    • Framework Migration Guide

Rate Limiting Plugins

go-wind-plugins provides the unified rate limiting interface ratelimit.Limiter, with three built-in algorithms: token bucket, BBR, and Sentinel.

1. The Limiter Interface

type Limiter interface {
    // Allow returns immediately. ok is false (with ErrLimited) if the
    // rate limit has been exceeded.
    Allow() (ok bool, err error)

    // Wait blocks until a request is permitted or ctx is cancelled.
    Wait(ctx context.Context) error

    Close() error
}

2. Adapter List

AdapterImport PathAlgorithm
Token Bucketplugins/ratelimit/tokenbucketToken bucket algorithm
BBRplugins/ratelimit/bbrBBR adaptive rate limiting
Sentinelplugins/ratelimit/sentinelalibaba/sentinel-golang

3. Token Bucket

import tbPlugin "github.com/tx7do/go-wind-plugins/ratelimit/tokenbucket"

// Token refill rate and burst capacity
limiter, _ := tbPlugin.New(100, 50)
defer limiter.Close()

Checking the Rate Limit

allowed, err := limiter.Allow()
if !allowed {
    // return 429 Too Many Requests
}

Waiting for a Permit

// Blocks until a token is acquired or ctx is cancelled
err := limiter.Wait(ctx)
if err != nil {
    // timed out
}

YAML Configuration

ratelimit:
  tokenbucket:
    rate: 100
    burst: 50

4. BBR (Adaptive Rate Limiting)

Adjusts automatically based on CPU load and sliding-window QPS, with no preset threshold required:

import bbrPlugin "github.com/tx7do/go-wind-plugins/ratelimit/bbr"

limiter := bbrPlugin.New(
    bbrPlugin.WithCPUThreshold(80),          // CPU threshold (%)
    bbrPlugin.WithWindow(10*time.Second),    // statistics window
    bbrPlugin.WithBucketCount(100),          // number of window buckets
    bbrPlugin.WithMinQPS(800),               // minimum QPS to trigger degradation
)
defer limiter.Close()

if ok, _ := limiter.Allow(); !ok {
    // system overloaded, reject the request
}

// Report the elapsed time after the request finishes, for BBR statistics
limiter.Done(rtt)

5. Sentinel

import sentinelPlugin "github.com/tx7do/go-wind-plugins/ratelimit/sentinel"

// Create a limiter per resource (backed by alibaba/sentinel-golang)
limiter := sentinelPlugin.New("api.users",
    sentinelPlugin.WithTrafficType(base.Inbound),   // traffic type
    sentinelPlugin.WithWaitInterval(100*time.Millisecond),
)
defer limiter.Close()

allowed, err := limiter.Allow()

// When you need to manage the Entry manually (e.g. to inspect BlockError details):
entry, blockErr := limiter.AllowEntry()
if entry != nil {
    defer limiter.ReleaseEntry(entry)
}

Flow-control and circuit breaking rules are loaded through the native Sentinel API (sentinel.LoadRules); the plugin focuses on the resource-level Limiter wrapper.

YAML Configuration

ratelimit:
  sentinel:
    flow_rules:
      - resource: "api.users"
        threshold: 100
        stat_interval_ms: 1000
        control_behavior: 0
      - resource: "api.orders"
        threshold: 50
        stat_interval_ms: 1000
    circuit_rules:
      - resource: "service.payment"
        strategy: 2              # error ratio
        threshold: 0.5
        min_request_amount: 5
        time_window: 10

6. HTTP Middleware

The HTTP/gRPC transport layers ship built-in rate limiting middleware (transport/http/middleware/ratelimit, transport/grpc/middleware/ratelimit):

import ratelimitMW "github.com/tx7do/go-wind-plugins/transport/http/middleware/ratelimit"

// Global rate limiting
router.Use(ratelimitMW.Middleware(limiter))

// Keyed rate limiting: KeyFunc extracts the dimension, factory creates/reuses a Limiter per key
router.Use(ratelimitMW.MiddlewareKeyed(
    func(r *http.Request) string { return r.RemoteAddr },  // per IP
    func(key string) ratelimit.Limiter { return newLimiterForKey(key) },
))

// Optional behaviors
ratelimitMW.WithWait()                                    // queue instead of rejecting immediately
ratelimitMW.WithSkipFunc(func(r *http.Request) bool {     // skip health checks
    return r.URL.Path == "/health"
})
ratelimitMW.WithErrorHandler(func(w http.ResponseWriter, r *http.Request, err error) {
    w.WriteHeader(http.StatusTooManyRequests)
})

Related Documents

  • Plugin Configuration System
  • Security and Authentication Plugins
  • Declarative Middleware Orchestration
Edit this page
Last Updated:: 11/3/26, 2:14 PM
Contributors: Bobo
Prev
Object Storage Plugins (OSS)
Next
Metrics Plugins