Files
mesh-catalog/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/gate.go
T

91 lines
3.1 KiB
Go

package main
import (
"sync"
"time"
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
)
// The holder of the seat mesh-delivery (novox/hq ADR 0239, to-be 47): from milestone G's delivery it keeps no merge states
// and asks no Approve per merge (design 47 G §172, §431, §592, §593, §613). What it keeps is the delivery records
// (record.go), the source revision per watched branch (merges.go) and what it owes the forge. It merges nothing.
// Seat is the seat the gate holds.
const Seat = "mesh-delivery"
// Holder is the seat's holder.
type Holder struct {
// Settings reads the operator's values (settings.go); nil keeps every default.
Settings func() Settings
Now func() time.Time
Logf func(format string, a ...any)
// Merges and Old are the old sections the switch reads (switch.go); Source is the gate's source (merges.go).
Merges MergeStore
Old OldState
Source SourceStore
// Owed says what the gate owes the forge as the seat's event owed (owed.go).
Owed OwedSayer
// Records keeps the delivery records on the gate's ledger (record.go, ADR 0656 §7); nil keeps none.
Records *Records
// Consumer applies the step events to the records (consumer.go); nil applies none.
Consumer *StepConsumer
// SaySeat says an event of the seat mesh-delivery (recordevents.go); nil says none.
SaySeat SeatSay
// Findings is the gate's one held writer of its own findings (findings.go); nil writes none.
Findings FindingWriter
// Say says a condition to the operator, and Conditions keeps which the gate raised (conditions.go); nil says none.
Say func(c oc.Condition) error
Conditions ConditionStore
// said is what Conditions keeps, read at the first look; sayFailures the looks in a row a condition could not be said.
// Touched by React alone.
said map[string]string
sayFailures int
mu sync.Mutex
started time.Time
refusals []string
// tried counts the owed effects tried since this owner started, by how each ended (novox/hq issue 407).
tried owedTries
// reactMu keeps one React at a time; poke asks for a look (react.go); wanted is when the next is wanted, and its timer.
reactMu sync.Mutex
poke chan struct{}
wanted lookWanted
}
// owedTries is how many owed effects were tried since this owner started, and when the last was.
type owedTries struct {
done, failed, givenUp int
last time.Time
}
// The word this owner signs its acts with.
const byOwner = "mesh-delivery"
func (h *Holder) init() {
if h.Now == nil {
h.Now = func() time.Time { return time.Now().UTC() }
}
if h.Logf == nil {
h.Logf = func(string, ...any) {}
}
h.poke = make(chan struct{}, 1)
h.started = h.Now()
}
// Refuse keeps a refusal from outside the lock.
func (h *Holder) Refuse(err error) {
h.mu.Lock()
defer h.mu.Unlock()
h.refuse(err)
}
// refuse keeps a refusal, said, for the status; under the lock.
func (h *Holder) refuse(err error) {
h.Logf("[mesh-delivery] %v", err)
h.refusals = append(h.refusals, h.Now().Format(time.RFC3339)+" "+err.Error())
if len(h.refusals) > 50 {
h.refusals = h.refusals[len(h.refusals)-50:]
}
}