Skip to content

Commit bde4884

Browse files
committed
put the initialization in NewRepository instead and remove the mutex
On-behalf-of: Gergely Brautigam <gergely.brautigam@sap.com> Signed-off-by: Gergely Brautigam <182850+Skarlso@users.noreply.github.com>
1 parent 325c73a commit bde4884

1 file changed

Lines changed: 13 additions & 39 deletions

File tree

api/oci/extensions/repositories/ocireg/repository.go

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net/http"
77
"path"
88
"strings"
9-
"sync"
109
"time"
1110

1211
"github.com/containerd/errdefs"
@@ -54,7 +53,6 @@ type RepositoryImpl struct {
5453
spec *RepositorySpec
5554
info *RepositoryInfo
5655

57-
mu sync.Mutex
5856
transport *http.Transport
5957
timeout *time.Duration
6058
lock *locker.Locker
@@ -71,11 +69,18 @@ func NewRepository(ctx cpi.Context, spec *RepositorySpec, info *RepositoryInfo)
7169
if urs.Scheme == "http" {
7270
logger.Warn("using insecure http for oci registry {{host}}", "host", urs.Host)
7371
}
72+
transport, timeout, err := configureTransport(ctx, info.Scheme)
73+
if err != nil {
74+
return nil, err
75+
}
7476
i := &RepositoryImpl{
7577
RepositoryImplBase: cpi.NewRepositoryImplBase(ctx),
7678
logger: logger,
7779
spec: spec,
7880
info: info,
81+
transport: transport,
82+
timeout: timeout,
83+
lock: locker.New(),
7984
}
8085
i.logger.Debug("created repository")
8186
return cpi.NewRepository(i), nil
@@ -94,13 +99,7 @@ func (r *RepositoryImpl) GetSpecification() cpi.RepositorySpec {
9499
}
95100

96101
func (r *RepositoryImpl) Close() error {
97-
r.mu.Lock()
98-
defer r.mu.Unlock()
99-
100-
if r.transport != nil {
101-
r.transport.CloseIdleConnections()
102-
r.transport = nil
103-
}
102+
r.transport.CloseIdleConnections()
104103

105104
return nil
106105
}
@@ -131,26 +130,6 @@ func (r *RepositoryImpl) getCreds(comp string) (credentials.Credentials, error)
131130
return identity.GetCredentials(r.GetContext(), r.info.Locator, comp)
132131
}
133132

134-
func (r *RepositoryImpl) getOrCreateTransport() (*http.Transport, *time.Duration, error) {
135-
r.mu.Lock()
136-
defer r.mu.Unlock()
137-
138-
if r.transport == nil {
139-
transport, timeout, err := configureTransport(r.GetContext(), r.info.Scheme)
140-
if err != nil {
141-
return nil, nil, err
142-
}
143-
r.transport = transport
144-
r.timeout = timeout
145-
}
146-
147-
if r.lock == nil {
148-
r.lock = locker.New()
149-
}
150-
151-
return r.transport, r.timeout, nil
152-
}
153-
154133
func (r *RepositoryImpl) getResolver(comp string) (oras.Resolver, error) {
155134
creds, err := r.getCreds(comp)
156135
if err != nil {
@@ -188,25 +167,20 @@ func (r *RepositoryImpl) getResolver(comp string) (oras.Resolver, error) {
188167
}
189168
}
190169

191-
transport, timeout, err := r.getOrCreateTransport()
192-
if err != nil {
193-
return nil, err
194-
}
195-
196-
if creds != nil && transport.TLSClientConfig != nil {
170+
if creds != nil && r.transport.TLSClientConfig != nil {
197171
c := creds.GetProperty(credentials.ATTR_CERTIFICATE_AUTHORITY)
198172
if c != "" {
199-
transport.TLSClientConfig.RootCAs.AppendCertsFromPEM([]byte(c))
173+
r.transport.TLSClientConfig.RootCAs.AppendCertsFromPEM([]byte(c))
200174
}
201175
}
202176

203-
retryTransport := retry.NewTransport(transport)
177+
retryTransport := retry.NewTransport(r.transport)
204178

205179
client := &http.Client{
206180
Transport: ocmlog.NewRoundTripper(retryTransport, logger),
207181
}
208-
if timeout != nil {
209-
client.Timeout = *timeout
182+
if r.timeout != nil {
183+
client.Timeout = *r.timeout
210184
}
211185

212186
authClient := &auth.Client{

0 commit comments

Comments
 (0)