Skip to content

Commit

Permalink
Merge pull request #802 from 0xPolygonID/add_schema_cache_w3c_doc_loader
Browse files Browse the repository at this point in the history
chore: add schema cache to w3c doc loader
  • Loading branch information
martinsaporiti authored Oct 2, 2024
2 parents 6f16e0b + 050a530 commit 994ed04
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 18 deletions.
3 changes: 1 addition & 2 deletions cmd/notifications/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/pending_publisher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cmd/platform/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
7 changes: 1 addition & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/core/services/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion internal/jsonschema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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)

Expand Down
41 changes: 39 additions & 2 deletions internal/loader/w3c_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,58 @@ 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.RWMutex
data map[string]*ld.RemoteDocument
}

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

func (c *w3CDocumentLoaderLocalCache) Get(key string) (doc *ld.RemoteDocument, expireTime time.Time, err error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
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()
defer c.mutex.Unlock()
c.data[key] = doc
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),
}
}

Expand Down

0 comments on commit 994ed04

Please sign in to comment.