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:
+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