Skip to content

Instantly share code, notes, and snippets.

View chmike's full-sized avatar

Christophe Meessen chmike

View GitHub Profile
// 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. {
@chmike
chmike / sliceDump.go
Last active April 13, 2024 19:19
Go language function to dump a byte slice in hexadecimal and ASCII
func dumpByteSlice(b []byte) {
var a [16]byte
n := (len(b) + 15) &^ 15
for i := 0; i < n; i++ {
if i%16 == 0 {
fmt.Printf("%4d", i)
}
if i%8 == 0 {
fmt.Print(" ")
}