Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#741 uri cache mutex #742

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ jobs:
- run: go test -v -run TestRaceyPatternSchema -race ./...
env:
CGO_ENABLED: '1'
- run: go test -v -run TestIssue741 -race ./...
env:
CGO_ENABLED: '1'
- run: git --no-pager diff --exit-code

- if: runner.os == 'Linux'
Expand Down
43 changes: 43 additions & 0 deletions openapi3/issue741_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package openapi3

import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"

"github.com/stretchr/testify/require"
)

func TestIssue741(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
body := `{"openapi":"3.0.0","info":{"title":"MyAPI","version":"0.1","description":"An API"},"paths":{},"components":{"schemas":{"Foo":{"type":"string"}}}}`
_, err := w.Write([]byte(body))
if err != nil {
panic(err)
}
}))
defer ts.Close()

rootSpec := []byte(fmt.Sprintf(
`{"openapi":"3.0.0","info":{"title":"MyAPI","version":"0.1","description":"An API"},"paths":{},"components":{"schemas":{"Bar1":{"$ref":"%s#/components/schemas/Foo"}}}}`,
ts.URL,
))

wg := &sync.WaitGroup{}
n := 10
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
defer wg.Done()
loader := NewLoader()
loader.IsExternalRefsAllowed = true
doc, err := loader.LoadFromData(rootSpec)
require.NoError(t, err)
require.NotNil(t, doc)
}()
}
wg.Wait()
}
8 changes: 8 additions & 0 deletions openapi3/loader_uri_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import (
"net/http"
"net/url"
"path/filepath"
"sync"
)

// ReadFromURIFunc defines a function which reads the contents of a resource
// located at a URI.
type ReadFromURIFunc func(loader *Loader, url *url.URL) ([]byte, error)

var uriMu = &sync.RWMutex{}

// ErrURINotSupported indicates the ReadFromURIFunc does not know how to handle a
// given URI.
var ErrURINotSupported = errors.New("unsupported URI")
Expand Down Expand Up @@ -92,12 +95,17 @@ func URIMapCache(reader ReadFromURIFunc) ReadFromURIFunc {
}
uri := location.String()
var ok bool
uriMu.RLock()
if buf, ok = cache[uri]; ok {
uriMu.RUnlock()
return
}
uriMu.RUnlock()
if buf, err = reader(loader, location); err != nil {
return
}
uriMu.Lock()
defer uriMu.Unlock()
cache[uri] = buf
return
}
Expand Down