Pipeline main.8 failed TestBridgeSaysOneHeadMovedPerMove and TestBridgePerformsWhatIsOwedOnce on
shanks; both fail the same way at 403fb306, before the catalog client change, under load.
- The bridge read every head again, start true, whenever the registrations' first view said its
values after the start had begun, so a head moved just after the start was said as a start.
Only a registration adding a watched branch is adopted now (Watched.Set says so), decided under
one lock with the start's read.
- The head-moved test moved the head before the restart's read was answered, and read the state
heads before the bridge wrote it; it now waits for the stand-in's answer and watches heads.
- The owed test stopped the bridge while an outcome's publish was on its way (heard again after
the start), and an act said while the bridge stops can be handed to the stopped bridge's pull,
which the bus hands again only after the consumer's acknowledgement wait (30 s) past the
test's 20 s bound. It now hears the bridge's acknowledgements before the stop and waits the
consumer's acknowledgement wait for the act said meanwhile.
Builds: design 47 H §739; design 47 G §532; design 47 H-A-48
99 lines
2.8 KiB
Go
99 lines
2.8 KiB
Go
package bridge
|
|
|
|
import (
|
|
"slices"
|
|
"sync"
|
|
)
|
|
|
|
// Watched is the branches the bridge watches, by repository (design 47 G §4, §11, §507, §532): the registrations' watched
|
|
// branches by their origin, from a first view of `registrations` and then `registration-changed`; before the first
|
|
// registration, the branches the gate's `source` answers, of every repository. No branch is fixed in code (§7).
|
|
type Watched struct {
|
|
mu sync.Mutex
|
|
byRepo map[string][]string
|
|
source []string
|
|
registered bool
|
|
}
|
|
|
|
// Registered is one registration as the bridge reads it: its origin `<owner>/<name>` and its watched branches.
|
|
type Registered struct {
|
|
Origin string
|
|
Branches []string
|
|
}
|
|
|
|
// Set takes what the bridge read: the registrations (none before the first) and the gate's source's branches by name. It
|
|
// says whether a branch is watched now that was not (§532: a registration adding a watched branch is adopted); the same
|
|
// registrations read again add none.
|
|
func (w *Watched) Set(regs []Registered, source []string) (added bool) {
|
|
by := map[string][]string{}
|
|
for _, r := range regs {
|
|
for _, b := range r.Branches {
|
|
if b != "" && !slices.Contains(by[r.Origin], b) {
|
|
by[r.Origin] = append(by[r.Origin], b)
|
|
}
|
|
}
|
|
}
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
before := w.pairsLocked()
|
|
w.byRepo, w.registered = by, len(regs) > 0
|
|
w.source = slices.Clone(source)
|
|
for p := range w.pairsLocked() {
|
|
if !before[p] {
|
|
added = true
|
|
}
|
|
}
|
|
return added
|
|
}
|
|
|
|
// pairsLocked is every watched branch as `<repository> <branch>`, the repository empty before the first registration
|
|
// (the gate's source's branch, of every repository). The caller holds mu.
|
|
func (w *Watched) pairsLocked() map[string]bool {
|
|
out := map[string]bool{}
|
|
if !w.registered {
|
|
for _, b := range w.source {
|
|
out[" "+b] = true
|
|
}
|
|
return out
|
|
}
|
|
for repo, branches := range w.byRepo {
|
|
for _, b := range branches {
|
|
out[repo+" "+b] = true
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Watches says whether a move of branch in repository is one the bridge writes.
|
|
func (w *Watched) Watches(repository, branch string) bool {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
if !w.registered {
|
|
return slices.Contains(w.source, branch)
|
|
}
|
|
return slices.Contains(w.byRepo[repository], branch)
|
|
}
|
|
|
|
// Branches are the watched branches of repository, in the order the registrations name them; before the first
|
|
// registration, the gate's source's.
|
|
func (w *Watched) Branches(repository string) []string {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
if !w.registered {
|
|
return slices.Clone(w.source)
|
|
}
|
|
return slices.Clone(w.byRepo[repository])
|
|
}
|
|
|
|
// Repositories are the repositories the registrations name, sorted; none before the first registration.
|
|
func (w *Watched) Repositories() []string {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
out := make([]string, 0, len(w.byRepo))
|
|
for r := range w.byRepo {
|
|
out = append(out, r)
|
|
}
|
|
slices.Sort(out)
|
|
return out
|
|
}
|