Skip to content

Commit

Permalink
Updated logic to handle a single core
Browse files Browse the repository at this point in the history
The remote loader was blocking the only thread.

Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Oct 24, 2023
1 parent f644fbb commit e26897d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
7 changes: 4 additions & 3 deletions document_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 13 additions & 8 deletions index/rolodex_remote_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"github.com/pb33f/libopenapi/datamodel"
"log/slog"
"runtime"

"golang.org/x/sync/syncmap"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -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
}
}
}
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion index/spec_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e26897d

Please sign in to comment.