| Conditions | 12 |
| Total Lines | 66 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like providers.*ExternalCredentialsProvider.GetCredentials often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package providers |
||
| 183 | func (provider *ExternalCredentialsProvider) GetCredentials() (cc *Credentials, err error) { |
||
| 184 | // 先检查是否需要更新(使用读锁) |
||
| 185 | provider.mu.RLock() |
||
| 186 | needUpdate := provider.sessionCredentials == nil || |
||
| 187 | provider.expirationTimestamp == 0 || |
||
| 188 | provider.expirationTimestamp-time.Now().Unix() <= 180 |
||
| 189 | provider.mu.RUnlock() |
||
| 190 | |||
| 191 | if needUpdate { |
||
| 192 | // 获取新凭证(在锁外执行,避免阻塞其他 goroutine) |
||
| 193 | sessionCredentials, err1 := provider.getCredentials() |
||
| 194 | if err1 != nil { |
||
| 195 | return nil, err1 |
||
| 196 | } |
||
| 197 | |||
| 198 | // 使用写锁更新共享状态 |
||
| 199 | provider.mu.Lock() |
||
| 200 | // 双重检查,避免多个 goroutine 同时更新 |
||
| 201 | if provider.sessionCredentials == nil || |
||
| 202 | provider.expirationTimestamp == 0 || |
||
| 203 | provider.expirationTimestamp-time.Now().Unix() <= 180 { |
||
| 204 | provider.sessionCredentials = sessionCredentials |
||
| 205 | |||
| 206 | // 如果返回了过期时间,解析并缓存 |
||
| 207 | if sessionCredentials.Expiration != "" { |
||
| 208 | expirationTime, err2 := time.Parse("2006-01-02T15:04:05Z", sessionCredentials.Expiration) |
||
| 209 | if err2 != nil { |
||
| 210 | // 如果解析失败,不设置过期时间,下次调用时重新获取 |
||
| 211 | provider.expirationTimestamp = 0 |
||
| 212 | } else { |
||
| 213 | provider.lastUpdateTimestamp = time.Now().Unix() |
||
| 214 | provider.expirationTimestamp = expirationTime.Unix() |
||
| 215 | } |
||
| 216 | } else { |
||
| 217 | // 没有过期时间,下次调用时重新获取 |
||
| 218 | provider.expirationTimestamp = 0 |
||
| 219 | } |
||
| 220 | } |
||
| 221 | expirationTimestamp := provider.expirationTimestamp |
||
| 222 | sessionCredentials = provider.sessionCredentials |
||
| 223 | provider.mu.Unlock() |
||
| 224 | |||
| 225 | // 如果设置了回调函数,则调用回调函数写回配置文件(在锁外执行) |
||
| 226 | if provider.credentialUpdateCallback != nil { |
||
| 227 | err1 := provider.credentialUpdateCallback( |
||
| 228 | sessionCredentials.AccessKeyId, |
||
| 229 | sessionCredentials.AccessKeySecret, |
||
| 230 | sessionCredentials.SecurityToken, |
||
| 231 | expirationTimestamp, |
||
| 232 | ) |
||
| 233 | if err1 != nil { |
||
| 234 | fmt.Printf("Warning: failed to update external credentials in config file: %v\n", err1) |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | // 使用读锁读取凭证 |
||
| 240 | provider.mu.RLock() |
||
| 241 | cc = &Credentials{ |
||
| 242 | AccessKeyId: provider.sessionCredentials.AccessKeyId, |
||
| 243 | AccessKeySecret: provider.sessionCredentials.AccessKeySecret, |
||
| 244 | SecurityToken: provider.sessionCredentials.SecurityToken, |
||
| 245 | ProviderName: provider.GetProviderName(), |
||
| 246 | } |
||
| 247 | provider.mu.RUnlock() |
||
| 248 | return |
||
| 249 | } |
||
| 254 |