Skip to content

fix: panic for dragonfly 1.17 when interface convertation #99

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

Open
wants to merge 2 commits into
base: v10
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package redis_rate
import (
"context"
"fmt"
"reflect"
"strconv"
"time"

Expand Down Expand Up @@ -116,10 +117,20 @@ func (l Limiter) AllowN(
return nil, err
}

allowed, err := convertToInt(values[0])
if err != nil {
return nil, err
}

remaining, err := convertToInt(values[1])
if err != nil {
return nil, err
}

res := &Result{
Limit: limit,
Allowed: int(values[0].(int64)),
Remaining: int(values[1].(int64)),
Allowed: allowed,
Remaining: remaining,
RetryAfter: dur(retryAfter),
ResetAfter: dur(resetAfter),
}
Expand Down Expand Up @@ -152,10 +163,20 @@ func (l Limiter) AllowAtMost(
return nil, err
}

allowed, err := convertToInt(values[0])
if err != nil {
return nil, err
}

remaining, err := convertToInt(values[1])
if err != nil {
return nil, err
}

res := &Result{
Limit: limit,
Allowed: int(values[0].(int64)),
Remaining: int(values[1].(int64)),
Allowed: allowed,
Remaining: remaining,
RetryAfter: dur(retryAfter),
ResetAfter: dur(resetAfter),
}
Expand All @@ -174,6 +195,17 @@ func dur(f float64) time.Duration {
return time.Duration(f * float64(time.Second))
}

func convertToInt(value interface{}) (int, error) {
switch v := value.(type) {
case int64:
return int(v), nil
case float64:
return int(v), nil
default:
return 0, fmt.Errorf("value type is not match. Type: %s", reflect.TypeOf(value).Name())
}
}

type Result struct {
// Limit is the limit that was used to obtain this result.
Limit Limit
Expand Down