Skip to content

Commit 38bf5ff

Browse files
committed
[registry-facade] Parallelise IPFS CID lookup
1 parent 91c3f12 commit 38bf5ff

File tree

1 file changed

+22
-21
lines changed
  • components/registry-facade/pkg/registry

1 file changed

+22
-21
lines changed

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

Lines changed: 22 additions & 21 deletions
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
}

0 commit comments

Comments
 (0)