Skip to content

Instantly share code, notes, and snippets.

View chmike's full-sized avatar

Christophe Meessen chmike

View GitHub Profile
package main
import (
"image/color"
"log"
"os"
"strings"
"gioui.org/app"
"gioui.org/f32"
// NewRateLimiter returns a rate limiter function returning false
// when the event must be rejected, and true when it must be accepted.
// The rate is expressed in Hz (events/seconds). It simply ensures that
// the time interval between two accept satisfies the rate criteria. It
// is equivalent to the token bucket algorithm with a bucket size of one.
func NewRateLimiter(rate float64) func() bool {
var stamp time.Time // time stamp of last accepted event
return func() bool {
now := time.Now()
if now.Sub(stamp).Seconds() *rate < 1. {