Skip to content

Commit

Permalink
bonsai
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Dec 16, 2023
1 parent be4c5ee commit 881f0cc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions counter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ func DisableAutoDeleteExpired() Option {
}

// NewCounter creates a new Counter
func New(windowLen time.Duration, opts ...Option) *Counter {
func New(ttl time.Duration, opts ...Option) *Counter {
c := &Counter{}
for _, opt := range opts {
opt(c)
}
ttlOpts := []ttlcache.Option[string, *uint64]{
ttlcache.WithTTL[string, *uint64](windowLen * 2),
ttlcache.WithTTL[string, *uint64](ttl),
}
if c.capacity > 0 {
ttlOpts = append(ttlOpts, ttlcache.WithCapacity[string, *uint64](c.capacity))
Expand Down
10 changes: 5 additions & 5 deletions rl.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ type limiter struct {
}

func newLimiter(l Limiter) *limiter {
const defaultWindowLen = 1 * time.Hour
const defaultTTL = 1 * time.Hour
ll := &limiter{
Limiter: l,
}
if c, ok := l.(Counter); ok {
ll.Get = c.Get
ll.Increment = c.Increment
} else {
dl := defaultWindowLen
ttl := defaultTTL
r, err := ll.Rule(&http.Request{})
if err == nil {
dl = r.WindowLen
if err == nil && r.WindowLen > 0 {
ttl = r.WindowLen * 2
}
cc := counter.New(dl)
cc := counter.New(ttl)
ll.Get = cc.Get
ll.Increment = cc.Increment
}
Expand Down

0 comments on commit 881f0cc

Please sign in to comment.