Skip to content

Commit

Permalink
fix: fix the bug of debounce
Browse files Browse the repository at this point in the history
Signed-off-by: baoyinghai <[email protected]>
  • Loading branch information
baoyinghai committed Sep 1, 2023
1 parent 3c369ab commit a379d14
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 16 deletions.
18 changes: 2 additions & 16 deletions cmd/agent/app/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"time"

"github.com/bep/debounce"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
cliflag "k8s.io/component-base/cli/flag"
Expand Down Expand Up @@ -71,21 +72,6 @@ func NewAgentCommand(ctx context.Context) *cobra.Command {
return cmd
}

func Debounce(waits time.Duration) func(func()) {
var timer *time.Timer
return func(f func()) {
if timer != nil {
timer.Reset(time.Second * waits)
} else {
timer = time.NewTimer(time.Second * waits)
}
go func() {
<-timer.C
f()
}()
}
}

func run(ctx context.Context, opts *options.Options) error {
restConfig, err := clientcmd.BuildConfigFromFlags("", opts.KubeConfig)
if err != nil {
Expand Down Expand Up @@ -123,7 +109,7 @@ func run(ctx context.Context, opts *options.Options) error {
NodeName: os.Getenv(utils.EnvNodeName),
ClusterName: os.Getenv(utils.EnvClusterName),
NetworkManager: agent.NetworkManager(),
DebounceFunc: Debounce(5),
DebounceFunc: debounce.New(5 * time.Second),
}
if err = clusterNodeController.SetupWithManager(mgr); err != nil {
klog.Fatalf("Unable to create cluster node controller: %v", err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/kosmos.io/kosmos
go 1.20

require (
github.com/bep/debounce v1.2.1
github.com/coreos/go-iptables v0.6.0
github.com/gogo/protobuf v1.3.2
github.com/olekukonko/tablewriter v0.0.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY=
github.com/bep/debounce v1.2.1/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
Expand Down
27 changes: 27 additions & 0 deletions vendor/github.com/bep/debounce/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/bep/debounce/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions vendor/github.com/bep/debounce/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions vendor/github.com/bep/debounce/debounce.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ github.com/antlr/antlr4/runtime/Go/antlr
# github.com/beorn7/perks v1.0.1
## explicit; go 1.11
github.com/beorn7/perks/quantile
# github.com/bep/debounce v1.2.1
## explicit
github.com/bep/debounce
# github.com/blang/semver/v4 v4.0.0
## explicit; go 1.14
github.com/blang/semver/v4
Expand Down

0 comments on commit a379d14

Please sign in to comment.