From 4f382f6f5461ff9d17f5aa02ad275bd32579e36d Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 20 Oct 2025 13:29:33 +0200 Subject: [PATCH] Simpler code Signed-off-by: David Gageot --- pkg/gateway/catalog.go | 5 ++-- pkg/sync/oncerr.go | 16 ------------- pkg/sync/oncerr_test.go | 52 ----------------------------------------- 3 files changed, 2 insertions(+), 71 deletions(-) delete mode 100644 pkg/sync/oncerr.go delete mode 100644 pkg/sync/oncerr_test.go diff --git a/pkg/gateway/catalog.go b/pkg/gateway/catalog.go index 434130d81..89b25f888 100644 --- a/pkg/gateway/catalog.go +++ b/pkg/gateway/catalog.go @@ -6,8 +6,7 @@ import ( "fmt" "net/http" "strings" - - "github.com/docker/cagent/pkg/sync" + "sync" ) const DockerCatalogURL = "https://desktop.docker.com/mcp/catalog/v3/catalog.yaml" @@ -43,7 +42,7 @@ func ServerSpec(_ context.Context, serverName string) (Server, error) { } // Read the MCP Catalog only once and cache the result. -var readCatalogOnce = sync.OnceErr(func() (Catalog, error) { +var readCatalogOnce = sync.OnceValues(func() (Catalog, error) { // Use the JSON version because it's 3x time faster to parse than YAML. url := strings.Replace(DockerCatalogURL, ".yaml", ".json", 1) diff --git a/pkg/sync/oncerr.go b/pkg/sync/oncerr.go deleted file mode 100644 index 1959db6aa..000000000 --- a/pkg/sync/oncerr.go +++ /dev/null @@ -1,16 +0,0 @@ -package sync - -import "sync" - -func OnceErr[T any](fn func() (T, error)) func() (T, error) { - var once sync.Once - var result T - var err error - - return func() (T, error) { - once.Do(func() { - result, err = fn() - }) - return result, err - } -} diff --git a/pkg/sync/oncerr_test.go b/pkg/sync/oncerr_test.go deleted file mode 100644 index cf1c0e00a..000000000 --- a/pkg/sync/oncerr_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package sync - -import ( - "errors" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestOnceErr(t *testing.T) { - t.Parallel() - - called := 0 - fn := func() (int, error) { - called++ - return 42, nil - } - - memoizedFn := OnceErr(fn) - - value, err := memoizedFn() - require.NoError(t, err) - require.Equal(t, 42, value) - require.Equal(t, 1, called) - - value, err = memoizedFn() - require.NoError(t, err) - require.Equal(t, 42, value) - require.Equal(t, 1, called) // Didn't have to call the inner fn -} - -func TestOnceErr_Error(t *testing.T) { - t.Parallel() - - called := 0 - fn := func() (int, error) { - called++ - return 1337, errors.New("test error") - } - - memoizedFn := OnceErr(fn) - - value, err := memoizedFn() - require.Error(t, err) - require.Equal(t, 1337, value) - require.Equal(t, 1, called) - - value, err = memoizedFn() - require.Error(t, err) - require.Equal(t, 1337, value) - require.Equal(t, 1, called) // Didn't have to call the inner fn -}