Skip to content

Commit

Permalink
Fix throttle decode value from redis (#666)
Browse files Browse the repository at this point in the history
* Fix decode value from redis

* Fix
  • Loading branch information
kirillov6 authored Aug 12, 2024
1 parent 42d0568 commit 11cbb41
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions plugin/action/throttle/redis_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (l *redisLimiter) updateLimiterValues(maxID, bucketIdx int, totalLimiterVal
}

func getLimitValFromJson(data []byte, valField string) (int64, error) {
var m map[string]json.Number
var m map[string]json.RawMessage
reader := bytes.NewReader(data)
if err := json.NewDecoder(reader).Decode(&m); err != nil {
return 0, fmt.Errorf("failed to unmarshal map: %w", err)
Expand All @@ -216,7 +216,7 @@ func getLimitValFromJson(data []byte, valField string) (int64, error) {
if !has {
return 0, fmt.Errorf("no %q key in map", valField)
}
return limitVal.Int64()
return json.Number(bytes.Trim(limitVal, `"`)).Int64()
}

// updateKeyLimit reads key limit from redis and updates current limit.
Expand Down
9 changes: 9 additions & 0 deletions plugin/action/throttle/redis_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,15 @@ func Test_getLimitValFromJson(t *testing.T) {
want: 3000,
wantErr: false,
},
{
name: "ok_with_object",
args: args{
data: []byte(`{"limit_key":"3000","some_obj":{"field":"key"}}`),
valField: "limit_key",
},
want: 3000,
wantErr: false,
},
{
name: "unmarshal_error",
args: args{
Expand Down

0 comments on commit 11cbb41

Please sign in to comment.