From e26897d8a103e0fd1ef332ddb09368446d6613eb Mon Sep 17 00:00:00 2001 From: quobix Date: Tue, 24 Oct 2023 17:54:02 -0400 Subject: [PATCH] Updated logic to handle a single core The remote loader was blocking the only thread. Signed-off-by: quobix --- document_examples_test.go | 7 ++++--- index/rolodex_remote_loader.go | 21 +++++++++++++-------- index/spec_index_test.go | 2 +- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/document_examples_test.go b/document_examples_test.go index dcdc9749..61201d81 100644 --- a/document_examples_test.go +++ b/document_examples_test.go @@ -119,9 +119,10 @@ func ExampleNewDocument_fromWithDocumentConfigurationSuccess() { // create a DocumentConfiguration that allows loading file and remote references, and sets the baseURL // to somewhere that can resolve the relative references. config := datamodel.DocumentConfiguration{ - AllowFileReferences: true, - AllowRemoteReferences: true, - BaseURL: baseURL, + BaseURL: baseURL, + Logger: slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ + Level: slog.LevelError, + })), } // create a new document from specification bytes diff --git a/index/rolodex_remote_loader.go b/index/rolodex_remote_loader.go index 1f522451..f155678a 100644 --- a/index/rolodex_remote_loader.go +++ b/index/rolodex_remote_loader.go @@ -8,6 +8,7 @@ import ( "fmt" "github.com/pb33f/libopenapi/datamodel" "log/slog" + "runtime" "golang.org/x/sync/syncmap" "gopkg.in/yaml.v3" @@ -259,10 +260,14 @@ func (i *RemoteFS) Open(remoteURL string) (fs.File, error) { // if we're processing, we need to block and wait for the file to be processed // try path first if _, ok := i.ProcessingFiles.Load(remoteParsedURL.Path); ok { - i.logger.Debug("waiting for existing fetch to complete", "file", remoteURL, "remoteURL", remoteParsedURL.String()) - for { - if wf, ko := i.Files.Load(remoteParsedURL.Path); ko { - return wf.(*RemoteFile), nil + // we can't block if we only have a single CPU, as we'll deadlock, only when we're running in parallel + // can we block threads. + if runtime.GOMAXPROCS(-1) > 1 { + i.logger.Debug("waiting for existing fetch to complete", "file", remoteURL, "remoteURL", remoteParsedURL.String()) + for { + if wf, ko := i.Files.Load(remoteParsedURL.Path); ko { + return wf.(*RemoteFile), nil + } } } } @@ -288,10 +293,10 @@ func (i *RemoteFS) Open(remoteURL string) (fs.File, error) { i.logger.Debug("loading remote file", "file", remoteURL, "remoteURL", remoteParsedURL.String()) - // no handler func? use the default client. - if i.RemoteHandlerFunc == nil { - i.RemoteHandlerFunc = i.defaultClient.Get - } + //// no handler func? use the default client. + //if i.RemoteHandlerFunc == nil { + // i.RemoteHandlerFunc = i.defaultClient.Get + //} response, clientErr := i.RemoteHandlerFunc(remoteParsedURL.String()) if clientErr != nil { diff --git a/index/spec_index_test.go b/index/spec_index_test.go index 7f430512..168f7b2b 100644 --- a/index/spec_index_test.go +++ b/index/spec_index_test.go @@ -103,7 +103,7 @@ func TestSpecIndex_DigitalOcean(t *testing.T) { cf.AllowRemoteLookup = true cf.AvoidCircularReferenceCheck = true cf.Logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ - Level: slog.LevelError, + Level: slog.LevelDebug, })) // setting this baseURL will override the base