Skip to content

Commit

Permalink
SchemaLoader: use RWMutex to allow concurrency
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Feb 17, 2021
1 parent b076d39 commit fdf2203
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions schemaPool.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"errors"
"fmt"
"reflect"
"sync"

"github.com/xeipuuv/gojsonreference"
)
Expand All @@ -40,11 +41,27 @@ type schemaPoolDocument struct {
}

type schemaPool struct {
mutex sync.RWMutex
schemaPoolDocuments map[string]*schemaPoolDocument
jsonLoaderFactory JSONLoaderFactory
autoDetect *bool
}

func (p *schemaPool) get(key string) (*schemaPoolDocument, bool) {
p.mutex.RLock()
defer p.mutex.RUnlock()
if d, ok := p.schemaPoolDocuments[key]; ok {
return d, ok
}
return nil, false
}

func (p *schemaPool) set(key string, doc *schemaPoolDocument) {
p.mutex.Lock()
p.schemaPoolDocuments[key] = doc
p.mutex.Unlock()
}

func (p *schemaPool) parseReferences(document interface{}, ref gojsonreference.JsonReference, pooled bool) error {

var (
Expand All @@ -53,7 +70,7 @@ func (p *schemaPool) parseReferences(document interface{}, ref gojsonreference.J
reference = ref.String()
)
// Only the root document should be added to the schema pool if pooled is true
if _, ok := p.schemaPoolDocuments[reference]; pooled && ok {
if _, ok := p.get(reference); pooled && ok {
return fmt.Errorf("Reference already exists: \"%s\"", reference)
}

Expand All @@ -67,7 +84,7 @@ func (p *schemaPool) parseReferences(document interface{}, ref gojsonreference.J
err = p.parseReferencesRecursive(document, ref, draft)

if pooled {
p.schemaPoolDocuments[reference] = &schemaPoolDocument{Document: document, Draft: draft}
p.set(reference, &schemaPoolDocument{Document: document, Draft: draft})
}

return err
Expand Down Expand Up @@ -97,10 +114,10 @@ func (p *schemaPool) parseReferencesRecursive(document interface{}, ref gojsonre
if err == nil {
localRef, err = ref.Inherits(jsonReference)
if err == nil {
if _, ok := p.schemaPoolDocuments[localRef.String()]; ok {
if _, ok := p.get(localRef.String()); ok {
return fmt.Errorf("Reference already exists: \"%s\"", localRef.String())
}
p.schemaPoolDocuments[localRef.String()] = &schemaPoolDocument{Document: document, Draft: draft}
p.set(localRef.String(), &schemaPoolDocument{Document: document, Draft: draft})
}
}
}
Expand Down Expand Up @@ -167,7 +184,7 @@ func (p *schemaPool) GetDocument(reference gojsonreference.JsonReference) (*sche

refToURL.GetUrl().Fragment = ""

if cachedSpd, ok := p.schemaPoolDocuments[refToURL.String()]; ok {
if cachedSpd, ok := p.get(refToURL.String()); ok {
document, _, err := reference.GetPointer().Get(cachedSpd.Document)

if err != nil {
Expand All @@ -179,7 +196,7 @@ func (p *schemaPool) GetDocument(reference gojsonreference.JsonReference) (*sche
}

spd = &schemaPoolDocument{Document: document, Draft: cachedSpd.Draft}
p.schemaPoolDocuments[reference.String()] = spd
p.set(reference.String(), spd)

return spd, nil
}
Expand Down

0 comments on commit fdf2203

Please sign in to comment.