Files
mesh-catalog/modules/distribution/cmd/distribution-tools/bounded.go
T
jschoubben bfc877209f Distribution: each bus step of a publish bounded at 10 s and logged by name
Builds: design 47 G (publish path of oci-artifact-registry; no design line covers the bounds or log lines)
2026-11-15 01:30:05 +01:00

103 lines
3.9 KiB
Go

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, bounded and logged: the receiver says the incoming fill through it on an open.
func boundedFinding(f holder.Finding) holder.Finding {
return func(kind, subject, words string, raised bool) {
_ = stepErr(stepBound, "the finding "+kind+" of "+subject, func() error { f(kind, subject, words, raised); return nil })
}
}