Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
torywheelwright committed Apr 3, 2024
1 parent f412bbb commit 3e11b91
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/redispub/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func PublishStream(clients []redis.UniversalClient, in <-chan *Publication, opts
var outChans []chan error
var sendFailedMetrics []prometheus.Counter
var sendSucceededMetrics []prometheus.Counter
var temporaryFailuresMetrics []prometheus.Counter

defer func () {
for _, c := range(inChans) {
Expand All @@ -90,6 +91,7 @@ func PublishStream(clients []redis.UniversalClient, in <-chan *Publication, opts
outChans = append(outChans, outChan)
sendSucceededMetrics = append(sendSucceededMetrics, metricSentMessages.WithLabelValues("sent", clientIdxStr))
sendFailedMetrics = append(sendFailedMetrics, metricSentMessages.WithLabelValues("failed", clientIdxStr))
temporaryFailuresMetrics = append(temporaryFailuresMetrics, metricTemporaryFailures.WithLabelValues(clientIdxStr))

go func() {
defer close(outChan)
Expand All @@ -100,7 +102,7 @@ func PublishStream(clients []redis.UniversalClient, in <-chan *Publication, opts

for p := range inChan {
log.Log.Debugw("Attempting to publish to", "clientIdx", clientIdx)
outChan <- publishSingleMessageWithRetries(p, 30, clientIdx, time.Second, publishFn)
outChan <- publishSingleMessageWithRetries(p, 30, clientIdx, time.Second, temporaryFailuresMetrics[clientIdx], publishFn)
}
}()
}
Expand Down Expand Up @@ -140,7 +142,7 @@ func PublishStream(clients []redis.UniversalClient, in <-chan *Publication, opts
}
}

func publishSingleMessageWithRetries(p *Publication, maxRetries int, clientIdx int, sleepTime time.Duration, publishFn func(p *Publication) error) error {
func publishSingleMessageWithRetries(p *Publication, maxRetries int, clientIdx int, sleepTime time.Duration, temporaryFailuresMetric prometheus.Counter, publishFn func(p *Publication) error) error {
if p == nil {
return errors.New("Nil Redis publication")
}
Expand All @@ -155,7 +157,7 @@ func publishSingleMessageWithRetries(p *Publication, maxRetries int, clientIdx i
"retryNumber", retries)

// failure, retry
metricTemporaryFailures.WithLabelValues(strconv.FormatInt(int64(clientIdx), 10)).Inc()
temporaryFailuresMetric.Inc()
retries++
time.Sleep(sleepTime)
} else {
Expand Down
10 changes: 6 additions & 4 deletions lib/redispub/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

var temporaryFailuresMetric = metricTemporaryFailures.WithLabelValues("0")

// We don't test PublishStream here -- it requires a real Redis server because
// miniredis doesn't support PUBLISH and its lua support is spotty. It gets
// tested in integration tests.
Expand All @@ -33,7 +35,7 @@ func TestPublishSingleMessageWithRetriesImmediateSuccess(t *testing.T) {
return nil
}

err := publishSingleMessageWithRetries(publication, 30, 0, time.Second, publishFn)
err := publishSingleMessageWithRetries(publication, 30, 0, time.Second, temporaryFailuresMetric, publishFn)

if err != nil {
t.Errorf("Got unexpected error: %s", err)
Expand Down Expand Up @@ -67,7 +69,7 @@ func TestPublishSingleMessageWithRetriesTransientFailure(t *testing.T) {
return nil
}

err := publishSingleMessageWithRetries(publication, 30, 0, 0, publishFn)
err := publishSingleMessageWithRetries(publication, 30, 0, 0, temporaryFailuresMetric, publishFn)

if err != nil {
t.Errorf("Got unexpected error: %s", err)
Expand All @@ -85,7 +87,7 @@ func TestPublishSingleMessageWithRetriesPermanentFailure(t *testing.T) {
return errors.New("Some error")
}

err := publishSingleMessageWithRetries(publication, 30, 0, 0, publishFn)
err := publishSingleMessageWithRetries(publication, 30, 0, 0, temporaryFailuresMetric, publishFn)

if err == nil {
t.Errorf("Expected an error, but didn't get one")
Expand Down Expand Up @@ -173,7 +175,7 @@ func TestPeriodicallyUpdateTimestamp(t *testing.T) {
}

func TestNilPublicationMessage(t *testing.T) {
err := publishSingleMessageWithRetries(nil, 5, 0, 1*time.Second, func(p *Publication) error {
err := publishSingleMessageWithRetries(nil, 5, 0, 1*time.Second, temporaryFailuresMetric, func(p *Publication) error {
t.Error("Should not have been called")
return nil
})
Expand Down

0 comments on commit 3e11b91

Please sign in to comment.