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

chore: add schema cache to w3c doc loader #802

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Changes from 1 commit
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
Next Next commit
chore: add schema cache to w3c doc loader
martinsaporiti committed Oct 1, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 94a4e82446035e5b8e79e1400350b293d0105563
3 changes: 1 addition & 2 deletions cmd/notifications/main.go
Original file line number Diff line number Diff line change
@@ -144,8 +144,7 @@ func newCredentialsService(ctx context.Context, cfg *config.Configuration, stora

rhsFactory := reverse_hash.NewFactory(*networkResolver, reverse_hash.DefaultRHSTimeOut)
revocationStatusResolver := revocation_status.NewRevocationStatusResolver(*networkResolver)
// TODO: Cache only if cfg.APIUI.SchemaCache == true
schemaLoader := loader.NewDocumentLoader(cfg.IPFS.GatewayURL)
schemaLoader := loader.NewDocumentLoader(cfg.IPFS.GatewayURL, cfg.SchemaCache)

mtService := services.NewIdentityMerkleTrees(mtRepository)
qrService := services.NewQrStoreService(cachex)
2 changes: 1 addition & 1 deletion cmd/pending_publisher/main.go
Original file line number Diff line number Diff line change
@@ -71,7 +71,7 @@ func main() {
}(storage)

// TODO: Cache only if cfg.APIUI.SchemaCache == true
schemaLoader := loader.NewDocumentLoader(cfg.IPFS.GatewayURL)
schemaLoader := loader.NewDocumentLoader(cfg.IPFS.GatewayURL, cfg.SchemaCache)

vaultCfg := providers.Config{
UserPassAuthEnabled: cfg.KeyStore.VaultUserPassAuthEnabled,
2 changes: 1 addition & 1 deletion cmd/platform/main.go
Original file line number Diff line number Diff line change
@@ -71,7 +71,7 @@ func main() {
}

// TODO: Cache only if cfg.APIUI.SchemaCache == true
schemaLoader := loader.NewDocumentLoader(cfg.IPFS.GatewayURL)
schemaLoader := loader.NewDocumentLoader(cfg.IPFS.GatewayURL, cfg.SchemaCache)

vaultCfg := providers.Config{
UserPassAuthEnabled: cfg.KeyStore.VaultUserPassAuthEnabled,
2 changes: 1 addition & 1 deletion internal/api/main_test.go
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ func TestMain(m *testing.M) {
cfg.ServerUrl = "https://testing.env"
cfg.Ethereum = cfgForTesting.Ethereum
cfg.UniversalLinks = config.UniversalLinks{BaseUrl: "https://testing.env"}
schemaLoader = loader.NewDocumentLoader(ipfsGatewayURL)
schemaLoader = loader.NewDocumentLoader(ipfsGatewayURL, false)
m.Run()
}

7 changes: 1 addition & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ type Configuration struct {
ServerUrl string `env:"ISSUER_SERVER_URL" envDefault:"http://localhost"`
ServerPort int `env:"ISSUER_SERVER_PORT" envDefault:"3001"`
PublishingKeyPath string `env:"ISSUER_PUBLISH_KEY_PATH" envDefault:"pbkey"`
SchemaCache *bool `env:"ISSUER_SCHEMA_CACHE" envDefault:"false"`
SchemaCache bool `env:"ISSUER_SCHEMA_CACHE" envDefault:"false"`
OnChainCheckStatusFrequency time.Duration `env:"ISSUER_ONCHAIN_CHECK_STATUS_FREQUENCY"`
NetworkResolverPath string `env:"ISSUER_RESOLVER_PATH"`
NetworkResolverFile *string `env:"ISSUER_RESOLVER_FILE"`
@@ -310,11 +310,6 @@ func checkEnvVars(ctx context.Context, cfg *Configuration) error {
return errors.New("ISSUER_CACHE_URL value is missing")
}

if cfg.SchemaCache == nil {
log.Info(ctx, "ISSUER_SCHEMA_CACHE is missing and the server set up it as false")
cfg.SchemaCache = common.ToPointer(false)
}

if cfg.MediaTypeManager.Enabled == nil {
log.Info(ctx, "ISSUER_MEDIA_TYPE_MANAGER_ENABLED is missing and the server set up it as true")
cfg.MediaTypeManager.Enabled = common.ToPointer(true)
2 changes: 1 addition & 1 deletion internal/core/services/main_test.go
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ func TestMain(m *testing.M) {

cachex = cache.NewMemoryCache()

docLoader = loader.NewDocumentLoader(ipfsGatewayURL)
docLoader = loader.NewDocumentLoader(ipfsGatewayURL, false)
cfg.Ethereum = cfgForTesting.Ethereum
cfg.ServerUrl = "http://localhost:3001"
m.Run()
2 changes: 1 addition & 1 deletion internal/jsonschema/schema_test.go
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import (
func TestValidateCredentialSubject(t *testing.T) {
const ipfsGatewayURL = "http://127.0.0.1:8080"
ctx := context.Background()
ld := loader.NewDocumentLoader(ipfsGatewayURL)
ld := loader.NewDocumentLoader(ipfsGatewayURL, false)

type config struct {
name string
4 changes: 2 additions & 2 deletions internal/loader/loader.go
Original file line number Diff line number Diff line change
@@ -17,6 +17,6 @@ type Loader interface {
type Factory func(url string) Loader

// NewDocumentLoader returns a new ld.DocumentLoader that will use http ipfs gateway to download documents
func NewDocumentLoader(ipfsGateway string) ld.DocumentLoader {
return NewW3CDocumentLoader(nil, ipfsGateway)
func NewDocumentLoader(ipfsGateway string, withCache bool) ld.DocumentLoader {
return NewW3CDocumentLoader(nil, ipfsGateway, withCache)
}
2 changes: 1 addition & 1 deletion internal/loader/w3CDocumentLoader_test.go
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ import (
)

func TestW3CDocumentLoader_LoadDocument(t *testing.T) {
w3cLoader := NewW3CDocumentLoader(nil, "https://ipfs.io")
w3cLoader := NewW3CDocumentLoader(nil, "https://ipfs.io", false)
doc, err := w3cLoader.LoadDocument(W3CCredential2018ContextURL)
require.NoError(t, err)

39 changes: 37 additions & 2 deletions internal/loader/w3c_loader.go
Original file line number Diff line number Diff line change
@@ -2,21 +2,56 @@ package loader

import (
"strings"
"sync"
"time"

"github.com/iden3/go-schema-processor/v2/loaders"
shell "github.com/ipfs/go-ipfs-api"
"github.com/piprate/json-gold/ld"
)

const defaultSchemaCacheDuration = 30 * time.Minute

// W3CDocumentLoader is a document loader for w3c documents
type W3CDocumentLoader struct {
l ld.DocumentLoader
}

type w3CDocumentLoaderLocalCache struct {
mutex sync.Mutex
data map[string]*ld.RemoteDocument
}

func newLocalCache() *w3CDocumentLoaderLocalCache {
return &w3CDocumentLoaderLocalCache{
mutex: sync.Mutex{},
data: make(map[string]*ld.RemoteDocument),
}
}

func (c *w3CDocumentLoaderLocalCache) Get(key string) (doc *ld.RemoteDocument, expireTime time.Time, err error) {
doc, ok := c.data[key]
if !ok {
return nil, time.Time{}, nil
}
return doc, time.Now().Add(defaultSchemaCacheDuration), nil
}

func (c *w3CDocumentLoaderLocalCache) Set(key string, doc *ld.RemoteDocument, expireTime time.Time) error {
c.mutex.Lock()
c.data[key] = doc
c.mutex.Unlock()
return nil
}

// NewW3CDocumentLoader creates a new document loader with a predefined http schema
func NewW3CDocumentLoader(_ *shell.Shell, ipfsGW string) ld.DocumentLoader {
func NewW3CDocumentLoader(_ *shell.Shell, ipfsGW string, withCache bool) ld.DocumentLoader {
if !withCache {
return loaders.NewDocumentLoader(nil, ipfsGW)
}
option := loaders.WithCacheEngine(newLocalCache())
return &W3CDocumentLoader{
l: loaders.NewDocumentLoader(nil, ipfsGW),
l: loaders.NewDocumentLoader(nil, ipfsGW, option),
}
}