mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-11-10 04:27:25 +01:00
Use mutex to ensure only on flush timer is running
This commit is contained in:
parent
9ccc5a6ca7
commit
04819d9db2
@ -45,6 +45,7 @@ type InfluxSink struct {
|
||||
flushTimer *time.Timer
|
||||
flushDelay time.Duration
|
||||
batchMutex sync.Mutex // Flush() runs in another goroutine, so this lock has to protect the buffer
|
||||
flushTimerMutex sync.Mutex // Ensure only one flush timer is running
|
||||
}
|
||||
|
||||
// connect connects to the InfluxDB server
|
||||
@ -103,16 +104,17 @@ func (s *InfluxSink) connect() error {
|
||||
|
||||
func (s *InfluxSink) Write(m lp.CCMetric) error {
|
||||
|
||||
if s.flushDelay != 0 && s.flushTimer == nil {
|
||||
if s.flushDelay != 0 && s.flushTimerMutex.TryLock() {
|
||||
// Run a batched flush for all metrics that arrived in the last flush delay interval
|
||||
cclog.ComponentDebug(s.name, "Starting new flush timer")
|
||||
s.flushTimer = time.AfterFunc(
|
||||
s.flushDelay,
|
||||
func() {
|
||||
err := s.Flush()
|
||||
if err != nil {
|
||||
defer s.flushTimerMutex.Unlock()
|
||||
cclog.ComponentDebug(s.name, "Starting flush in flush timer")
|
||||
if err := s.Flush(); err != nil {
|
||||
cclog.ComponentError(s.name, "Flush timer: flush failed:", err)
|
||||
}
|
||||
s.flushTimer = nil
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user