fix double push of metrics by properly handling tickers (#2374)
This commit is contained in:
parent
7106d396dc
commit
395cace69f
2 changed files with 11 additions and 8 deletions
|
@ -81,7 +81,7 @@ func (a *apic) SendMetrics(stop chan (bool)) {
|
|||
const checkInt = 20 * time.Second
|
||||
|
||||
// intervals must always be > 0
|
||||
metInts := []time.Duration{1, a.metricsIntervalFirst, a.metricsInterval}
|
||||
metInts := []time.Duration{1*time.Millisecond, a.metricsIntervalFirst, a.metricsInterval}
|
||||
|
||||
log.Infof("Start send metrics to CrowdSec Central API (interval: %s once, then %s)",
|
||||
metInts[1].Round(time.Second), metInts[2])
|
||||
|
@ -94,8 +94,6 @@ func (a *apic) SendMetrics(stop chan (bool)) {
|
|||
return metInts[count]
|
||||
}
|
||||
|
||||
// store the list of machine IDs to compare
|
||||
// with the next list
|
||||
machineIDs := []string{}
|
||||
|
||||
reloadMachineIDs := func() {
|
||||
|
@ -107,6 +105,10 @@ func (a *apic) SendMetrics(stop chan (bool)) {
|
|||
machineIDs = ids
|
||||
}
|
||||
|
||||
// store the list of machine IDs to compare
|
||||
// with the next list
|
||||
reloadMachineIDs()
|
||||
|
||||
checkTicker := time.NewTicker(checkInt)
|
||||
metTicker := time.NewTicker(nextMetInt())
|
||||
|
||||
|
@ -121,9 +123,10 @@ func (a *apic) SendMetrics(stop chan (bool)) {
|
|||
reloadMachineIDs()
|
||||
if !slices.Equal(oldIDs, machineIDs) {
|
||||
log.Infof("capi metrics: machines changed, immediate send")
|
||||
metTicker.Reset(1)
|
||||
metTicker.Reset(1*time.Millisecond)
|
||||
}
|
||||
case <-metTicker.C:
|
||||
metTicker.Stop()
|
||||
metrics, err := a.GetMetrics()
|
||||
if err != nil {
|
||||
log.Errorf("unable to get metrics (%s), will retry", err)
|
||||
|
|
|
@ -26,15 +26,15 @@ func TestAPICSendMetrics(t *testing.T) {
|
|||
}{
|
||||
{
|
||||
name: "basic",
|
||||
duration: time.Millisecond * 30,
|
||||
metricsInterval: time.Millisecond * 5,
|
||||
duration: time.Millisecond * 60,
|
||||
metricsInterval: time.Millisecond * 10,
|
||||
expectedCalls: 5,
|
||||
setUp: func(api *apic) {},
|
||||
},
|
||||
{
|
||||
name: "with some metrics",
|
||||
duration: time.Millisecond * 30,
|
||||
metricsInterval: time.Millisecond * 5,
|
||||
duration: time.Millisecond * 60,
|
||||
metricsInterval: time.Millisecond * 10,
|
||||
expectedCalls: 5,
|
||||
setUp: func(api *apic) {
|
||||
api.dbClient.Ent.Machine.Delete().ExecX(context.Background())
|
||||
|
|
Loading…
Reference in a new issue