mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-15 12:15:16 +00:00
## 背景 仓库早已迁移到 GitHub(`github.com/JianFeeeee/HomeAgent`,CI/CD 都在那边跑), gitcode 保留为**国内只读镜像**(`ci.yml` 的 sync-gitcode-main 快进推送)。 但 go.mod 的 module 路径一直是 `gitcode.com/JianFeeeee/...` —— 既与权威源 不符,`meta.SourceURL`(AGPL §13 的源码提供入口)也指向镜像而非实际运行的代码。 ## 关键区分(这是本次改动安全的前提) 两个 module 是**不同**的,影响面差一个数量级: | module | 谁依赖 | 改它的后果 | |---|---|---| | `.../HomeAgent`(主仓) | **只有主仓自己** | 内部重构,零外部影响 | | `.../homeagent-sdk`(SDK 仓) | 主仓 49 文件 + 所有外部插件 | 需重编插件 | ★ 实测确认:**插件不 import 主仓 module**(`strings qq/plugin.bin | grep HomeAgent` = 0), 只依赖 SDK 仓。而插件与内核的边界是 **RPC 方法名字符串** (`plugin.init` / `tool.invoke`…,见 proc/protocol.go),**与 Go module 名无关**。 ⇒ module 改名**不改变任何 wire 协议**,现网已装的插件照常运行。 SDK 的 GitHub 仓名是 `homeagentsdk`(无连字符),module 路径必须与之精确匹配, 否则 `go get` 会因路径不符失败,故取 `github.com/JianFeeeee/homeagentsdk`。 ## 改动 - `go.mod` / `go.sum`:两个 module 路径 + require/replace - 277 个 Go 文件 + Makefile + 打包脚本的 import 与 ldflags 路径 - SDK 仓 `meta.CoreModule`、`tools/hmapdev` 的 module 声明与工程模板 - 对外文档/链接的仓库地址(README、PLUGIN_DEV、OVERVIEW 等) ## 明确**保留** gitcode 的地方(镜像机制,不能动) - `GITCODE_TOKEN`、`GC_URL="…@gitcode.com"`(CI 同步 job) - `gitcode.com/api/v5/...`(release 附件上传) - git remote `gitcode`(镜像拉取/推送) - `docs/zh/ci-cd-runbook.md`(整篇讲镜像运维) ## 验证 - `go build ./...` / `go vet ./...` 绿 - `go test ./... -count=1` → 48 包 ok / 0 FAIL - hmapdev 工具链编译通过(`third_party/homeagent-sdk/tools/hmapdev`) - 全仓残留检查:module 形态的 `gitcode.com/JianFeeeee` = 0 - 镜像机制抽验:GC_URL / @gitcode / api/v5 / remote 均完好 ## 顺带修正 `hmapdev/templates.go` 的注释此前写「gitcode 的模块不在 proxy.golang.org 上」—— module 已迁 GitHub,该论断不再成立。改为如实说明**为何仍默认 replace 到本机 SDK**: 离线可构建、避免版本漂移、内网可用。理由与托管地无关,故 replace 保留。
191 lines
5.0 KiB
Go
191 lines
5.0 KiB
Go
package plugin
|
||
|
||
import (
|
||
"sync"
|
||
"sync/atomic"
|
||
"testing"
|
||
"time"
|
||
|
||
sdk "github.com/JianFeeeee/HomeAgent/internal/sdk"
|
||
)
|
||
|
||
// ===== StopAll 并行化 =====
|
||
//
|
||
// 串行停 23 个插件、每个最坏 5s(plugin.stop 调用)+5s(等退出)+2s(收割)
|
||
// = 最坏 276s,而 systemd 只给 90s ⇒ 关停几乎必然被 SIGKILL。
|
||
// 线上实测:每次 stop 都 "State 'stop-sigterm' timed out",
|
||
// 进程组里 23 个插件全退完了,最后那条 [homed] stopped 仍打不出来。
|
||
//
|
||
// 并行后最坏约等于单个插件的预算(约 12s),而不是 N 倍。
|
||
//
|
||
// ★ 并行化最大的风险是死锁与重复释放:Stop 会触发 markExited →
|
||
// onExit → ReclaimOwner,后者要读共享内存段。所以判据同时盯
|
||
// "真的并行"与"不死锁、不错杀"。
|
||
|
||
// stubPlugin 是最小可用插件。
|
||
type stubPlugin struct {
|
||
name string
|
||
delay time.Duration
|
||
stopped atomic.Bool
|
||
panicOnStop bool
|
||
stopCount atomic.Int32
|
||
}
|
||
|
||
func newStubPlugin(name string, delay time.Duration) *stubPlugin {
|
||
return &stubPlugin{name: name, delay: delay}
|
||
}
|
||
|
||
func (s *stubPlugin) Name() string { return s.name }
|
||
func (s *stubPlugin) Start(*sdk.PluginSDK) error { return nil }
|
||
func (s *stubPlugin) Stop() error {
|
||
s.stopCount.Add(1)
|
||
if s.panicOnStop {
|
||
panic("stub: 故意 panic")
|
||
}
|
||
if s.delay > 0 {
|
||
time.Sleep(s.delay)
|
||
}
|
||
s.stopped.Store(true)
|
||
return nil
|
||
}
|
||
|
||
// StopAll 必须真的并行,否则退回串行 = 关停超时。
|
||
func TestStopAllStopsInParallel(t *testing.T) {
|
||
const n = 8
|
||
const each = 120 * time.Millisecond
|
||
|
||
var plugins []sdk.Plugin
|
||
var stubs []*stubPlugin
|
||
for i := 0; i < n; i++ {
|
||
s := newStubPlugin("p", each)
|
||
_ = i
|
||
stubs = append(stubs, s)
|
||
plugins = append(plugins, s)
|
||
}
|
||
r := &Registry{instances: plugins}
|
||
|
||
start := time.Now()
|
||
r.StopAll()
|
||
elapsed := time.Since(start)
|
||
|
||
serial := n * each
|
||
// 串行实现会耗时 serial;留一半余量仍能可靠区分。
|
||
if elapsed > serial/2 {
|
||
t.Errorf("StopAll 是串行的:%d 个插件各 %v 用了 %v(串行≈%v,并行应≈%v)",
|
||
n, each, elapsed, serial, each)
|
||
}
|
||
for _, s := range stubs {
|
||
if !s.stopped.Load() {
|
||
t.Errorf("插件 %s 没被停掉", s.name)
|
||
}
|
||
}
|
||
}
|
||
|
||
// 一个插件 panic 不能带崩整个关停,也不能让其它插件停不掉。
|
||
func TestStopAllSurvivesPanickingPlugin(t *testing.T) {
|
||
bad := newStubPlugin("bad", 0)
|
||
bad.panicOnStop = true
|
||
good := newStubPlugin("good", 0)
|
||
r := &Registry{instances: []sdk.Plugin{bad, good}}
|
||
|
||
done := make(chan struct{})
|
||
go func() {
|
||
defer close(done)
|
||
defer func() {
|
||
if rec := recover(); rec != nil {
|
||
t.Errorf("插件的 panic 不该冒到关停流程上:%v", rec)
|
||
}
|
||
}()
|
||
r.StopAll()
|
||
}()
|
||
|
||
select {
|
||
case <-done:
|
||
case <-time.After(10 * time.Second):
|
||
t.Fatal("StopAll 卡死(可能死锁)")
|
||
}
|
||
if !good.stopped.Load() {
|
||
t.Error("一个插件 panic 不该让其它插件停不掉")
|
||
}
|
||
}
|
||
|
||
// 关停期间必须冻结自动重启,否则崩溃判定会在关停中把插件重新拉起
|
||
// (段已拆而进程还在 → SIGBUS)。
|
||
func TestStopAllFreezesAutoRestart(t *testing.T) {
|
||
s := newStubPlugin("p", 0)
|
||
r := &Registry{instances: []sdk.Plugin{s}}
|
||
r.StopAll()
|
||
if !r.shuttingDown.Load() {
|
||
t.Error("StopAll 之后 shuttingDown 应为 true")
|
||
}
|
||
if r.instances != nil {
|
||
t.Error("StopAll 之后 instances 应被清空")
|
||
}
|
||
}
|
||
|
||
// 每个插件恰好 Stop 一次:重复调用会二次释放共享段/重复跑 stop handler。
|
||
func TestStopAllStopsEachPluginExactlyOnce(t *testing.T) {
|
||
var plugins []sdk.Plugin
|
||
var stubs []*stubPlugin
|
||
for i := 0; i < 5; i++ {
|
||
s := newStubPlugin("p", 0)
|
||
stubs = append(stubs, s)
|
||
plugins = append(plugins, s)
|
||
}
|
||
r := &Registry{instances: plugins}
|
||
r.StopAll()
|
||
for _, s := range stubs {
|
||
if c := s.stopCount.Load(); c != 1 {
|
||
t.Errorf("Stop 被调 %d 次,应恰好 1 次", c)
|
||
}
|
||
}
|
||
}
|
||
|
||
// stop handler 必须在 Stop 之前跑完(handler 负责解绑通道等),
|
||
// 且并行化后这个顺序不能被破坏。
|
||
//
|
||
// handler 挂在 PluginSDK 上(RegisterStopHandler),由 runStopHandlers
|
||
// 通过 r.sdkRefs 取出执行 —— 所以判据必须真的构造一个 PluginSDK,
|
||
// 否则测的是一条不存在的注册路径。
|
||
func TestStopAllRunsStopHandlerBeforeStop(t *testing.T) {
|
||
var mu sync.Mutex
|
||
var order []string
|
||
|
||
s := newStubPlugin("p", 0)
|
||
r := &Registry{
|
||
instances: []sdk.Plugin{s},
|
||
sdkRefs: map[string]*sdk.PluginSDK{},
|
||
}
|
||
sdkn := sdk.New("p", sdk.SDKConfig{})
|
||
sdkn.RegisterStopHandler(func() {
|
||
mu.Lock()
|
||
order = append(order, "handler")
|
||
mu.Unlock()
|
||
})
|
||
r.sdkRefs["p"] = sdkn
|
||
|
||
probe := &orderProbePlugin{stubPlugin: s, onStop: func() {
|
||
mu.Lock()
|
||
order = append(order, "stop")
|
||
mu.Unlock()
|
||
}}
|
||
r.instances = []sdk.Plugin{probe}
|
||
|
||
r.StopAll()
|
||
mu.Lock()
|
||
defer mu.Unlock()
|
||
if len(order) != 2 || order[0] != "handler" || order[1] != "stop" {
|
||
t.Errorf("stop handler 必须先于 Stop 执行,实际顺序 %v", order)
|
||
}
|
||
}
|
||
|
||
type orderProbePlugin struct {
|
||
*stubPlugin
|
||
onStop func()
|
||
}
|
||
|
||
func (o *orderProbePlugin) Stop() error {
|
||
o.onStop()
|
||
return o.stubPlugin.Stop()
|
||
}
|