| 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 |
||
| 140 | func (provider *ExternalCredentialsProvider) GetCredentials() (cc *Credentials, err error) { |
||
| 141 | // 先检查是否需要更新(使用读锁) |
||
| 142 | provider.mu.RLock() |
||
| 143 | needUpdate := provider.sessionCredentials == nil || |
||
| 144 | provider.expirationTimestamp == 0 || |
||
| 145 | provider.expirationTimestamp-time.Now().Unix() <= 180 |
||
| 146 | provider.mu.RUnlock() |
||
| 147 | |||
| 148 | if needUpdate { |
||
| 149 | // 获取新凭证(在锁外执行,避免阻塞其他 goroutine) |
||
| 150 | sessionCredentials, err1 := provider.getCredentials() |
||
| 151 | if err1 != nil { |
||
| 152 | return nil, err1 |
||
| 153 | } |
||
| 154 | |||
| 155 | // 使用写锁更新共享状态 |
||
| 156 | provider.mu.Lock() |
||
| 157 | // 双重检查,避免多个 goroutine 同时更新 |
||
| 158 | if provider.sessionCredentials == nil || |
||
| 159 | provider.expirationTimestamp == 0 || |
||
| 160 | provider.expirationTimestamp-time.Now().Unix() <= 180 { |
||
| 161 | provider.sessionCredentials = sessionCredentials |
||
| 162 | |||
| 163 | // 如果返回了过期时间,解析并缓存 |
||
| 164 | if sessionCredentials.Expiration != "" { |
||
| 165 | expirationTime, err2 := time.Parse("2006-01-02T15:04:05Z", sessionCredentials.Expiration) |
||
| 166 | if err2 != nil { |
||
| 167 | // 如果解析失败,不设置过期时间,下次调用时重新获取 |
||
| 168 | provider.expirationTimestamp = 0 |
||
| 169 | } else { |
||
| 170 | provider.lastUpdateTimestamp = time.Now().Unix() |
||
| 171 | provider.expirationTimestamp = expirationTime.Unix() |
||
| 172 | } |
||
| 173 | } else { |
||
| 174 | // 没有过期时间,下次调用时重新获取 |
||
| 175 | provider.expirationTimestamp = 0 |
||
| 176 | } |
||
| 177 | } |
||
| 178 | expirationTimestamp := provider.expirationTimestamp |
||
| 179 | sessionCredentials = provider.sessionCredentials |
||
| 180 | provider.mu.Unlock() |
||
| 181 | |||
| 182 | // 如果设置了回调函数,则调用回调函数写回配置文件(在锁外执行) |
||
| 183 | if provider.credentialUpdateCallback != nil { |
||
| 184 | err1 := provider.credentialUpdateCallback( |
||
| 185 | sessionCredentials.AccessKeyId, |
||
| 186 | sessionCredentials.AccessKeySecret, |
||
| 187 | sessionCredentials.SecurityToken, |
||
| 188 | expirationTimestamp, |
||
| 189 | ) |
||
| 190 | if err1 != nil { |
||
| 191 | fmt.Printf("Warning: failed to update external credentials in config file: %v\n", err1) |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | // 使用读锁读取凭证 |
||
| 197 | provider.mu.RLock() |
||
| 198 | cc = &Credentials{ |
||
| 199 | AccessKeyId: provider.sessionCredentials.AccessKeyId, |
||
| 200 | AccessKeySecret: provider.sessionCredentials.AccessKeySecret, |
||
| 201 | SecurityToken: provider.sessionCredentials.SecurityToken, |
||
| 202 | ProviderName: provider.GetProviderName(), |
||
| 203 | } |
||
| 204 | provider.mu.RUnlock() |
||
| 205 | return |
||
| 206 | } |
||
| 211 |