Implementing BBR Congestion Control in Kratos Framework

BBR (Bottleneck Bandwidth and Round-trip time) is a congestion control algorithm originally developed by Google. In the context of rate limiting, BBR has been adapted to provide dynamic throttling by automatically adjusting request concurrency to balance system throughput and response times.

Core Principles of BBR Rate Limiting

The BBR algorithm operates on three fundamental principles:

  1. Continuous monitoring of system metrics (CPU utilization, request throughput, response latency)
  2. Dynamic calculation of system capacity based on these metrics
  3. Triggering throttling when system load approaches or exceeds capacity

Implementation in Go

Below is a analysis of BBR implementation in the Kratos framework.

Core Data Structure


type BBRController struct {
    cpuMonitor       performanceMonitor
    throughputStats  metrics.SlidingWindow
    latencyStats     metrics.SlidingWindow
    activeRequests   atomic.Int64
    samplingRate     int64
    samplingInterval time.Duration
    lastThrottleTime atomic.Value
    peakThroughput   atomic.Value
    optimalLatency   atomic.Value
    config          controllerOptions
}

Initialization


func NewBBRController(cfg ...ConfigOption) *BBRController {
    // Initialization logic here
}

Key Methods

CalculatePeakThroughput()


func (c *BBRController) CalculatePeakThroughput() int64 {
    // Implementation details
}

DetermineOptimalLatency()


func (c *BBRController) DetermineOptimalLatency() int64 {
    // Implementation details
}

ComputeMaxConcurrency()


func (c *BBRController) ComputeMaxConcurrency() int64 {
    return int64(math.Floor(
        float64(c.CalculatePeakThroughput()*
        c.DetermineOptimalLatency()*
        c.samplingRate)/1000.0) + 0.5)
}

ShouldThrottle()


func (c *BBRController) ShouldThrottle() bool {
    // Decision logic implementation
}

RequestCheckpoint()


func (c *BBRController) RequestCheckpoint() (CompletionCallback, error) {
    // Main entry point implementation
}

System Monitoring

The BBR implementation encludes a background process that samples CPU utilization every 500ms using an Exponential Moving Average (EMA) algorithm.

BBR Workflow

  1. Continuous monitoring of system metrics
  2. Request validation through RequestCheckpoint()
  3. Throttle decision via ShouldThrottle()
  4. Request processing with concurrency control
  5. Metric updates upon request completion

Advantages of BBR

  • Self-adapting to system conditions
  • Multi-dimensional protection
  • High performance under load
  • Precise request control

Implementation Considerations

  • Proper CPU threshold configuration
  • Optimal sliding window sizing
  • Continuous performance monitoring

Tags: Golang RateLimiting kratos bbr Concurrency

Posted on Tue, 25 Aug 2026 16:22:01 +0000 by vandalite