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

Cache locally whether issuers have been persisted or not already. #169

Merged
merged 15 commits into from
Aug 23, 2024
28 changes: 17 additions & 11 deletions personalities/sctfe/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"sync"

"github.com/google/certificate-transparency-go/x509"
tessera "github.com/transparency-dev/trillian-tessera"
Expand Down Expand Up @@ -94,33 +95,38 @@ func (cts *CTStorage) AddIssuerChain(ctx context.Context, chain []*x509.Certific
// Only up to N keys will be stored locally.
// TODO(phboneff): add monitoring for the number of keys
type cachedIssuerStorage struct {
m map[string]bool
sync.RWMutex
m map[string]struct{}
N int // maximum number of entries allowed in m
s IssuerStorage
}

// Exists checks if the key exists in the local cache, if not checks in the underlying storage.
// If it finds it there, caches the key locally.
func (c cachedIssuerStorage) Exists(ctx context.Context, key []byte) (bool, error) {
func (c *cachedIssuerStorage) Exists(ctx context.Context, key []byte) (bool, error) {
phbnf marked this conversation as resolved.
Show resolved Hide resolved
c.RLock()
_, ok := c.m[string(key)]
c.RUnlock()
if ok {
klog.V(2).Infof("Exists: found %q in local key cache", hex.EncodeToString(key))
klog.V(2).Infof("Exists: found %q in local key cache", key)
return true, nil
}
ok, err := c.s.Exists(ctx, key)
if err != nil {
return false, fmt.Errorf("error checking if issuer %q exists in the underlying IssuerStorage: %s", string(key), err)
return false, fmt.Errorf("error checking if issuer %q exists in the underlying IssuerStorage: %s", key, err)
}
if ok {
c.m[string(key)] = true
c.Lock()
c.m[string(key)] = struct{}{}
c.Unlock()
}
return ok, nil
}

// AddIssuers first adds the issuers to the underlying storage, then caches their sha256 locally.
//
// Only up to c.N issuer sha256 will be cached.
func (c cachedIssuerStorage) AddIssuers(ctx context.Context, kv []KV) error {
func (c *cachedIssuerStorage) AddIssuers(ctx context.Context, kv []KV) error {
req := []KV{}
for _, kv := range kv {
b, err := c.Exists(ctx, kv.K)
Expand All @@ -139,13 +145,13 @@ func (c cachedIssuerStorage) AddIssuers(ctx context.Context, kv []KV) error {
klog.V(2).Infof("Add: local issuer cache full, will stop caching issuers.")
return nil
}
c.m[string(kv.K)] = true
c.Lock()
c.m[string(kv.K)] = struct{}{}
c.Unlock()
}
return nil
}

func NewCachedIssuerStorage(s IssuerStorage) cachedIssuerStorage {
c := cachedIssuerStorage{s: s, N: maxCachedIssuerKeys}
c.m = make(map[string]bool)
return c
func NewCachedIssuerStorage(s IssuerStorage) *cachedIssuerStorage {
return &cachedIssuerStorage{s: s, N: maxCachedIssuerKeys, m: make(map[string]struct{})}
}
Loading