From afce62ec1d45b01a79b719129226124990e10222 Mon Sep 17 00:00:00 2001 From: crimson <1291463831@qq.com> Date: Fri, 1 Sep 2023 14:38:01 +0800 Subject: [PATCH] fix build on go 1.13 --- credentials.go | 10 +++++----- credentials_provider.go | 9 +++++---- credentials_test.go | 6 +++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/credentials.go b/credentials.go index bfbdc95f..1b2e5e76 100644 --- a/credentials.go +++ b/credentials.go @@ -51,19 +51,19 @@ func (t *TempCredentials) WithExpiredFactor(factor float64) *TempCredentials { // Returns true if credentials has expired already or will expire soon. func (t *TempCredentials) ShouldRefresh() bool { - now := time.Now().UnixMilli() - if now >= t.expirationInMills { + nowInMills := time.Now().UnixNano() / 1e6 + if nowInMills >= t.expirationInMills { return true } duration := (float64)(t.expirationInMills-t.lastUpdatedInMills) * t.expiredFactor if duration < 0.0 { // check here duration = 0 } - return (now - t.lastUpdatedInMills) >= int64(duration) + return (nowInMills - t.lastUpdatedInMills) >= int64(duration) } // Returns true if credentials has expired already. func (t *TempCredentials) HasExpired() bool { - now := time.Now().UnixMilli() - return now >= t.expirationInMills + nowInMills := time.Now().UnixNano() / 1e6 + return nowInMills >= t.expirationInMills } diff --git a/credentials_provider.go b/credentials_provider.go index 812a1d0f..bf4e6791 100644 --- a/credentials_provider.go +++ b/credentials_provider.go @@ -142,7 +142,7 @@ func updateFuncFetcher(updateFunc UpdateTokenFunction) CredentialsFetcher { return nil, fmt.Errorf("updateTokenFunc result not valid, expirationTime:%s", expireTime.Format(time.RFC3339)) } - return NewTempCredentials(id, secret, token, expireTime.UnixMilli(), -1), nil + return NewTempCredentials(id, secret, token, expireTime.UnixNano()/1e6, -1), nil } } @@ -168,7 +168,7 @@ func (adp *UpdateFuncProviderAdapter) GetCredentials() (Credentials, error) { adp.cred.Store(&res.Credentials) adp.expirationInMills.Store(res.expirationInMills) level.Debug(Logger).Log("reason", "updateTokenFunc fetch new credentials succeed", - "expirationTime", time.UnixMilli(res.expirationInMills).Format(CRED_TIME_FORMAT), + "expirationTime", time.Unix(res.expirationInMills/1e3, res.expirationInMills%1e3*1e6).Format(CRED_TIME_FORMAT), ) return res.Credentials, nil } @@ -181,11 +181,12 @@ func (adp *UpdateFuncProviderAdapter) shouldRefresh() bool { return true } now := time.Now() - return time.UnixMilli(adp.expirationInMills.Load()).Sub(now) <= adp.advanceDuration + millis := adp.expirationInMills.Load() + return time.Unix(millis/1e3, millis%1e3*1e6).Sub(now) <= adp.advanceDuration } func checkSTSTokenValid(accessKeyID, accessKeySecret, securityToken string, expirationTime time.Time) bool { - return accessKeyID != "" && accessKeySecret != "" && expirationTime.UnixMilli() > 0 + return accessKeyID != "" && accessKeySecret != "" && expirationTime.UnixNano() > 0 } const ECS_RAM_ROLE_URL_PREFIX = "http://100.100.100.200/latest/meta-data/ram/security-credentials/" diff --git a/credentials_test.go b/credentials_test.go index bddc66c2..b4c5cc84 100644 --- a/credentials_test.go +++ b/credentials_test.go @@ -4,7 +4,7 @@ import ( "bytes" "errors" "fmt" - io "io" + "io/ioutil" "net/http" "os" "testing" @@ -107,7 +107,7 @@ func TestBuilderParser(t *testing.T) { body := `` resp := http.Response{ - Body: io.NopCloser(bytes.NewBufferString(body)), + Body: ioutil.NopCloser(bytes.NewBufferString(body)), } _, err = ecsRamRoleParser(&resp) assert.Error(t, err) @@ -115,7 +115,7 @@ func TestBuilderParser(t *testing.T) { "SecurityToken": "zzzz", "Expiration": 234, "LastUpdated": 456 }` resp = http.Response{ - Body: io.NopCloser(bytes.NewBufferString(body)), + Body: ioutil.NopCloser(bytes.NewBufferString(body)), } cred, err := ecsRamRoleParser(&resp) assert.NoError(t, err)