From 4373937817300be84c7e554e618770d4ec02379f Mon Sep 17 00:00:00 2001 From: ntsd Date: Fri, 20 Nov 2026 07:16:42 +0700 Subject: [PATCH] refactor(server): dedup CreateHubIfAbsent and GetOrCreateHub shared skeleton Task: zf-z-49f43665 refactor(server): dedup CreateHubIfAbsent and GetOrCreateHub shared skeleton (Ponytail Rung 2) --- server/pkg/zerohub/zerohub.go | 21 ++++++++++------- server/pkg/zerohub/zerohub_test.go | 38 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/server/pkg/zerohub/zerohub.go b/server/pkg/zerohub/zerohub.go index 87847d4..fe9a4ac 100644 --- a/server/pkg/zerohub/zerohub.go +++ b/server/pkg/zerohub/zerohub.go @@ -93,18 +93,14 @@ func (z *zeroHub) CreateHubIfAbsent(hubId string, metadata string, isPermanent b z.mu.Lock() defer z.mu.Unlock() - if existing := z.GetHubById(hubId); existing != nil { - return nil, ErrHubAlreadyExists - } - - newHub, err := z.buildHub(hubId, metadata, isPermanent) + h, created, err := z.getOrCreateHubLocked(hubId, metadata, isPermanent) if err != nil { return nil, err } - - z.HubStorage.Add(hubId, newHub) - - return newHub, nil + if !created { + return nil, ErrHubAlreadyExists + } + return h, nil } // GetOrCreateHub atomically returns the existing hub with the given ID, or @@ -116,6 +112,13 @@ func (z *zeroHub) GetOrCreateHub(hubId string, metadata string, isPermanent bool z.mu.Lock() defer z.mu.Unlock() + return z.getOrCreateHubLocked(hubId, metadata, isPermanent) +} + +// getOrCreateHubLocked performs the check-then-store under the caller's held +// z.mu, so concurrent calls for the same ID collapse to a single hub instead +// of orphaning one. created reports whether this call stored the hub. +func (z *zeroHub) getOrCreateHubLocked(hubId string, metadata string, isPermanent bool) (hub.Hub, bool, error) { if existing := z.GetHubById(hubId); existing != nil { return existing, false, nil } diff --git a/server/pkg/zerohub/zerohub_test.go b/server/pkg/zerohub/zerohub_test.go index 6f9d00c..4dd03f9 100644 --- a/server/pkg/zerohub/zerohub_test.go +++ b/server/pkg/zerohub/zerohub_test.go @@ -350,3 +350,41 @@ func TestGetOrCreateHubReturnsExisting(t *testing.T) { t.Fatal("expected distinct hubs for distinct IDs") } } + +// TestSharedCoreBuildHubFailure proves that when buildHub fails (unknown peer +// storage config), the shared getOrCreateHubLocked core surfaces the error and +// stores nothing, through both public methods. This guards the error branch +// that was previously duplicated in each method and now lives once in the core. +func TestSharedCoreBuildHubFailure(t *testing.T) { + zh := &zeroHub{ + cfg: &config.Config{ + App: config.AppConfig{ + PeerStorage: "bogus", + }, + }, + HubStorage: storage.NewMemoryStorage[hub.Hub](), + } + + h, err := zh.CreateHubIfAbsent("x", "meta", false) + if err == nil { + t.Fatalf("CreateHubIfAbsent: got nil error, want unknown peer storage error") + } + if h != nil { + t.Fatalf("CreateHubIfAbsent: got hub %p, want nil on error", h) + } + + h, created, err := zh.GetOrCreateHub("x", "meta", false) + if err == nil { + t.Fatalf("GetOrCreateHub: got nil error, want unknown peer storage error") + } + if h != nil { + t.Fatalf("GetOrCreateHub: got hub %p, want nil on error", h) + } + if created { + t.Fatalf("GetOrCreateHub created = true, want false on build error") + } + + if got := zh.GetHubById("x"); got != nil { + t.Fatalf("failed build left a hub in storage: %p", got) + } +}