forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: client support credentialsProvider #216
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
437c3ff
feat: client support credentialsProvider
crimson-gao 9e0e2a4
fix build, remove 1.20 func usage
crimson-gao f2d2e8a
Merge branch 'master' into credentials-provider
crimson-gao d4d3f1a
fix conflict
crimson-gao c7cebf5
refine to code review
crimson-gao 80b414c
refine readme
crimson-gao 08143f7
use atomic, add ecs ram fetcher
crimson-gao 41311d0
add ram fetcher ut
crimson-gao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
即使 < 0 也没关系,一样会return true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里主要是担心客户端时间不严格有序