@@ -116,8 +116,9 @@ type dockerClient struct {
116116 challenges []challenge
117117 supportsSignatures bool
118118
119- // Private state for setupRequestAuth (key: string, value: bearerToken)
120- tokenCache sync.Map
119+ // Private state for setupRequestAuth
120+ tokenCacheLock sync.Mutex // Protects tokenCache.
121+ tokenCache map [string ]bearerToken
121122 // Private state for detectProperties:
122123 detectPropertiesOnce sync.Once // detectPropertiesOnce is used to execute detectProperties() at most once.
123124 detectPropertiesError error // detectPropertiesError caches the initial error.
@@ -274,6 +275,7 @@ func newDockerClient(sys *types.SystemContext, registry, reference string) (*doc
274275 registry : registry ,
275276 userAgent : userAgent ,
276277 tlsClientConfig : tlsClientConfig ,
278+ tokenCache : map [string ]bearerToken {},
277279 reportedWarnings : set .New [string ](),
278280 }, nil
279281}
@@ -755,10 +757,12 @@ func (c *dockerClient) obtainBearerToken(ctx context.Context, challenge challeng
755757 }
756758
757759 var token bearerToken
758- t , inCache := c .tokenCache .Load (cacheKey )
759- if inCache {
760- token = t .(bearerToken )
761- }
760+ var inCache bool
761+ func () { // A scope for defer
762+ c .tokenCacheLock .Lock ()
763+ defer c .tokenCacheLock .Unlock ()
764+ token , inCache = c .tokenCache [cacheKey ]
765+ }()
762766 if ! inCache || time .Now ().After (token .expirationTime ) {
763767 var (
764768 t * bearerToken
@@ -774,7 +778,11 @@ func (c *dockerClient) obtainBearerToken(ctx context.Context, challenge challeng
774778 }
775779
776780 token = * t
777- c .tokenCache .Store (cacheKey , token )
781+ func () { // A scope for defer
782+ c .tokenCacheLock .Lock ()
783+ defer c .tokenCacheLock .Unlock ()
784+ c .tokenCache [cacheKey ] = token
785+ }()
778786 }
779787 return token .token , nil
780788}
0 commit comments