Skip to content

Commit

Permalink
change after @shamil cr
Browse files Browse the repository at this point in the history
  • Loading branch information
Shareed2k committed Apr 1, 2021
1 parent bffdfdd commit a724a3c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 104 deletions.
64 changes: 0 additions & 64 deletions algorithm/gcra/gcra_lua.go

This file was deleted.

15 changes: 1 addition & 14 deletions algorithm/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package algorithm

import (
"context"
"fmt"
"time"

"github.com/go-redis/redis/v8"
)

var (
Registry []*RegInfo
)
var Registry []*RegInfo

type (
Limit interface {
Expand Down Expand Up @@ -79,13 +76,3 @@ type (
func Register(info *RegInfo) {
Registry = append(Registry, info)
}

func Find(name string) (*RegInfo, error) {
for _, item := range Registry {
if item.Name == name {
return item, nil
}
}

return nil, fmt.Errorf("didn't find algorithm called %q", name)
}
14 changes: 6 additions & 8 deletions limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package go_redis_ratelimit

import "time"

type (
Limit struct {
Algorithm string
Burst int64
Rate int64
Period time.Duration
}
)
type Limit struct {
Algorithm string
Burst int64
Rate int64
Period time.Duration
}

func (l *Limit) GetAlgorithm() string {
return l.Algorithm
Expand Down
29 changes: 11 additions & 18 deletions limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,31 @@ import (
"github.com/go-redis/redis/v8"

"github.com/bringg/go_redis_ratelimit/algorithm"

_ "github.com/bringg/go_redis_ratelimit/algorithm/all"
)

const (
DefaultPrefix = "limiter"
)

var (
algorithmPool map[string]algorithm.Algorithm
)
const DefaultPrefix = "limiter"

type (
// Limiter controls how frequently events are allowed to happen.
Limiter struct {
Prefix string
}
)
// Limiter controls how frequently events are allowed to happen.
type Limiter struct {
Prefix string
algorithms map[string]algorithm.Algorithm
}

// NewLimiter returns a new Limiter.
func NewLimiter(rdb *redis.Client) (*Limiter, error) {
algorithmPool = make(map[string]algorithm.Algorithm, len(algorithm.Registry))
algorithms := make(map[string]algorithm.Algorithm, len(algorithm.Registry))

var err error
for _, info := range algorithm.Registry {
if algorithmPool[info.Name], err = info.NewAlgorithm(rdb); err != nil {
if algorithms[info.Name], err = info.NewAlgorithm(rdb); err != nil {
return nil, err
}
}

return &Limiter{
Prefix: DefaultPrefix,
Prefix: DefaultPrefix,
algorithms: algorithms,
}, nil
}

Expand All @@ -54,7 +47,7 @@ func (l *Limiter) Allow(key string, limit *Limit) (*algorithm.Result, error) {
}

func (l *Limiter) findAlgorithm(name string) (algorithm.Algorithm, error) {
if algo, ok := algorithmPool[name]; ok {
if algo, ok := l.algorithms[name]; ok {
return algo, nil
}

Expand Down

0 comments on commit a724a3c

Please sign in to comment.