Skip to content

armgit / ARTEX

Public Imported from GitHub
0

ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)

Coalesce circuit-breaker writes onto one queue.

protonmatter847e32407a1b
diff --git a/server/llmpool.go b/server/llmpool.go
index a3901fc..65b9465 100644
--- a/server/llmpool.go
+++ b/server/llmpool.go
@@ -2,6 +2,7 @@ package server
import (
"log"
+ "sync"
"time"
"github.com/Autumn-27/artex/agent"
@@ -23,20 +24,12 @@ func newLLMHealthRegistry(pg *db.DB) *llmpool.Registry {
if pg == nil {
return llmpool.NewRegistry(nil, nil)
}
+ syncer := newHealthSync(pg.SaveLLMHealth, pg.ClearLLMHealth)
persist := func(id int64, st llmpool.State) {
- h := db.LLMHealth{ProfileID: id, Fails: st.Fails, Trips: st.Trips, LastError: st.LastError}
- if !st.OpenUntil.IsZero() {
- t := st.OpenUntil
- h.OpenUntil = &t
- }
- go func() {
- if err := pg.SaveLLMHealth(h); err != nil {
- log.Printf("[llmpool] failed to persist circuit-breaker state: %v", err)
- }
- }()
+ syncer.enqueue(id, healthOp{st: st})
}
forget := func(id int64) {
- go func() { _ = pg.ClearLLMHealth(id) }()
+ syncer.enqueue(id, healthOp{clear: true})
}
reg := llmpool.NewRegistry(persist, forget)
// Restore only windows that haven't expired (LoadLLMHealth filters), so a
@@ -54,6 +47,93 @@ func newLLMHealthRegistry(pg *db.DB) *llmpool.Registry {
return reg
}
+// healthOp is the latest circuit-breaker write for one profile. clear drops the row.
+type healthOp struct {
+ clear bool
+ st llmpool.State
+}
+
+// healthSync writes circuit-breaker state from one goroutine. A newer update for
+// the same profile replaces one that has not been written yet, so a burst of
+// state changes becomes one database call and the request path does not wait.
+type healthSync struct {
+ save func(db.LLMHealth) error
+ clear func(int64) error
+ mu sync.Mutex
+ pending map[int64]healthOp
+ wake chan struct{}
+}
+
+func newHealthSync(save func(db.LLMHealth) error, clear func(int64) error) *healthSync {
+ h := &healthSync{
+ save: save,
+ clear: clear,
+ pending: map[int64]healthOp{},
+ wake: make(chan struct{}, 1),
+ }
+ go h.loop()
+ return h
+}
+
+func (h *healthSync) enqueue(id int64, op healthOp) {
+ h.mu.Lock()
+ if h.pending == nil {
+ h.pending = map[int64]healthOp{}
+ }
+ h.pending[id] = op
+ h.mu.Unlock()
+ if h.wake == nil {
+ return
+ }
+ select {
+ case h.wake <- struct{}{}:
+ default:
+ }
+}
+
+func (h *healthSync) take() map[int64]healthOp {
+ h.mu.Lock()
+ defer h.mu.Unlock()
+ if len(h.pending) == 0 {
+ return nil
+ }
+ out := h.pending
+ h.pending = map[int64]healthOp{}
+ return out
+}
+
+func (h *healthSync) loop() {
+ for {
+ batch := h.take()
+ if len(batch) == 0 {
+ <-h.wake
+ continue
+ }
+ for id, op := range batch {
+ if op.clear {
+ if h.clear == nil {
+ continue
+ }
+ if err := h.clear(id); err != nil {
+ log.Printf("[llmpool] clear circuit-breaker profile #%d: %v", id, err)
+ }
+ continue
+ }
+ if h.save == nil {
+ continue
+ }
+ row := db.LLMHealth{ProfileID: id, Fails: op.st.Fails, Trips: op.st.Trips, LastError: op.st.LastError}
+ if !op.st.OpenUntil.IsZero() {
+ openUntil := op.st.OpenUntil
+ row.OpenUntil = &openUntil
+ }
+ if err := h.save(row); err != nil {
+ log.Printf("[llmpool] persist circuit-breaker profile #%d: %v", id, err)
+ }
+ }
+ }
+}
+
// poolMember builds one chain member from a profile, reusing the per-profile
// provider cache so every agent pointing at the same profile shares one provider
// (hence one rate limiter). Returns nil when the profile can't be built.
diff --git a/server/llmpool_health_test.go b/server/llmpool_health_test.go
new file mode 100644
index 0000000..c759d2c
--- /dev/null
+++ b/server/llmpool_health_test.go
@@ -0,0 +1,51 @@
+package server
+
+import (
+ "testing"
+
+ "github.com/Autumn-27/artex/db"
+ "github.com/Autumn-27/artex/llmpool"
+)
+
+func TestHealthSyncCoalescesToLatest(t *testing.T) {
+ h := &healthSync{pending: map[int64]healthOp{}}
+ h.enqueue(7, healthOp{st: llmpool.State{Fails: 1}})
+ h.enqueue(7, healthOp{st: llmpool.State{Fails: 4}})
+ h.enqueue(7, healthOp{clear: true})
+ h.enqueue(8, healthOp{st: llmpool.State{Fails: 2}})
+ batch := h.take()
+ if len(batch) != 2 || !batch[7].clear || batch[8].st.Fails != 2 {
+ t.Fatalf("batch=%+v", batch)
+ }
+ if again := h.take(); again != nil {
+ t.Fatalf("pending was not cleared: %+v", again)
+ }
+ var saved []db.LLMHealth
+ var cleared []int64
+ worker := &healthSync{
+ save: func(row db.LLMHealth) error {
+ saved = append(saved, row)
+ return nil
+ },
+ clear: func(id int64) error {
+ cleared = append(cleared, id)
+ return nil
+ },
+ }
+ worker.enqueue(7, healthOp{clear: true})
+ worker.enqueue(8, healthOp{st: llmpool.State{Fails: 3, LastError: "down"}})
+ for id, op := range worker.take() {
+ if op.clear {
+ if err := worker.clear(id); err != nil {
+ t.Fatal(err)
+ }
+ continue
+ }
+ if err := worker.save(db.LLMHealth{ProfileID: id, Fails: op.st.Fails, LastError: op.st.LastError}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if len(cleared) != 1 || cleared[0] != 7 || len(saved) != 1 || saved[0].ProfileID != 8 || saved[0].Fails != 3 {
+ t.Fatalf("cleared=%v saved=%+v", cleared, saved)
+ }
+}