A second pull loop on a durable the same process already pulls takes every other event from the first, and nothing said so: the tool runner's two loops on the indexer bundle's durable left the catalog seat's revision verb stale twice today. The SDK now refuses it at bind time, naming the durable and both binders: PullOnce claims a durable (by stream, name and the time the bus made it), and HearEvents and Conn.Consume claim through it, releasing on stop, drain or the bus ending the loop. Builds: design 47 H H-A-196
84 lines
2.8 KiB
Go
84 lines
2.8 KiB
Go
package process
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
)
|
|
|
|
// A process pulls a durable consumer once (design 47 H, H-A-196): a second pull loop on a durable the same process
|
|
// already pulls takes every other event from the first, so each event reaches one loop only and nothing says so. The SDK
|
|
// refuses it, loud at bind time, the error naming the durable and both binders. A durable is told by its stream, its name
|
|
// and the time the bus made it, so the same names on two buses (two embedded buses in one test process) are two durables.
|
|
|
|
var pulled = struct {
|
|
sync.Mutex
|
|
by map[string]string
|
|
}{by: map[string]string{}}
|
|
|
|
// PullOnce claims the pull of cons, a durable consumer, for binder (a short label; empty is the calling function's
|
|
// name): it answers the release, called once the pull loop ends, or an error when this process already pulls that
|
|
// durable, naming it and both binders. A consumer that is not durable is no claim, and its release does nothing.
|
|
func PullOnce(cons jetstream.Consumer, binder string) (release func(), err error) {
|
|
if binder == "" {
|
|
binder = callerOutside()
|
|
}
|
|
info := cons.CachedInfo()
|
|
if info == nil || info.Config.Durable == "" {
|
|
return func() {}, nil
|
|
}
|
|
key := info.Stream + "/" + info.Name + "@" + info.Created.UTC().Format(time.RFC3339Nano)
|
|
pulled.Lock()
|
|
defer pulled.Unlock()
|
|
if first, ok := pulled.by[key]; ok {
|
|
return nil, fmt.Errorf("the durable consumer %s on %s is pulled in this process already, by %s: a second pull loop, by %s, "+
|
|
"would take every other event from it, so it is refused (design 47 H, H-A-196)", info.Name, info.Stream, first, binder)
|
|
}
|
|
pulled.by[key] = binder
|
|
var once sync.Once
|
|
return func() {
|
|
once.Do(func() {
|
|
pulled.Lock()
|
|
delete(pulled.by, key)
|
|
pulled.Unlock()
|
|
})
|
|
}, nil
|
|
}
|
|
|
|
// ownPackage is this package's path as a function's name begins with it.
|
|
const ownPackage = "git.novox.be/novox/mesh-sdk/go/process."
|
|
|
|
// callerOutside is the name of the first function on the stack outside this package: the part that asked the pull.
|
|
func callerOutside() string {
|
|
pcs := make([]uintptr, 16)
|
|
frames := runtime.CallersFrames(pcs[:runtime.Callers(3, pcs)])
|
|
for {
|
|
f, more := frames.Next()
|
|
if !strings.HasPrefix(f.Function, ownPackage) {
|
|
return f.Function
|
|
}
|
|
if !more {
|
|
return f.Function
|
|
}
|
|
}
|
|
}
|
|
|
|
// pulledOnce is a pull loop's context that releases its claim (PullOnce) when it is stopped or drained, or when the bus
|
|
// ends the loop.
|
|
type pulledOnce struct {
|
|
jetstream.ConsumeContext
|
|
release func()
|
|
}
|
|
|
|
func (p pulledOnce) Stop() { p.ConsumeContext.Stop(); p.release() }
|
|
func (p pulledOnce) Drain() { p.ConsumeContext.Drain(); p.release() }
|
|
|
|
func claimed(cc jetstream.ConsumeContext, release func()) jetstream.ConsumeContext {
|
|
go func() { <-cc.Closed(); release() }()
|
|
return pulledOnce{ConsumeContext: cc, release: release}
|
|
}
|