forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: client support credentialsProvider (#216)
* feat: client support credentialsProvider * fix build, remove 1.20 func usage * fix conflict * refine to code review * refine readme * use atomic, add ecs ram fetcher * add ram fetcher ut
- Loading branch information
1 parent
2c71d01
commit 386a0d0
Showing
15 changed files
with
753 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package sls | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type Credentials struct { | ||
AccessKeyID string | ||
AccessKeySecret string | ||
SecurityToken string | ||
} | ||
|
||
const DEFAULT_EXPIRED_FACTOR = 0.8 | ||
|
||
// Expirable credentials with an expiration. | ||
type TempCredentials struct { | ||
Credentials | ||
expiredFactor float64 | ||
expirationInMills int64 // The time when the credentials expires, unix timestamp in millis | ||
lastUpdatedInMills int64 | ||
} | ||
|
||
func NewTempCredentials(accessKeyId, accessKeySecret, securityToken string, | ||
expirationInMills, lastUpdatedInMills int64) *TempCredentials { | ||
|
||
return &TempCredentials{ | ||
Credentials: Credentials{ | ||
AccessKeyID: accessKeyId, | ||
AccessKeySecret: accessKeySecret, | ||
SecurityToken: securityToken, | ||
}, | ||
expirationInMills: expirationInMills, | ||
lastUpdatedInMills: lastUpdatedInMills, | ||
expiredFactor: DEFAULT_EXPIRED_FACTOR, | ||
} | ||
} | ||
|
||
// @param factor must > 0.0 and <= 1.0, the less the factor is, | ||
// the more frequently the credentials will be updated. | ||
// | ||
// If factor is set to 0, the credentials will be fetched every time | ||
// [GetCredentials] is called. | ||
// | ||
// If factor is set to 1, the credentials will be fetched only when expired . | ||
func (t *TempCredentials) WithExpiredFactor(factor float64) *TempCredentials { | ||
if factor > 0.0 && factor <= 1.0 { | ||
t.expiredFactor = factor | ||
} | ||
return t | ||
} | ||
|
||
// Returns true if credentials has expired already or will expire soon. | ||
func (t *TempCredentials) ShouldRefresh() bool { | ||
now := time.Now().UnixMilli() | ||
if now >= 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) | ||
} | ||
|
||
// Returns true if credentials has expired already. | ||
func (t *TempCredentials) HasExpired() bool { | ||
now := time.Now().UnixMilli() | ||
return now >= t.expirationInMills | ||
} |
Oops, something went wrong.