forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change creates a local JSON-LD Document Cache loader using a cache of type *sync.Map to avoid 'error on cache writes' panics. This change also introduces the 'benchmark' Make target to support benchmarking code in the framework. closes hyperledger-archives#1833 for the fatal error mentioned in the comment Also related and closes the discussion about the same panic topic in hyperledger-archives#2487 Signed-off-by: Baha Shaaban <[email protected]>
- Loading branch information
Baha Shaaban
committed
Mar 9, 2021
1 parent
ada2929
commit f83e0fc
Showing
13 changed files
with
494 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package jsonld | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/piprate/json-gold/ld" | ||
) | ||
|
||
// CachingDocumentLoader is similar to json-gold's CachingDocumentLoader but uses cache as *sync.Map instead. | ||
type CachingDocumentLoader struct { | ||
nextLoader ld.DocumentLoader | ||
cache map[string]interface{} | ||
rwMutex sync.RWMutex | ||
} | ||
|
||
// NewCachingDocLoader creates a new instance of CachingDocumentLoader. | ||
func NewCachingDocLoader(nextLoader ld.DocumentLoader) *CachingDocumentLoader { | ||
cdl := &CachingDocumentLoader{ | ||
nextLoader: nextLoader, | ||
cache: map[string]interface{}{}, | ||
} | ||
|
||
return cdl | ||
} | ||
|
||
// LoadDocument returns a RemoteDocument containing the contents of the JSON resource from the given URL (u). | ||
func (cdl *CachingDocumentLoader) LoadDocument(u string) (*ld.RemoteDocument, error) { | ||
cdl.rwMutex.RLock() | ||
if doc, cached := cdl.cache[u]; cached { | ||
defer cdl.rwMutex.RUnlock() | ||
|
||
if cachedDoc, ok := doc.(*ld.RemoteDocument); ok { | ||
return cachedDoc, nil | ||
} | ||
|
||
return nil, errors.New("invalid document entry") | ||
} | ||
|
||
cdl.rwMutex.RUnlock() | ||
|
||
docFromLoader, err := cdl.nextLoader.LoadDocument(u) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cdl.rwMutex.Lock() | ||
if _, cached := cdl.cache[u]; !cached { | ||
cdl.cache[u] = docFromLoader | ||
} | ||
|
||
cdl.rwMutex.Unlock() | ||
|
||
return docFromLoader, nil | ||
} | ||
|
||
// AddDocument populates the cache with the given document (doc) for the provided URL (u). | ||
func (cdl *CachingDocumentLoader) AddDocument(u string, doc interface{}) { | ||
cdl.rwMutex.Lock() | ||
|
||
// add doc if u is not found in cache | ||
if _, cached := cdl.cache[u]; !cached { | ||
cdl.cache[u] = &ld.RemoteDocument{DocumentURL: u, Document: doc, ContextURL: ""} | ||
} | ||
|
||
cdl.rwMutex.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package jsonld | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/piprate/json-gold/ld" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewCachingDocLoader(t *testing.T) { | ||
u := "https://www.w3.org/2018/credentials/v1" | ||
loader := NewCachingDocLoader(ld.NewRFC7324CachingDocumentLoader(httpclient())) | ||
_, err := loader.LoadDocument(u) | ||
require.Error(t, err, "network should be disabled") | ||
|
||
loader.AddDocument(u, jsonVCWithProperContexts) | ||
|
||
expectedDoc := &ld.RemoteDocument{ | ||
DocumentURL: "https://www.w3.org/2018/credentials/v1", | ||
Document: jsonVCWithProperContexts, | ||
ContextURL: "", | ||
} | ||
|
||
doc, err := loader.LoadDocument(u) | ||
require.NoError(t, err) | ||
require.EqualValues(t, expectedDoc, doc) | ||
} | ||
|
||
// nolint | ||
const jsonVCWithProperContexts = `{ | ||
"@context": "https://w3id.org/security/v2", | ||
"id": "http://www.example.org/foo/documents/a3480d17-df7f-449f-9480-e2c35e20a865", | ||
"allowedAction": ["read", "write"], | ||
"invocationTarget": { | ||
"ID": "http://www.example.org/foo/documents/a3480d17-df7f-449f-9480-e2c35e20a865", | ||
"Type": "urn:edv:document" | ||
}, | ||
"proof": [{ | ||
"created": "2020-12-04T15:28:14.673975717-05:00", | ||
"jws": "eyJhbGciOiJFZERTQSIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..6OfIULug35ZmoU7lysChVpD6sjYfV71UwxqIZ8u0woYSIzRtzCo3MsZJw6cGIZMEaMssnQyRqIzo8B0yHEL2Dw", | ||
"nonce": "da7CcJahAdFG0GXN-JnS2f2mywcFNtaLyXtGVqku2DwVwUaJbGpUQjhlNi5kDbS4ZMi2cNhEN5ac6LponS-C9w", | ||
"proofPurpose": "capabilityDelegation", | ||
"type": "Ed25519Signature2018", | ||
"verificationMethod": "did:key:z6MkmkFTTczYKzU94t45sG65iZi2HA21tAU9ns8bXSmBEap4#z6MkmkFTTczYKzU94t45sG65iZi2HA21tAU9ns8bXSmBEap4" | ||
}] | ||
}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.