credential_updater.go 628 B

12345678910111213141516171819202122232425
  1. package credentials
  2. import (
  3. "net/http"
  4. "time"
  5. )
  6. const defaultInAdvanceScale = 0.95
  7. var hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
  8. return fn
  9. }
  10. type credentialUpdater struct {
  11. credentialExpiration int
  12. lastUpdateTimestamp int64
  13. inAdvanceScale float64
  14. }
  15. func (updater *credentialUpdater) needUpdateCredential() (result bool) {
  16. if updater.inAdvanceScale == 0 {
  17. updater.inAdvanceScale = defaultInAdvanceScale
  18. }
  19. return time.Now().Unix()-updater.lastUpdateTimestamp >= int64(float64(updater.credentialExpiration)*updater.inAdvanceScale)
  20. }