Skip to content

Commit 4d5d20e

Browse files
csweichelFuristo
authored andcommitted
[registry-facade] Parallelise IPFS CID lookup
1 parent 07b3833 commit 4d5d20e

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

components/registry-facade/pkg/registry/blob.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ type blobHandler struct {
8585
Spec *api.ImageSpec
8686
Resolver remotes.Resolver
8787
Store content.Store
88-
IPFS *IPFSStore
88+
IPFS *IPFSBlobCache
8989
AdditionalSources []BlobSource
9090
ConfigModifier ConfigModifier
9191

components/registry-facade/pkg/registry/ipfs.go

+22-21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package registry
77
import (
88
"context"
99
"io"
10+
"sync"
1011
"time"
1112

1213
"github.com/gitpod-io/gitpod/common-go/log"
@@ -18,30 +19,42 @@ import (
1819
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
1920
)
2021

22+
// ipfsManifestModifier modifies a manifest and adds IPFS URLs to the layers
2123
func (reg *Registry) ipfsManifestModifier(mf *ociv1.Manifest) error {
2224
if reg.IPFS == nil {
2325
return nil
2426
}
2527

2628
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
2729
defer cancel()
30+
31+
var wg sync.WaitGroup
2832
for i, l := range mf.Layers {
29-
url, _ := reg.IPFS.Get(ctx, l.Digest)
30-
if url == "" {
31-
continue
32-
}
33-
mf.Layers[i].URLs = append(mf.Layers[i].URLs, url)
33+
wg.Add(1)
34+
go func(i int, dgst digest.Digest) {
35+
defer wg.Done()
36+
37+
url, _ := reg.IPFS.Get(ctx, dgst)
38+
if url == "" {
39+
return
40+
}
41+
mf.Layers[i].URLs = append(mf.Layers[i].URLs, url)
42+
}(i, l.Digest)
3443
}
44+
wg.Wait()
3545

3646
return nil
3747
}
3848

39-
type IPFSStore struct {
49+
// IPFSBlobCache can cache blobs in IPFS
50+
type IPFSBlobCache struct {
4051
Redis *redis.Client
4152
IPFS ipfs.CoreAPI
4253
}
4354

44-
func (store *IPFSStore) Get(ctx context.Context, dgst digest.Digest) (ipfsURL string, err error) {
55+
// Get retrieves the IPFS URL for a previously stored blob.
56+
// Returns an error if the blob is not stored in IPFS yet.
57+
func (store *IPFSBlobCache) Get(ctx context.Context, dgst digest.Digest) (ipfsURL string, err error) {
4558
if store == nil || store.IPFS == nil || store.Redis == nil {
4659
return "", nil
4760
}
@@ -54,20 +67,8 @@ func (store *IPFSStore) Get(ctx context.Context, dgst digest.Digest) (ipfsURL st
5467
return "ipfs://" + res, nil
5568
}
5669

57-
func (store *IPFSStore) Has(ctx context.Context, dgst digest.Digest) (ok bool, err error) {
58-
if store == nil || store.IPFS == nil || store.Redis == nil {
59-
return false, nil
60-
}
61-
62-
res := store.Redis.Exists(ctx, dgst.String())
63-
if err := res.Err(); err != nil {
64-
return false, err
65-
}
66-
67-
return res.Val() == 1, nil
68-
}
69-
70-
func (store *IPFSStore) Store(ctx context.Context, dgst digest.Digest, content io.Reader) (err error) {
70+
// Store stores a blob in IPFS. Will happily overwrite/re-upload a blob.
71+
func (store *IPFSBlobCache) Store(ctx context.Context, dgst digest.Digest, content io.Reader) (err error) {
7172
if store == nil || store.IPFS == nil || store.Redis == nil {
7273
return nil
7374
}

components/registry-facade/pkg/registry/registry.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type Registry struct {
7070
Config config.Config
7171
Resolver ResolverProvider
7272
Store content.Store
73-
IPFS *IPFSStore
73+
IPFS *IPFSBlobCache
7474
LayerSource LayerSource
7575
ConfigModifier ConfigModifier
7676
SpecProvider map[string]ImageSpecProvider
@@ -190,7 +190,7 @@ func NewRegistry(cfg config.Config, newResolver ResolverProvider, reg prometheus
190190
specProvider[api.ProviderPrefixFixed] = FixedImageSpecProvider(fp)
191191
}
192192

193-
var ipfs *IPFSStore
193+
var ipfs *IPFSBlobCache
194194
if cfg.IPFSCache != nil && cfg.IPFSCache.Enabled {
195195
addr := cfg.IPFSCache.IPFSAddr
196196
if ipfsHost := os.Getenv("IPFS_HOST"); ipfsHost != "" {
@@ -210,7 +210,7 @@ func NewRegistry(cfg config.Config, newResolver ResolverProvider, reg prometheus
210210
return nil, xerrors.Errorf("cannot connect to Redis: %w", err)
211211
}
212212

213-
ipfs = &IPFSStore{
213+
ipfs = &IPFSBlobCache{
214214
Redis: rdc,
215215
IPFS: core,
216216
}

0 commit comments

Comments
 (0)