Skip to content

udhos/debounce

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

license Go Report Card Go Reference

debounce

debounce is a simple implementation of a debouncer.

The debouncer coalesces multiple function calls.

The first function call is executed immediately, and a delay window is started.

Subsequent function calls within the delay window will not reset the timer, but will update the function to be executed.

After the delay has passed, only the most recently provided function will be executed, returning the debouncer to its initial state.

The debouncer is thread-safe, and can be used in concurrent environments. But it is often useful in sequential non-concurrent code as well.

A common use case is to reduce the number of reconciliation operations in response to events. For example, in Kubernetes controllers, events may be sent in bursts, and it is often desirable to only reconcile once after many events have been received.

Synopsis

In the example below, we create a debouncer with a delay of 100 milliseconds.

We then run the function 100 times in a loop.

Since all calls happen within the delay window, the function will be executed twice:

  • The first call will execute immediately.
  • The second call will be the most recent function provided, and will be executed after the delay has passed.
package main

import (
	"fmt"
	"time"

	"github.com/udhos/debounce/debounce"
)

func main() {
	debouncer := debounce.New(100 * time.Millisecond)

	var executions int

	myFunc := func() {
		executions++
	}

	for range 100 {
		debouncer.Run(myFunc)
	}

	time.Sleep(1 * time.Second)

	fmt.Printf("executions: %d\n", executions)
}

Thus the output will be:

executions: 2

Example

See a full example in cmd/debounce-example/main.go.

About

debounce is a simple implementation of a debouncer.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors