An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:zap: Cache workspace drive detection for runtime information https://github.com/siyuan-note/siyuan/issues/20132
Daniel535b9ea451db
diff --git a/kernel/util/runtime.go b/kernel/util/runtime.goindex 478819a6fb..cd86d9f917 100644--- a/kernel/util/runtime.go+++ b/kernel/util/runtime.go@@ -18,6 +18,7 @@ package utilimport ("bytes"+ "context""crypto/rand""fmt""net/url"@@ -152,7 +153,7 @@ func getWorkspaceDriveType() string {if IsMobileContainer() {return ghw.DriveTypeSSD.String()}- return detectWorkspaceDriveType()+ return workspaceDrive.get(context.Background(), detectWorkspaceDriveType)}// detectWorkspaceDriveType 仅返回实际检测到的磁盘类型,不根据平台推断。diff --git a/kernel/util/runtime_info.go b/kernel/util/runtime_info.goindex 34741eae99..6416cedf4e 100644--- a/kernel/util/runtime_info.go+++ b/kernel/util/runtime_info.go@@ -81,7 +81,9 @@ func RuntimeInfo(ctx context.Context) string {logStage("go_heap")driveType := ""if !IsMobileContainer() {- driveType = detectWorkspaceDriveType()+ storageCtx, cancel := context.WithTimeout(ctx, time.Second)+ driveType = workspaceDrive.get(storageCtx, detectWorkspaceDriveType)+ cancel()}logStage("workspace_storage")if driveType == "" {diff --git a/kernel/util/workspace_drive.go b/kernel/util/workspace_drive.gonew file mode 100644index 0000000000..6a344dc779--- /dev/null+++ b/kernel/util/workspace_drive.go@@ -0,0 +1,30 @@+package util++import (+ "context"+ "sync"+)++var workspaceDrive = workspaceDriveTypeCache{done: make(chan struct{})}++type workspaceDriveTypeCache struct {+ once sync.Once+ done chan struct{}+ value string+}++// get 复用内核生命周期内的检测结果,调用者取消等待不会中断或重复启动系统查询。+func (cache *workspaceDriveTypeCache) get(ctx context.Context, detect func() string) string {+ cache.once.Do(func() {+ go func() {+ cache.value = detect()+ close(cache.done)+ }()+ })+ select {+ case <-cache.done:+ return cache.value+ case <-ctx.Done():+ return ""+ }+}diff --git a/kernel/util/workspace_drive_test.go b/kernel/util/workspace_drive_test.gonew file mode 100644index 0000000000..0c3be3c785--- /dev/null+++ b/kernel/util/workspace_drive_test.go@@ -0,0 +1,54 @@+package util++import (+ "context"+ "sync"+ "testing"+ "testing/synctest"+ "time"+)++func TestWorkspaceDriveCache(t *testing.T) {+ for _, value := range []string{"SSD", ""} {+ t.Run(value, func(t *testing.T) {+ cache := workspaceDriveTypeCache{done: make(chan struct{})}+ calls := 0+ detect := func() string { calls++; return value }+ var callers sync.WaitGroup+ for range 10 {+ callers.Go(func() {+ if got := cache.get(context.Background(), detect); got != value {+ t.Errorf("got %q, want %q", got, value)+ }+ })+ }+ callers.Wait()+ if calls != 1 {+ t.Fatalf("detection ran %d times", calls)+ }+ })+ }+}++func TestWorkspaceDriveCacheCancellation(t *testing.T) {+ synctest.Test(t, func(t *testing.T) {+ cache := workspaceDriveTypeCache{done: make(chan struct{})}+ release := make(chan struct{})+ calls := 0+ detect := func() string { calls++; <-release; return "HDD" }+ ctx, cancel := context.WithTimeout(context.Background(), time.Second)+ defer cancel()+ if got := cache.get(ctx, detect); got != "" || ctx.Err() != context.DeadlineExceeded {+ t.Fatalf("slow query did not time out: %q, %v", got, ctx.Err())+ }+ canceled, stop := context.WithCancel(context.Background())+ stop()+ if got := cache.get(canceled, detect); got != "" {+ t.Fatalf("canceled caller returned %q", got)+ }+ close(release)+ if got := cache.get(context.Background(), detect); got != "HDD" || calls != 1 {+ t.Fatalf("query result was not reused: %q, calls=%d", got, calls)+ }+ })+}