This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"image/color" | |
"log" | |
"os" | |
"strings" | |
"gioui.org/app" | |
"gioui.org/f32" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. { |
OlderNewer