Skip to content

Commit

Permalink
Add a debouncer to limit reloads
Browse files Browse the repository at this point in the history
  • Loading branch information
safchain committed Dec 26, 2022
1 parent f2c2fb1 commit 3a4958c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ require (
github.com/sassoftware/go-rpmutils v0.2.0 // indirect
github.com/secure-systems-lab/go-securesystemslib v0.4.0
github.com/shopspring/decimal v1.2.0 // indirect
github.com/skydive-project/go-debouncer v1.0.0 // indirect
github.com/skydive-project/go-debouncer v1.0.0
github.com/smira/go-ftp-protocol v0.0.0-20140829150050-066b75c2b70d // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
Expand Down
30 changes: 25 additions & 5 deletions pkg/security/rconfig/rconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/hashicorp/go-multierror"
"github.com/skydive-project/go-debouncer"

"github.com/DataDog/datadog-agent/pkg/config/remote"
"github.com/DataDog/datadog-agent/pkg/config/remote/data"
Expand All @@ -24,7 +25,10 @@ import (
"github.com/DataDog/datadog-agent/pkg/util/log"
)

const securityAgentRCPollInterval = time.Second * 1
const (
securityAgentRCPollInterval = time.Second * 1
debounceDelay = 5 * time.Second
)

// RCPolicyProvider defines a remote config policy provider
type RCPolicyProvider struct {
Expand All @@ -34,6 +38,7 @@ type RCPolicyProvider struct {
onNewPoliciesReadyCb func()
lastDefaults map[string]state.ConfigCWSDD
lastCustoms map[string]state.ConfigCWSCustom
debouncer *debouncer.Debouncer
}

var _ rules.PolicyProvider = (*RCPolicyProvider)(nil)
Expand All @@ -45,15 +50,20 @@ func NewRCPolicyProvider(name string, agentVersion *semver.Version) (*RCPolicyPr
return nil, err
}

return &RCPolicyProvider{
r := &RCPolicyProvider{
client: c,
}, nil
}
r.debouncer = debouncer.New(debounceDelay, r.onNewPoliciesReady)

return r, nil
}

// Start starts the Remote Config policy provider and subscribes to updates
func (r *RCPolicyProvider) Start() {
log.Info("remote-config policies provider started")

r.debouncer.Start()

r.client.RegisterCWSDDUpdate(r.rcDefaultsUpdateCallback)
r.client.RegisterCWSCustomUpdate(r.rcCustomsUpdateCallback)

Expand All @@ -67,7 +77,7 @@ func (r *RCPolicyProvider) rcDefaultsUpdateCallback(configs map[string]state.Con

log.Info("new policies from remote-config policy provider")

r.onNewPoliciesReadyCb()
r.debouncer.Call()
}

func (r *RCPolicyProvider) rcCustomsUpdateCallback(configs map[string]state.ConfigCWSCustom) {
Expand All @@ -77,7 +87,7 @@ func (r *RCPolicyProvider) rcCustomsUpdateCallback(configs map[string]state.Conf

log.Info("new policies from remote-config policy provider")

r.onNewPoliciesReadyCb()
r.debouncer.Call()
}

func normalize(policy *rules.Policy) {
Expand Down Expand Up @@ -122,8 +132,18 @@ func (r *RCPolicyProvider) SetOnNewPoliciesReadyCb(cb func()) {
r.onNewPoliciesReadyCb = cb
}

func (r *RCPolicyProvider) onNewPoliciesReady() {
r.RLock()
defer r.RUnlock()

if r.onNewPoliciesReadyCb != nil {
r.onNewPoliciesReadyCb()
}
}

// Close stops the client
func (r *RCPolicyProvider) Close() error {
r.debouncer.Stop()
r.client.Close()
return nil
}

0 comments on commit 3a4958c

Please sign in to comment.