Skip to content

Instantly share code, notes, and snippets.

@prashantv
Last active March 30, 2025 12:39
Show Gist options
  • Save prashantv/26016a7dbc6fc1ec52d8c2b6591f3582 to your computer and use it in GitHub Desktop.
Save prashantv/26016a7dbc6fc1ec52d8c2b6591f3582 to your computer and use it in GitHub Desktop.

golang.org rate limiter:

	rl := rate.NewLimiter(1000000, 1)
 
	last := time.Now()
	for i := 0; i < 10; i++ {
		rl.Wait(context.Background())
		cur := time.Now()
		fmt.Println("last", cur.Sub(last))
		last = cur
	}

Output:

last 16.038µs
last 11.367µs
last 4.051µs
last 4.582µs
last 2.146µs
last 3.085µs
last 2.173µs
last 3.121µs
last 1.984µs
last 3.09µs

uber-go rate limiter:

	rl := ratelimit.New(1000000, ratelimit.WithoutSlack)

	last := time.Now()
	for i := 0; i < 10; i++ {
		rl.Take()
		cur := time.Now()
		fmt.Println("last", cur.Sub(last))
		last = cur
	}

Output:

last 315ns
last 1.846µs
last 1.3µs
last 1.218µs
last 1.191µs
last 1.202µs
last 1.206µs
last 1.216µs
last 1.166µs
last 1.18µs
@howard1209a
Copy link

good test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment