Skip to content

Commit 52862a3

Browse files
yndu13peze
authored andcommitted
feat: support compute throttling time left
1 parent cb71c13 commit 52862a3

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

service/service.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,40 @@ func shaHmac1(source, secret string) []byte {
633633
hmac.Write([]byte(source))
634634
return hmac.Sum(nil)
635635
}
636+
637+
func getTimeLeft(rateLimit *string) (_result *int64) {
638+
if rateLimit != nil {
639+
pairs := strings.Split(tea.StringValue(rateLimit), ",")
640+
for _, pair := range pairs {
641+
kv := strings.Split(pair, ":")
642+
if len(kv) == 2 {
643+
key, value := kv[0], kv[1]
644+
if key == "TimeLeft" {
645+
timeLeftValue, err := strconv.ParseInt(value, 10, 64)
646+
if err != nil {
647+
return nil
648+
}
649+
return tea.Int64(timeLeftValue)
650+
}
651+
}
652+
}
653+
}
654+
return nil
655+
}
656+
657+
/**
658+
* Get throttling param
659+
* @param the response headers
660+
* @return time left
661+
*/
662+
func GetThrottlingTimeLeft(headers map[string]*string) (_result *int64) {
663+
rateLimitForUserApi := headers["X-RateLimit-User-API"]
664+
rateLimitForUser := headers["X-RateLimit-User"]
665+
timeLeftForUserApi := getTimeLeft(rateLimitForUserApi)
666+
timeLeftForUser := getTimeLeft(rateLimitForUser)
667+
if tea.Int64Value(timeLeftForUserApi) > tea.Int64Value(timeLeftForUser) {
668+
return timeLeftForUserApi
669+
} else {
670+
return timeLeftForUser
671+
}
672+
}

service/service_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,40 @@ func Test_SignatureMethod(t *testing.T) {
255255
res = SignatureMethod(priKey, "source", "ACS3-RSA-SHA256")
256256
utils.AssertEqual(t, "a00b88ae04f651a8ab645e724949ff435bbb2cf9a37aa54323024477f8031f4e13dc948484c5c5a81ba53a55eb0571dffccc1e953c93269d6da23ed319e0f1ef699bcc9823a646574628ae1b70ed569b5a07d139dda28996b5b9231f5ba96141f0893deec2fbf54a0fa2c203b8ae74dd26f457ac29c873745a5b88273d2b3d12", tea.StringValue(HexEncode(res)))
257257
}
258+
259+
func Test_GetThrottlingTimeLeft(t *testing.T) {
260+
headers := map[string]*string{
261+
"X-RateLimit-User-API": nil,
262+
"X-RateLimit-User": nil,
263+
}
264+
timeLeft := GetThrottlingTimeLeft(headers)
265+
utils.AssertNil(t, timeLeft)
266+
267+
headers = map[string]*string{
268+
"X-RateLimit-User-API": nil,
269+
"X-RateLimit-User": tea.String("Limit:1,Remain:0,TimeLeft:2000,Reset:1234"),
270+
}
271+
timeLeft = GetThrottlingTimeLeft(headers)
272+
utils.AssertEqual(t, int64(2000), tea.Int64Value(timeLeft))
273+
274+
headers = map[string]*string{
275+
"X-RateLimit-User-API": tea.String("Limit:1,Remain:0,TimeLeft:2000,Reset:1234"),
276+
"X-RateLimit-User": nil,
277+
}
278+
timeLeft = GetThrottlingTimeLeft(headers)
279+
utils.AssertEqual(t, int64(2000), tea.Int64Value(timeLeft))
280+
281+
headers = map[string]*string{
282+
"X-RateLimit-User-API": tea.String("Limit:1,Remain:0,TimeLeft:2000,Reset:1234"),
283+
"X-RateLimit-User": tea.String("Limit:1,Remain:0,TimeLeft:0,Reset:1234"),
284+
}
285+
timeLeft = GetThrottlingTimeLeft(headers)
286+
utils.AssertEqual(t, int64(2000), tea.Int64Value(timeLeft))
287+
288+
headers = map[string]*string{
289+
"X-RateLimit-User-API": tea.String("Limit:1,Remain:0,TimeLeft:0,Reset:1234"),
290+
"X-RateLimit-User": tea.String("Limit:1,Remain:0,TimeLeft:0,Reset:1234"),
291+
}
292+
timeLeft = GetThrottlingTimeLeft(headers)
293+
utils.AssertEqual(t, int64(0), tea.Int64Value(timeLeft))
294+
}

0 commit comments

Comments
 (0)