package main import ( "fmt" "time" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/parts" "distribution/internal/holder" ) // stepBound bounds each bus operation of a publish well under the tool runner's 30 s call bound, so a hang is an error // naming its step and the publish answers with it. const stepBound = 10 * time.Second // step runs f, logs its start and its end with its time, and gives up on it after bound: the error names the step. An // operation given up on keeps running in its goroutine until the bus answers it. func step[T any](bound time.Duration, what string, f func() (T, error)) (T, error) { type res struct { v T err error } start := time.Now() logf("step %s starts", what) ch := make(chan res, 1) go func() { v, err := f(); ch <- res{v, err} }() select { case r := <-ch: logf("step %s ended after %s (%v)", what, time.Since(start).Round(time.Millisecond), r.err) return r.v, r.err case <-time.After(bound): var zero T err := fmt.Errorf("%s did not answer within %s", what, bound) logf("step %s: %v", what, err) return zero, err } } func stepErr(bound time.Duration, what string, f func() error) error { _, err := step(bound, what, func() (struct{}, error) { return struct{}{}, f() }) return err } // boundedObjects is the incoming object store with each operation bounded and logged. type boundedObjects struct{ parts.Objects } func (b boundedObjects) Used() (uint64, error) { return step(stepBound, "the incoming object store's fill (objects)", b.Objects.Used) } func (b boundedObjects) Put(name string, data []byte) error { return stepErr(stepBound, "the put of a chunk in the incoming object store (objects)", func() error { return b.Objects.Put(name, data) }) } func (b boundedObjects) Get(name string) ([]byte, error) { return step(stepBound, "the get of a chunk from the incoming object store (objects)", func() ([]byte, error) { return b.Objects.Get(name) }) } func (b boundedObjects) Delete(name string) error { return stepErr(stepBound, "the delete of a chunk in the incoming object store (objects)", func() error { return b.Objects.Delete(name) }) } // boundedTransfers is the transfers state with its reads and writes bounded and logged. type boundedTransfers struct { parts.Transfers[holder.Opening] } func (b boundedTransfers) Get(id string) (parts.Transfer[holder.Opening], bool, error) { type r struct { t parts.Transfer[holder.Opening] ok bool } x, err := step(stepBound, "the read of the transfer in the transfers state", func() (r, error) { t, ok, err := b.Transfers.Get(id) return r{t, ok}, err }) return x.t, x.ok, err } func (b boundedTransfers) Put(t parts.Transfer[holder.Opening]) error { return stepErr(stepBound, "the write of the transfer in the transfers state", func() error { return b.Transfers.Put(t) }) } func (b boundedTransfers) Delete(id string) error { return stepErr(stepBound, "the delete of the transfer in the transfers state", func() error { return b.Transfers.Delete(id) }) } // boundedCap is the incoming cap read from its binding, bounded and logged. func boundedCap(c func() (uint64, error)) func() (uint64, error) { return func() (uint64, error) { return step(stepBound, "the read of incoming's cap from its binding (objects)", c) } } // boundedArm is the idle key's write, bounded and logged. func boundedArm(a func(string, time.Duration) error) func(string, time.Duration) error { return func(id string, ttl time.Duration) error { return stepErr(stepBound, "the write of the idle key in the idle state", func() error { return a(id, ttl) }) } } // boundedFinding is the findings writer's say moved off the publish's path: the receiver says the incoming fill on every // open, and a publish never waits for it. One worker says them in order, each bounded and logged; one said while the worker // is full is dropped and logged (the next open says the fill again). func boundedFinding(f holder.Finding) holder.Finding { type said struct { kind, subject, words string raised bool } queue := make(chan said, 64) go func() { for s := range queue { _ = stepErr(stepBound, "the finding "+s.kind+" of "+s.subject, func() error { f(s.kind, s.subject, s.words, s.raised); return nil }) } }() return func(kind, subject, words string, raised bool) { select { case queue <- said{kind, subject, words, raised}: default: logf("the finding %s of %s is not said now: the writer is behind, the next open says it again", kind, subject) } } }