| 1 | /* |
| 2 | Copyright The ORAS Authors. |
| 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | you may not use this file except in compliance with the License. |
| 5 | You may obtain a copy of the License at |
| 6 | |
| 7 | http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | |
| 9 | Unless required by applicable law or agreed to in writing, software |
| 10 | distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | See the License for the specific language governing permissions and |
| 13 | limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | package casscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/ |
| 17 | |
| 18 | import ( |
| 19 | "contextscip-go gomod github.com/golang/go/src go1.23.0 context/" |
| 20 | "ioscip-go gomod github.com/golang/go/src go1.23.0 io/" |
| 21 | "syncscip-go gomod github.com/golang/go/src go1.23.0 sync/" |
| 22 | |
| 23 | ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/ "github.com/opencontainers/image-spec/specs-go/v1scip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/" |
| 24 | "oras.land/oras-go/v2/contentscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/" |
| 25 | "oras.land/oras-go/v2/internal/ioutilscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/ioutil`/" |
| 26 | ) |
| 27 | |
| 28 | // Proxy is a caching proxy for the storage. |
| 29 | // The first fetch call of a described content will read from the remote and |
| 30 | // cache the fetched content. |
| 31 | // The subsequent fetch call will read from the local cache. |
| 32 | type Proxyscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy# struct { |
| 33 | contentscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/.ReadOnlyStoragescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#ReadOnlyStorage. |
| 34 | Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#Cache. contentscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/.Storagescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/Storage# |
| 35 | StopCachingscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#StopCaching. bool |
| 36 | } |
| 37 | |
| 38 | // NewProxy creates a proxy for the `base` storage, using the `cache` storage as |
| 39 | // the cache. |
| 40 | func NewProxyscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/NewProxy().(base contentscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/.ReadOnlyStoragescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/ReadOnlyStorage#, cache contentscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/.Storagescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/Storage#) *Proxyscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy# { |
| 41 | return &Proxyscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#{ |
| 42 | ReadOnlyStoragescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#ReadOnlyStorage.: base, |
| 43 | Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#Cache.: cache, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // NewProxyWithLimit creates a proxy for the `base` storage, using the `cache` |
| 48 | // storage with a push size limit as the cache. |
| 49 | func NewProxyWithLimitscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/NewProxyWithLimit().(base contentscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/.ReadOnlyStoragescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/ReadOnlyStorage#, cache contentscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/.Storagescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/Storage#, pushLimit int64) *Proxyscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy# { |
| 50 | limitedCache := contentscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/.LimitStoragescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/LimitStorage().(cache, pushLimit) |
| 51 | return &Proxyscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#{ |
| 52 | ReadOnlyStoragescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#ReadOnlyStorage.: base, |
| 53 | Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#Cache.: limitedCache, |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Fetch fetches the content identified by the descriptor. |
| 58 | func (p *Proxyscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#) Fetchscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#Fetch().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, target ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#) (ioscip-go gomod github.com/golang/go/src go1.23.0 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.23.0 io/ReadCloser#, error) { |
| 59 | if p.StopCachingscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#StopCaching. { |
| 60 | return p.FetchCachedscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#FetchCached().(ctx, target) |
| 61 | } |
| 62 | |
| 63 | rc, err := p.Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#Cache..Fetchscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/Fetcher#Fetch.(ctx, target) |
| 64 | if err == nil { |
| 65 | return rc, nil |
| 66 | } |
| 67 | |
| 68 | rc, err = p.ReadOnlyStoragescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#ReadOnlyStorage..Fetchscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/Fetcher#Fetch.(ctx, target) |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | pr, pw := ioscip-go gomod github.com/golang/go/src go1.23.0 io/.Pipescip-go gomod github.com/golang/go/src go1.23.0 io/Pipe().() |
| 73 | var wg syncscip-go gomod github.com/golang/go/src go1.23.0 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.23.0 sync/WaitGroup# |
| 74 | wg.Addscip-go gomod github.com/golang/go/src go1.23.0 sync/WaitGroup#Add().(1) |
| 75 | var pushErr error |
| 76 | go func() { |
| 77 | defer wg.Donescip-go gomod github.com/golang/go/src go1.23.0 sync/WaitGroup#Done().() |
| 78 | pushErr = p.Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#Cache..Pushscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/Pusher#Push.(ctx, target, pr) |
| 79 | if pushErr != nil { |
| 80 | pr.CloseWithErrorscip-go gomod github.com/golang/go/src go1.23.0 io/PipeReader#CloseWithError().(pushErr) |
| 81 | } |
| 82 | }() |
| 83 | closer := ioutilscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/ioutil`/.CloserFuncscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/ioutil`/CloserFunc#(func() error { |
| 84 | rcErr := rc.Closescip-go gomod github.com/golang/go/src go1.23.0 io/Closer#Close().() |
| 85 | if err := pw.Closescip-go gomod github.com/golang/go/src go1.23.0 io/PipeWriter#Close().(); err != nil { |
| 86 | return err |
| 87 | } |
| 88 | wg.Waitscip-go gomod github.com/golang/go/src go1.23.0 sync/WaitGroup#Wait().() |
| 89 | if pushErr != nil { |
| 90 | return pushErr |
| 91 | } |
| 92 | return rcErr |
| 93 | }) |
| 94 | |
| 95 | return struct { |
| 96 | ioscip-go gomod github.com/golang/go/src go1.23.0 io/.Readerscip-go gomod github.com/golang/go/src go1.23.0 io/Reader# |
| 97 | ioscip-go gomod github.com/golang/go/src go1.23.0 io/.Closerscip-go gomod github.com/golang/go/src go1.23.0 io/Closer# |
| 98 | }{ |
| 99 | Reader: ioscip-go gomod github.com/golang/go/src go1.23.0 io/.TeeReaderscip-go gomod github.com/golang/go/src go1.23.0 io/TeeReader().(rc, pw), |
| 100 | Closer: closer, |
| 101 | }, nil |
| 102 | } |
| 103 | |
| 104 | // FetchCached fetches the content identified by the descriptor. |
| 105 | // If the content is not cached, it will be fetched from the remote without |
| 106 | // caching. |
| 107 | func (p *Proxyscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#) FetchCachedscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#FetchCached().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, target ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#) (ioscip-go gomod github.com/golang/go/src go1.23.0 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.23.0 io/ReadCloser#, error) { |
| 108 | exists, err := p.Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#Cache..Existsscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/ReadOnlyStorage#Exists.(ctx, target) |
| 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | if exists { |
| 113 | return p.Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#Cache..Fetchscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/Fetcher#Fetch.(ctx, target) |
| 114 | } |
| 115 | return p.ReadOnlyStoragescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#ReadOnlyStorage..Fetchscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/Fetcher#Fetch.(ctx, target) |
| 116 | } |
| 117 | |
| 118 | // Exists returns true if the described content exists. |
| 119 | func (p *Proxyscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#) Existsscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#Exists().(ctx contextscip-go gomod github.com/golang/go/src go1.23.0 context/.Contextscip-go gomod github.com/golang/go/src go1.23.0 context/Context#, target ocispecscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/.Descriptorscip-go gomod github.com/opencontainers/image-spec v1.1.1 `github.com/opencontainers/image-spec/specs-go/v1`/Descriptor#) (bool, error) { |
| 120 | exists, err := p.Cachescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#Cache..Existsscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/ReadOnlyStorage#Exists.(ctx, target) |
| 121 | if err == nil && exists { |
| 122 | return true, nil |
| 123 | } |
| 124 | return p.ReadOnlyStoragescip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/internal/cas`/Proxy#ReadOnlyStorage..Existsscip-go gomod oras.land/oras-go/v2 . `oras.land/oras-go/v2/content`/ReadOnlyStorage#Exists.(ctx, target) |
| 125 | } |