@@ -62,9 +62,20 @@ pub enum RegistryOperation {
62
62
// Types to allow better naming
63
63
type Registry = String ;
64
64
type Repository = String ;
65
- type TokenCacheKey = ( Registry , Repository , RegistryOperation ) ;
65
+
66
+ #[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
67
+ struct TokenCacheKey {
68
+ registry : Registry ,
69
+ repository : Repository ,
70
+ operation : RegistryOperation ,
71
+ }
72
+
66
73
type TokenExpiration = u64 ;
67
- type TokenCacheValue = ( RegistryTokenType , TokenExpiration ) ;
74
+
75
+ struct TokenCacheValue {
76
+ token : RegistryTokenType ,
77
+ expiration : TokenExpiration ,
78
+ }
68
79
69
80
// (registry, repository, scope) -> (token, expiration)
70
81
type CacheType = BTreeMap < TokenCacheKey , TokenCacheValue > ;
@@ -121,10 +132,14 @@ impl TokenCache {
121
132
let registry = reference. resolve_registry ( ) . to_string ( ) ;
122
133
let repository = reference. repository ( ) . to_string ( ) ;
123
134
debug ! ( %registry, %repository, ?op, %expiration, "Inserting token" ) ;
124
- self . tokens
125
- . write ( )
126
- . await
127
- . insert ( ( registry, repository, op) , ( token, expiration) ) ;
135
+ self . tokens . write ( ) . await . insert (
136
+ TokenCacheKey {
137
+ registry,
138
+ repository,
139
+ operation : op,
140
+ } ,
141
+ TokenCacheValue { token, expiration } ,
142
+ ) ;
128
143
}
129
144
130
145
pub ( crate ) async fn get (
@@ -134,28 +149,31 @@ impl TokenCache {
134
149
) -> Option < RegistryTokenType > {
135
150
let registry = reference. resolve_registry ( ) . to_string ( ) ;
136
151
let repository = reference. repository ( ) . to_string ( ) ;
137
- match self
138
- . tokens
139
- . read ( )
140
- . await
141
- . get ( & ( registry. clone ( ) , repository. clone ( ) , op) )
142
- {
143
- Some ( ( ref token, expiration) ) => {
152
+ let key = TokenCacheKey {
153
+ registry,
154
+ repository,
155
+ operation : op,
156
+ } ;
157
+ match self . tokens . read ( ) . await . get ( & key) {
158
+ Some ( TokenCacheValue {
159
+ ref token,
160
+ expiration,
161
+ } ) => {
144
162
let now = SystemTime :: now ( ) ;
145
163
let epoch = now
146
164
. duration_since ( UNIX_EPOCH )
147
165
. expect ( "Time went backwards" )
148
166
. as_secs ( ) ;
149
167
if epoch > * expiration {
150
- debug ! ( %registry , %repository , ?op , %expiration, miss=false , expired=true , "Fetching token" ) ;
168
+ debug ! ( ?key , %expiration, miss=false , expired=true , "Fetching token" ) ;
151
169
None
152
170
} else {
153
- debug ! ( %registry , %repository , ?op , %expiration, miss=false , expired=false , "Fetching token" ) ;
171
+ debug ! ( ?key , %expiration, miss=false , expired=false , "Fetching token" ) ;
154
172
Some ( token. clone ( ) )
155
173
}
156
174
}
157
175
None => {
158
- debug ! ( %registry , %repository , ?op , miss= true , "Fetching token" ) ;
176
+ debug ! ( ?key , miss = true , "Fetching token" ) ;
159
177
None
160
178
}
161
179
}
0 commit comments