Skip to content

Commit

Permalink
rename func for building a limit
Browse files Browse the repository at this point in the history
  • Loading branch information
da440dil committed Jan 2, 2022
1 parent 38fd085 commit b59167d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 42 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ func main() {
err := client.Del(ctx, key).Err()
requireNoError(err)

// Create limiter with 2 limiters.
ls := counter.NewLimiter(
// Create limiter with 2 limits.
limiter := counter.NewLimiter(
client,
// First limiter is limited to 3 calls per second.
counter.WithLimiter(time.Second, 3),
// Second limiter is limited to 5 calls per 2 seconds.
counter.WithLimiter(time.Second*2, 5),
// First limit: no more than 3 limiter calls within 1 second.
counter.WithLimit(time.Second, 3),
// Second limit: no more than 5 limiter calls within 2 seconds.
counter.WithLimit(time.Second*2, 5),
)

limit := func() {
r, err := ls.Limit(ctx, key)
r, err := limiter.Limit(ctx, key)
requireNoError(err)
fmt.Printf(
"Result: { ok: %v, counter: %v, remainder: %v, ttl: %v }\n",
Expand Down
20 changes: 10 additions & 10 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,25 @@ func BenchmarkLimiter(b *testing.B) {
tests := map[string]Limiter{
"One": NewLimiter(
client,
WithLimiter(size, limit),
WithLimit(size, limit),
),
"Two": NewLimiter(
client,
WithLimiter(size, limit),
WithLimiter(size*2, limit*2),
WithLimit(size, limit),
WithLimit(size*2, limit*2),
),
"Three": NewLimiter(
client,
WithLimiter(size, limit),
WithLimiter(size*2, limit*2),
WithLimiter(size*3, limit*3),
WithLimit(size, limit),
WithLimit(size*2, limit*2),
WithLimit(size*3, limit*3),
),
"Four": NewLimiter(
client,
WithLimiter(size, limit),
WithLimiter(size*2, limit*2),
WithLimiter(size*3, limit*3),
WithLimiter(size*4, limit*4),
WithLimit(size, limit),
WithLimit(size*2, limit*2),
WithLimit(size*3, limit*3),
WithLimit(size*4, limit*4),
),
}

Expand Down
2 changes: 1 addition & 1 deletion counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Counter struct {
size int
}

// Count increments key by value.
// Count increments key value by specified value.
func (c *Counter) Count(ctx context.Context, key string, value int) (Result, error) {
r := Result{}
res, err := c.script.Run(ctx, c.client, []string{key}, value, c.size, c.limit).Result()
Expand Down
14 changes: 7 additions & 7 deletions examples/limiter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ func main() {
err := client.Del(ctx, key).Err()
requireNoError(err)

// Create limiter with 2 limiters.
ls := counter.NewLimiter(
// Create limiter with 2 limits.
limiter := counter.NewLimiter(
client,
// First limiter is limited to 3 calls per second.
counter.WithLimiter(time.Second, 3),
// Second limiter is limited to 5 calls per 2 seconds.
counter.WithLimiter(time.Second*2, 5),
// First limit: no more than 3 limiter calls within 1 second.
counter.WithLimit(time.Second, 3),
// Second limit: no more than 5 limiter calls within 2 seconds.
counter.WithLimit(time.Second*2, 5),
)

limit := func() {
r, err := ls.Limit(ctx, key)
r, err := limiter.Limit(ctx, key)
requireNoError(err)
fmt.Printf(
"Result: { ok: %v, counter: %v, remainder: %v, ttl: %v }\n",
Expand Down
22 changes: 11 additions & 11 deletions limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func init() {
random = rand.New(rand.NewSource(time.Now().UnixNano()))
}

// Limiter implements distributed rate limiting. Contains one or more distributed counters.
// Limiter implements distributed rate limiting.
type Limiter interface {
// Limit applies the limit: increments key value of each distributed counter.
// Limit applies the limit.
Limit(ctx context.Context, key string) (Result, error)
}

Expand All @@ -35,12 +35,12 @@ const (
algSliding = 2
)

// WithLimiter creates params to build limiter.
// WithLimit creates parameters to build a limit.
//
// Each limiter uses fixed window algorithm by default, may be set with options.
// Each limiter is created with pseudo-random name which may be set with options, every Redis key will be prefixed with this name.
// The rate of decreasing the window size on each next limiter call by default equal 1, may be set with options.
func WithLimiter(size time.Duration, limit uint, options ...func(*params)) *params {
// By default a limit uses fixed window algorithm, may be set with options.
// Each limit is created with pseudo-random name which may be set with options.
// The rate of decreasing the window size on each next application of the limit by default equal 1, may be set with options.
func WithLimit(size time.Duration, limit uint, options ...func(*params)) *params {
p := &params{alg: algFixed, size: int(size / time.Millisecond), limit: int64(limit)}
for _, opt := range options {
opt(p)
Expand All @@ -54,28 +54,28 @@ func WithLimiter(size time.Duration, limit uint, options ...func(*params)) *para
return p
}

// WithFixedWindow sets limiter algorithm to fixed window.
// WithFixedWindow sets fixed window algorithm for the limit.
func WithFixedWindow() func(*params) {
return func(p *params) {
p.alg = algFixed
}
}

// WithSlidingWindow sets limiter algorithm to sliding window.
// WithSlidingWindow sets sliding window algorithm for the limit.
func WithSlidingWindow() func(*params) {
return func(p *params) {
p.alg = algSliding
}
}

// WithName sets unique limiter name.
// WithName sets unique name for the limit, every Redis key will be prefixed with this name.
func WithName(name string) func(*params) {
return func(p *params) {
p.prefix = name + ":"
}
}

// WithRate sets limiter rate of decreasing the window size on each next limiter call.
// WithRate sets the rate of decreasing the window size on each next application of the limit.
func WithRate(rate uint) func(*params) {
return func(p *params) {
p.rate = int(rate)
Expand Down
12 changes: 6 additions & 6 deletions limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ func TestNewLimiter(t *testing.T) {
sizev := int(size / time.Millisecond)
limitv := int64(limit)

v1 := NewLimiter(clientMock, WithLimiter(size, limit, WithName("x")))
v1 := NewLimiter(clientMock, WithLimit(size, limit, WithName("x")))
require.Equal(t, &limiter{counter: &Counter{client: clientMock, script: fwscr, size: sizev, limit: limitv}, prefix: "x:", rate: 1}, v1)

v2 := NewLimiter(clientMock, WithLimiter(size, limit, WithName("x"), WithFixedWindow()))
v2 := NewLimiter(clientMock, WithLimit(size, limit, WithName("x"), WithFixedWindow()))
require.Equal(t, &limiter{counter: &Counter{client: clientMock, script: fwscr, size: sizev, limit: limitv}, prefix: "x:", rate: 1}, v2)

v3 := NewLimiter(clientMock, WithLimiter(size, limit, WithName("x"), WithSlidingWindow()))
v3 := NewLimiter(clientMock, WithLimit(size, limit, WithName("x"), WithSlidingWindow()))
require.Equal(t, &limiter{counter: &Counter{client: clientMock, script: swscr, size: sizev, limit: limitv}, prefix: "x:", rate: 1}, v3)

v4 := NewLimiter(clientMock, WithLimiter(size, limit, WithName("x"), WithRate(2)))
v4 := NewLimiter(clientMock, WithLimit(size, limit, WithName("x"), WithRate(2)))
require.Equal(t, &limiter{counter: &Counter{client: clientMock, script: fwscr, size: sizev, limit: limitv}, prefix: "x:", rate: 2}, v4)

v5 := NewLimiter(clientMock, WithLimiter(size, limit, WithName("x")), WithLimiter(size, limit, WithName("y")))
v5 := NewLimiter(clientMock, WithLimit(size, limit, WithName("x")), WithLimit(size, limit, WithName("y")))
require.Equal(t, &batchlimiter{client: clientMock, prefixes: []string{"x:", "y:"}, args: []interface{}{1, sizev, limitv, algFixed, 1, sizev, limitv, algFixed}}, v5)

rnd := random
Expand All @@ -39,7 +39,7 @@ func TestNewLimiter(t *testing.T) {
random = rnd
}()

v6 := NewLimiter(clientMock, WithLimiter(size, limit))
v6 := NewLimiter(clientMock, WithLimit(size, limit))
require.Equal(t, &limiter{counter: &Counter{client: clientMock, script: fwscr, size: sizev, limit: limitv}, prefix: "3440579354231278675:", rate: 1}, v6)
}

Expand Down

0 comments on commit b59167d

Please sign in to comment.