From f2aa7fe3462cfc6917aa27e9661c15cb404c74a2 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 27 Oct 2026 01:36:33 +0800 Subject: [PATCH] plugin: HasGateway looks at the plugins once a second, not on every request (#3725) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every agent request asks plugin.HasGateway (gatewayHooks, and GatewayBegin again when on). It read plugins.json through lastgood and steady, cloned the list, and stat'ed and read every enabled plugin's package.json. With 12 plugins installed and none with hooks: Windows (Ryzen AI MAX+ 395, Defender on): 457µs, 51KB, 360 allocs macOS (M4 Pro): 251-286µs, 36KB, 260 allocs per call. It now keeps its answer for a second, keyed on plugins.json's path and filememo.Gen, as middleware.current does: magpie's own writes (plugin add/remove/on/off all go through save, which calls filememo.Forget) are seen at once, another program's within a second. Now 0.5µs/640B on Windows and 0.3-0.85µs/208B on macOS. Run at b9ab17231 plus this change: - go test -count=1 -timeout 45m ./internal/plugin ./internal/middleware ./internal/gwmw ./internal/gateway: all ok (gateway 541s). - TestHasGatewayLooksOnceASecond (new) is red with the cache turned off by hand ("the plugins were read again within the second"), green with it. It also checks filememo.Forget and an expired look see a change. - The numbers come from a throwaway benchmark (12 plugin dirs with package.json, plugins.json with options) run on this Mac and as a windows/amd64 test binary on the Windows box, old and new code. - GOOS=linux and GOOS=windows go vet -tags nogui on the three packages, gofmt. Not tried: a hooks plugin edited by hand outside magpie in the real app (the test covers the one-second look). Rebased onto origin/main 3fd7e1698 (no plugin or filememo change between); go build ./... and go test -count=1 ./internal/plugin ok at the pushed head. Before that, at b5d17b26e: go build ./..., linux and windows go vet -tags nogui ./internal/plugin, and go test -count=1 ./internal/plugin ./internal/middleware ./internal/gwmw all ok. --- internal/plugin/gateway.go | 29 ++++++++++++++++++++++++++-- internal/plugin/gateway_test.go | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/internal/plugin/gateway.go b/internal/plugin/gateway.go index 1a3b76732..f02e45ef7 100644 --- a/internal/plugin/gateway.go +++ b/internal/plugin/gateway.go @@ -7,7 +7,10 @@ import ( "path/filepath" "strings" "sync" + "sync/atomic" "time" + + "github.com/yetone/magpie/internal/filememo" ) // Gateway is the asynchronous request-hook entry a package runs in Bun: @@ -122,15 +125,37 @@ func GatewayStates(ctx context.Context) map[string]GatewayState { } // HasGateway avoids starting Bun for users who have no asynchronous hooks. +// Every request asks it, so the plugins are looked at once a second, and at +// once after magpie wrote a file of its own (filememo.Gen): reading +// plugins.json and each plugin's package.json took ~1ms a request with 12 +// plugins on Windows (#3725). func HasGateway() bool { + now, g, list := time.Now().UnixNano(), filememo.Gen(), listPath() + if c := hasGateway.Load(); c != nil && c.gen == g && c.list == list && now < c.until { + return c.on + } + on := false for _, e := range Load().Plugins { if !e.Off && Gateway(Target(e.Spec)) != "" { - return true + on = true + break } } - return false + hasGateway.Store(&gatewaySeen{on: on, gen: g, list: list, until: now + int64(time.Second)}) + return on } +// gatewaySeen is HasGateway's last look: for the plugins.json at list, +// while filememo's generation is gen, until until (UnixNano). +type gatewaySeen struct { + on bool + gen uint64 + list string + until int64 +} + +var hasGateway atomic.Pointer[gatewaySeen] + // GatewayRun belongs to the host that began it, even if the plugin list changes // before the response. Close releases its private state and cancels active I/O. type GatewayRun struct { diff --git a/internal/plugin/gateway_test.go b/internal/plugin/gateway_test.go index 97b896194..a324a46b7 100644 --- a/internal/plugin/gateway_test.go +++ b/internal/plugin/gateway_test.go @@ -11,6 +11,8 @@ import ( "strings" "testing" "time" + + "github.com/yetone/magpie/internal/filememo" ) func gatewayFixture(t *testing.T, source string) string { @@ -28,6 +30,38 @@ func gatewayFixture(t *testing.T, source string) string { return dir } +// TestHasGatewayLooksOnceASecond: every request asks HasGateway, so it +// reads the plugins at most once a second (#3725), and at once after magpie +// wrote a file of its own. +func TestHasGatewayLooksOnceASecond(t *testing.T) { + sandbox(t) + dir := gatewayFixture(t, `export function onRequest(body){return body}`) + if !HasGateway() { + t.Fatal("a plugin with hooks has no gateway") + } + pkg := filepath.Join(dir, "package.json") + if err := os.WriteFile(pkg, []byte(`{"name":"x"}`), 0o644); err != nil { + t.Fatal(err) + } + if !HasGateway() { + t.Fatal("the plugins were read again within the second") + } + filememo.Forget() + if HasGateway() { + t.Fatal("magpie's own write wasn't looked at") + } + if err := os.WriteFile(pkg, []byte(`{"magpie":{"hooks":"./request.js"}}`), 0o644); err != nil { + t.Fatal(err) + } + // a second later: the look is due + c := *hasGateway.Load() + c.until = time.Now().UnixNano() + hasGateway.Store(&c) + if !HasGateway() { + t.Fatal("another program's change wasn't seen a second later") + } +} + func TestGatewayHookWaitsForStartingHost(t *testing.T) { sandbox(t) gatewayFixture(t, `export function onRequest(body){return {...body,note:"recalled"}}`)