@@ -10,60 +10,31 @@ type Credentials struct {
1010 SecurityToken string
1111}
1212
13- const DEFAULT_EXPIRED_FACTOR = 0.8
14-
1513// Expirable credentials with an expiration.
16- type TempCredentials struct {
14+ type tempCredentials struct {
1715 Credentials
18- expiredFactor float64
19- expirationInMills int64 // The time when the credentials expires, unix timestamp in millis
20- lastUpdatedInMills int64
16+ Expiration time.Time // The time when the credentials expires, unix timestamp in millis
17+ LastUpdateTime time.Time
2118}
2219
23- func NewTempCredentials (accessKeyId , accessKeySecret , securityToken string ,
24- expirationInMills , lastUpdatedInMills int64 ) * TempCredentials {
20+ func newTempCredentials (accessKeyId , accessKeySecret , securityToken string ,
21+ expiration time. Time , lastUpdateTime time. Time ) * tempCredentials {
2522
26- return & TempCredentials {
23+ return & tempCredentials {
2724 Credentials : Credentials {
2825 AccessKeyID : accessKeyId ,
2926 AccessKeySecret : accessKeySecret ,
3027 SecurityToken : securityToken ,
3128 },
32- expirationInMills : expirationInMills ,
33- lastUpdatedInMills : lastUpdatedInMills ,
34- expiredFactor : DEFAULT_EXPIRED_FACTOR ,
35- }
36- }
37-
38- // @param factor must > 0.0 and <= 1.0, the less the factor is,
39- // the more frequently the credentials will be updated.
40- //
41- // If factor is set to 0, the credentials will be fetched every time
42- // [GetCredentials] is called.
43- //
44- // If factor is set to 1, the credentials will be fetched only when expired .
45- func (t * TempCredentials ) WithExpiredFactor (factor float64 ) * TempCredentials {
46- if factor > 0.0 && factor <= 1.0 {
47- t .expiredFactor = factor
29+ Expiration : expiration ,
30+ LastUpdateTime : lastUpdateTime ,
4831 }
49- return t
5032}
5133
52- // Returns true if credentials has expired already or will expire soon.
53- func (t * TempCredentials ) ShouldRefresh () bool {
54- nowInMills := time .Now ().UnixNano () / 1e6
55- if nowInMills >= t .expirationInMills {
56- return true
57- }
58- duration := (float64 )(t .expirationInMills - t .lastUpdatedInMills ) * t .expiredFactor
59- if duration < 0.0 { // check here
60- duration = 0
61- }
62- return (nowInMills - t .lastUpdatedInMills ) >= int64 (duration )
34+ func (t * tempCredentials ) isExpired () bool {
35+ return time .Now ().After (t .Expiration )
6336}
6437
65- // Returns true if credentials has expired already.
66- func (t * TempCredentials ) HasExpired () bool {
67- nowInMills := time .Now ().UnixNano () / 1e6
68- return nowInMills >= t .expirationInMills
38+ func (t * tempCredentials ) isValid () bool {
39+ return t .Credentials .AccessKeyID != "" && t .Credentials .AccessKeySecret != "" && ! t .Expiration .IsZero ()
6940}
0 commit comments