Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

cocotyty/throttle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Throttle - Precise Request Rate Controller for Go

Go Reference
License: MIT

A lightweight, zero-dependency Go library designed for precise request-per-second (RPS) control. Perfect for load testing, API clients, and rate-limited workloads.

Features

  • 🚀 Microsecond Precision: Leverages monotonic clock for accurate scheduling
  • Dynamic Rate Recovery: Auto-switches to accelerated mode when falling behind
  • 🧩 Minimalist API: Single struct with one core method
  • 🛡️ Concurrency-Ready: Lock-free design for goroutine-local usage
  • 📦 Zero Dependencies: Pure Go standard library implementation

Installation

go get github.com/cocotyty/throttle

Quick Start

package main

import (
	"time"
	"github.com/cocotyty/throttle"
)

func main() {
	controller := throttle.New(1000) // Target: 1000 requests/sec
	
	for {
		wait := controller.NextWait()
		time.Sleep(wait)
		
		// Execute your rate-limited operation
		performTask()
	}
}

Core API

throttle.New(targetRPS float64) *Throttler

Creates a rate controller instance:
targetRPS: Desired requests per second

NextWait() time.Duration

Returns the duration to wait before next request. Updates internal state atomically.

Design Principles

Base Timeline:
  Expected(n) = StartTime + n*(1s/targetRPS)

Recovery Mode (when behind schedule):
  Expected(m) = RecoveryStart + m*(1s/catchupRPS)

Contributing

Contributions are welcome! Please follow standard workflow:

  1. Fork the repository
  2. Create a feature branch
  3. Commit changes
  4. Push to the branch
  5. Open a Pull Request

License

Distributed under MIT License. See LICENSE for full text.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages