Reuse flush timer

This commit is contained in:
Holger Obermaier 2023-09-26 15:04:39 +02:00
parent 19ec6d06db
commit 1e606a1aa1

View File

@ -105,7 +105,17 @@ func (s *InfluxSink) connect() error {
func (s *InfluxSink) Write(m lp.CCMetric) error { func (s *InfluxSink) Write(m lp.CCMetric) error {
if s.flushDelay != 0 && s.flushTimerMutex.TryLock() { if s.flushDelay != 0 && s.flushTimerMutex.TryLock() {
// Run a batched flush for all metrics that arrived in the last flush delay interval
// Setup flush timer when flush delay is configured
// and no other timer is already running
if s.flushTimer != nil {
// Restarting existing flush timer
cclog.ComponentDebug(s.name, "Restarting flush timer")
s.flushTimer.Reset(s.flushDelay)
} else {
// Creating and starting flush timer
cclog.ComponentDebug(s.name, "Starting new flush timer") cclog.ComponentDebug(s.name, "Starting new flush timer")
s.flushTimer = time.AfterFunc( s.flushTimer = time.AfterFunc(
s.flushDelay, s.flushDelay,
@ -117,6 +127,7 @@ func (s *InfluxSink) Write(m lp.CCMetric) error {
} }
}) })
} }
}
// Lock access to batch slice // Lock access to batch slice
s.batchMutex.Lock() s.batchMutex.Lock()