From d304bcc31fedf63865fc807e0a0d01ab398b2311 Mon Sep 17 00:00:00 2001 From: Gary Holtz Date: Wed, 4 Jun 2025 12:36:29 -0400 Subject: [PATCH] chore: Adding thread safety, potentially --- commands/cmdutils/factory.go | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/commands/cmdutils/factory.go b/commands/cmdutils/factory.go index 4c7a0a68d..9c17720f0 100644 --- a/commands/cmdutils/factory.go +++ b/commands/cmdutils/factory.go @@ -3,6 +3,7 @@ package cmdutils import ( "fmt" "strings" + "sync" gitlab "gitlab.com/gitlab-org/api/client-go" "gitlab.com/gitlab-org/cli/api" @@ -25,6 +26,10 @@ type Factory struct { Config func() (config.Config, error) Branch func() (string, error) IO *iostreams.IOStreams + + clientOnce sync.Once + client *gitlab.Client + clientErr error } // MIT License @@ -62,6 +67,11 @@ func (f *Factory) RepoOverride(repo string) error { if err == nil { OverrideAPIProtocol(cfg, newRepo) } + + f.client = nil + f.clientErr = nil + f.clientOnce = sync.Once{} + f.HttpClient = func() (*gitlab.Client, error) { return LabClientFunc(newRepo.RepoHost(), cfg, false) } @@ -126,12 +136,20 @@ func httpClientFunc() (*gitlab.Client, error) { return LabClientFunc(repo.RepoHost(), cfg, false) } +func (f *Factory) safeHttpClientFunc() func() (*gitlab.Client, error) { + return func() (*gitlab.Client, error) { + f.clientOnce.Do(func() { + f.client, f.clientErr = httpClientFunc() + }) + return f.client, f.clientErr + } +} + func NewFactory() *Factory { - return &Factory{ - Config: configFunc, - Remotes: remotesFunc, - HttpClient: httpClientFunc, - BaseRepo: baseRepoFunc, + f := &Factory{ + Config: configFunc, + Remotes: remotesFunc, + BaseRepo: baseRepoFunc, Branch: func() (string, error) { currentBranch, err := git.CurrentBranch() if err != nil { @@ -141,4 +159,7 @@ func NewFactory() *Factory { }, IO: iostreams.Init(), } + + f.HttpClient = f.safeHttpClientFunc() + return f } -- GitLab