mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
The probe trips a shared breaker through one interpreted instance over an in-memory store. It checks that a second instance sees the breaker open after one refresh and sees it closed after a success. It also sums in-flight counts across two instances. A mismatch fails make yaegi-check.
100 lines
3.4 KiB
Go
100 lines
3.4 KiB
Go
// This file probes the shared breaker and shared in-flight counter under Yaegi. Two loops stand for two pods over
|
|
// one in-memory store, so the interpreter runs the loop, the flush, the refresh and the merge, not only the import.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/traefik/yaegi/interp"
|
|
)
|
|
|
|
const (
|
|
ssProbeOK = "ok"
|
|
// ssOpenBase, ssOpenMax and ssFailWindow are untyped nanosecond counts, so the interpreted call converts them to time.Duration.
|
|
ssOpenBase = int64(30 * time.Second)
|
|
ssOpenMax = int64(5 * time.Minute)
|
|
ssFailWindow = int64(time.Minute)
|
|
// ssMaxRequest gives the in-flight tracker two epochs, so a flush that straddles an epoch edge is still read.
|
|
ssMaxRequest = int64(5 * time.Minute)
|
|
)
|
|
|
|
// ssProbeSource returns "ok" or the first mismatch. PKG stands for the interpreted package's name.
|
|
// The loops write synchronously so each refresh reads what the previous step wrote.
|
|
const ssProbeSource = `func sharedStateProbe() string {
|
|
store := PKG.newMemoryStore()
|
|
sync := func(f func()) { f() }
|
|
la := PKG.newSharedStateLoop(store, sync, nil)
|
|
lb := PKG.newSharedStateLoop(store, sync, nil)
|
|
defer la.close()
|
|
defer lb.close()
|
|
|
|
a := PKG.newSharedBreaker(la, "probe", []string{"p"}, 3, %d, %d, %d)
|
|
b := PKG.newSharedBreaker(lb, "probe", []string{"p"}, 3, %d, %d, %d)
|
|
for i := 0; i < 3; i++ {
|
|
a.recordFailure("p")
|
|
}
|
|
la.refresh()
|
|
lb.refresh()
|
|
if !b.isOpen("p") {
|
|
return "breaker: second instance closed after the first tripped it and one refresh"
|
|
}
|
|
if b.isOpen("other") {
|
|
return "breaker: a trip on one id opened another"
|
|
}
|
|
b.recordSuccess("p")
|
|
lb.refresh()
|
|
if b.isOpen("p") {
|
|
return "breaker: second instance still open after its own success"
|
|
}
|
|
|
|
ia := PKG.newSharedInflight(la, []string{"p"}, %d)
|
|
ib := PKG.newSharedInflight(lb, []string{"p"}, %d)
|
|
e1 := ia.begin("p")
|
|
e2 := ia.begin("p")
|
|
e3 := ib.begin("p")
|
|
// Each refresh flushes its own deltas then reads, so the first instance needs a second pass to see the other's.
|
|
la.refresh()
|
|
lb.refresh()
|
|
la.refresh()
|
|
if got := ia.shared("p"); got != 3 {
|
|
return fmt.Sprintf("inflight: first instance sees %%d in flight, want 3", got)
|
|
}
|
|
if got := ib.merged("p", 1); got != 3 {
|
|
return fmt.Sprintf("inflight: second instance merged %%d, want 3", got)
|
|
}
|
|
e1()
|
|
e1()
|
|
e2()
|
|
e3()
|
|
la.refresh()
|
|
lb.refresh()
|
|
la.refresh()
|
|
if got := ia.shared("p"); got != 0 {
|
|
return fmt.Sprintf("inflight: %%d still in flight after every end, want 0", got)
|
|
}
|
|
return "ok"
|
|
}`
|
|
|
|
// exerciseSharedState trips a shared breaker through one interpreted instance, reads it from another, and sums in-flight across two.
|
|
func exerciseSharedState(i *interp.Interpreter, pkgName string) error {
|
|
src := fmt.Sprintf(strings.ReplaceAll(ssProbeSource, "PKG", pkgName),
|
|
ssOpenBase, ssOpenMax, ssFailWindow, ssOpenBase, ssOpenMax, ssFailWindow, ssMaxRequest, ssMaxRequest)
|
|
if _, err := i.Eval(`import "fmt"`); err != nil {
|
|
return fmt.Errorf("shared state probe: import fmt under yaegi: %w", err)
|
|
}
|
|
if _, err := i.Eval(src); err != nil {
|
|
return fmt.Errorf("shared state probe: declare the probe under yaegi: %w", err)
|
|
}
|
|
v, err := i.Eval("sharedStateProbe()")
|
|
if err != nil {
|
|
return fmt.Errorf("shared state probe: run under yaegi: %w", err)
|
|
}
|
|
if got := v.String(); got != ssProbeOK {
|
|
return fmt.Errorf("shared state probe: %s", got)
|
|
}
|
|
fmt.Println("yaegi-check: shared breaker and shared in-flight matched native Go under the interpreter")
|
|
return nil
|
|
}
|