-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from igmagollo/feat/publisher-health-check
feat: add unhealth warn to rabbitmq publisher
- Loading branch information
Showing
9 changed files
with
158 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package publisher | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
type waitGroupCount struct { | ||
sync.WaitGroup | ||
count int64 | ||
} | ||
|
||
func (wg *waitGroupCount) Add(delta int) { | ||
atomic.AddInt64(&wg.count, int64(delta)) | ||
wg.WaitGroup.Add(delta) | ||
} | ||
|
||
func (wg *waitGroupCount) Done() { | ||
atomic.AddInt64(&wg.count, -1) | ||
wg.WaitGroup.Done() | ||
} | ||
|
||
func (wg *waitGroupCount) GetCount() int64 { | ||
return atomic.LoadInt64(&wg.count) | ||
} | ||
|
||
func newWaitGroupCounter() *waitGroupCount { | ||
return &waitGroupCount{ | ||
WaitGroup: sync.WaitGroup{}, | ||
count: 0, | ||
} | ||
} | ||
|
||
const ( | ||
timeCheckTicker = 30 * time.Second | ||
timeWithoutPublishUnhealth = 30 * time.Second | ||
timePausedUnhealth = 30 * time.Second | ||
) | ||
|
||
func (r *rabbitmqPublisher) healthCheckLoop() { | ||
logger := r.logger.With().Str("component", "publisher_health_check").Logger() | ||
|
||
ticker := time.NewTicker(timeCheckTicker) | ||
defer ticker.Stop() | ||
for { | ||
<-ticker.C | ||
if r.closed.Load() { | ||
return | ||
} | ||
|
||
count := r.wg.GetCount() | ||
logger.Debug().Int64("messages_unpublished", count).Msg("checking publisher health") | ||
|
||
if r.isPaused() { | ||
logger.Debug().Time("last_paused", r.getLastPausedAt()).Msg("publisher is paused") | ||
if time.Since(r.getLastPausedAt()) > timePausedUnhealth { | ||
logger.Warn(). | ||
Int64("messages_unpublished", count). | ||
Time("last_paused", r.getLastPausedAt()). | ||
Msg("publisher unhealthy: paused for too long") | ||
} | ||
continue | ||
} | ||
|
||
if count == 0 { | ||
logger.Debug().Msg("no messages pending, skipping") | ||
continue | ||
} | ||
|
||
lastPublishedAt := r.getLastPublishedAt() | ||
if time.Since(lastPublishedAt) > timeWithoutPublishUnhealth { | ||
logger.Warn(). | ||
Int64("messages_unpublished", count). | ||
Time("last_published", lastPublishedAt). | ||
Msg("publisher unhealthy: no publishing for too long") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package publisher | ||
|
||
import "time" | ||
|
||
func (r *rabbitmqPublisher) pause() { | ||
r.pausePublishMux.Lock() | ||
r.pausePublish = true | ||
r.pausePublishMux.Unlock() | ||
|
||
r.lastPausedAtMux.Lock() | ||
r.lastPausedAt = time.Now() | ||
r.lastPausedAtMux.Unlock() | ||
|
||
if len(r.pauseSignalChan) == 0 { | ||
r.pauseSignalChan <- true | ||
} | ||
} | ||
|
||
func (r *rabbitmqPublisher) resume() { | ||
r.pausePublishMux.Lock() | ||
r.pausePublish = false | ||
r.pausePublishMux.Unlock() | ||
} | ||
|
||
func (r *rabbitmqPublisher) isPaused() bool { | ||
r.pausePublishMux.RLock() | ||
defer r.pausePublishMux.RUnlock() | ||
return r.pausePublish | ||
} | ||
|
||
func (r *rabbitmqPublisher) getLastPausedAt() time.Time { | ||
r.lastPausedAtMux.RLock() | ||
defer r.lastPausedAtMux.RUnlock() | ||
return r.lastPausedAt | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters