Skip to content

Commit 4849dec

Browse files
committed
fix race?
Signed-off-by: Lixia (Sylvia) Lei <lixlei@microsoft.com>
1 parent 451df3f commit 4849dec

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

internal/maps/maps.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 maps
17+
18+
// Copy copies all key/value pairs in src adding them to dst.
19+
// When a key in src is already present in dst,
20+
// the value in dst will be overwritten by the value associated
21+
// with the key in src.
22+
//
23+
// Reference: https://pkg.go.dev/maps@go1.21.1#Copy
24+
func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) {
25+
for k, v := range src {
26+
dst[k] = v
27+
}
28+
}

registry/remote/auth/scope.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"sort"
2121
"strings"
2222

23+
"oras.land/oras-go/v2/internal/maps"
2324
"oras.land/oras-go/v2/internal/slices"
2425
"oras.land/oras-go/v2/registry"
2526
)
@@ -146,15 +147,16 @@ type scopesPerHostContextKey struct{}
146147
//
147148
// Reference: https://docs.docker.com/registry/spec/auth/scope/
148149
func WithScopesPerHost(ctx context.Context, host string, scopes ...string) context.Context {
149-
var regMap map[string][]string
150-
var ok bool
151-
regMap, ok = ctx.Value(scopesPerHostContextKey{}).(map[string][]string)
152-
if !ok {
153-
regMap = make(map[string][]string)
150+
var scopesByHost map[string][]string
151+
if old, ok := ctx.Value(scopesPerHostContextKey{}).(map[string][]string); ok {
152+
scopesByHost = make(map[string][]string, len(old))
153+
maps.Copy(scopesByHost, old)
154+
} else {
155+
scopesByHost = make(map[string][]string, 1)
154156
}
155-
scopes = CleanScopes(scopes)
156-
regMap[host] = scopes
157-
return context.WithValue(ctx, scopesPerHostContextKey{}, regMap)
157+
158+
scopesByHost[host] = CleanScopes(scopes)
159+
return context.WithValue(ctx, scopesPerHostContextKey{}, scopesByHost)
158160
}
159161

160162
// AppendScopesPerHost appends additional scopes to the existing scopes
@@ -171,8 +173,8 @@ func AppendScopesPerHost(ctx context.Context, host string, scopes ...string) con
171173

172174
// GetScopesPerHost returns the scopes in the context for the given host.
173175
func GetScopesPerHost(ctx context.Context, host string) []string {
174-
if regMap, ok := ctx.Value(scopesPerHostContextKey{}).(map[string][]string); ok {
175-
return slices.Clone(regMap[host])
176+
if scopesByHost, ok := ctx.Value(scopesPerHostContextKey{}).(map[string][]string); ok {
177+
return slices.Clone(scopesByHost[host])
176178
}
177179
return nil
178180
}

0 commit comments

Comments
 (0)