hearHeld paces its pull at the kept-call pace and says each kind of error once
Builds: design 47 H2 §775, H-A-695 (2), H-A-733
This commit is contained in:
@@ -3,9 +3,11 @@ package process
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/busretry"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
)
|
||||
|
||||
@@ -65,3 +67,24 @@ func TestConsumerLedgerKeepsTheFactsAfterEachMessage(t *testing.T) {
|
||||
t.Errorf("after both applied: %+v, want sequence 8 and none unacknowledged", f)
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 H2 §775: the held pull waits after an error at the kept-call pace, doubling to its most, and says each kind of
|
||||
// error once until a message arrives.
|
||||
func TestHeldPullPacesAtTheKeptCallPaceAndSaysEachKindOnce(t *testing.T) {
|
||||
pace := busretry.Pace{First: time.Second, Max: 3 * time.Second}
|
||||
var f pullFaults
|
||||
a, b := errors.New("a"), errors.New("b")
|
||||
for i, row := range []struct {
|
||||
err error
|
||||
say bool
|
||||
wait time.Duration
|
||||
}{{a, true, time.Second}, {a, false, 2 * time.Second}, {b, true, 3 * time.Second}, {a, false, 3 * time.Second}} {
|
||||
if say, wait := f.next(row.err, pace); say != row.say || wait != row.wait {
|
||||
t.Errorf("error %d: said %v, waited %v; want %v, %v", i, say, wait, row.say, row.wait)
|
||||
}
|
||||
}
|
||||
f.ended()
|
||||
if say, wait := f.next(a, pace); !say || wait != time.Second {
|
||||
t.Errorf("after a message: said %v, waited %v", say, wait)
|
||||
}
|
||||
}
|
||||
|
||||
+35
-2
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/busretry"
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
)
|
||||
@@ -155,6 +156,7 @@ func hearHeld(cons jetstream.Consumer, binder string, hold *eventHold, handle fu
|
||||
pulled := &heldPull{it: it, closed: make(chan struct{})}
|
||||
go func() {
|
||||
defer func() { release(); close(pulled.closed) }()
|
||||
var faults pullFaults
|
||||
for {
|
||||
hold.waitRoom()
|
||||
m, err := it.Next()
|
||||
@@ -162,16 +164,47 @@ func hearHeld(cons jetstream.Consumer, binder string, hold *eventHold, handle fu
|
||||
if errors.Is(err, jetstream.ErrMsgIteratorClosed) {
|
||||
return
|
||||
}
|
||||
logf("hearing the bus: %v", err)
|
||||
time.Sleep(time.Second)
|
||||
say, wait := faults.next(err, busretry.KeptPace())
|
||||
if say {
|
||||
logf("hearing the bus: %v", err)
|
||||
}
|
||||
time.Sleep(wait)
|
||||
continue
|
||||
}
|
||||
faults.ended()
|
||||
handle(m)
|
||||
}
|
||||
}()
|
||||
return pulled, nil
|
||||
}
|
||||
|
||||
// pullFaults paces hearHeld's pull after an error at the kept-call pace (busretry.KeptPace: the settings
|
||||
// kept-call-pace-first and kept-call-pace-max), doubling from the first to the most, and says each kind of error once
|
||||
// until a message arrives (design 47 H2 §775, principle 11).
|
||||
type pullFaults struct {
|
||||
wait time.Duration
|
||||
said map[string]bool
|
||||
}
|
||||
|
||||
// next is the wait after err and whether err is of a kind not yet said.
|
||||
func (f *pullFaults) next(err error, pace busretry.Pace) (bool, time.Duration) {
|
||||
if f.wait <= 0 {
|
||||
f.wait = pace.First
|
||||
} else {
|
||||
f.wait = min(f.wait*2, pace.Max)
|
||||
}
|
||||
if f.said == nil {
|
||||
f.said = map[string]bool{}
|
||||
}
|
||||
kind := err.Error()
|
||||
say := !f.said[kind]
|
||||
f.said[kind] = true
|
||||
return say, f.wait
|
||||
}
|
||||
|
||||
// ended is a message pulled: the pace and what was said start again.
|
||||
func (f *pullFaults) ended() { *f = pullFaults{} }
|
||||
|
||||
// heldPull is the pull of hearHeld as a ConsumeContext.
|
||||
type heldPull struct {
|
||||
it jetstream.MessagesContext
|
||||
|
||||
Reference in New Issue
Block a user