Skip to content

Commit

Permalink
Merge pull request #20 from projectdiscovery/bugfix-go-routine-leak
Browse files Browse the repository at this point in the history
Add Stop method
  • Loading branch information
Mzack9999 committed Dec 22, 2022
2 parents ac97489 + 9a8b2a8 commit aa416f1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion go.mod
Expand Up @@ -2,7 +2,10 @@ module github.com/projectdiscovery/ratelimit

go 1.18

require github.com/stretchr/testify v1.8.1
require (
github.com/stretchr/testify v1.8.1
golang.org/x/exp v0.0.0-20221217163422-3c43f8badb15
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -10,6 +10,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/exp v0.0.0-20221217163422-3c43f8badb15 h1:5oN1Pz/eDhCpbMbLstvIPa0b/BEQo6g6nwV3pLjfM6w=
golang.org/x/exp v0.0.0-20221217163422-3c43f8badb15/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
20 changes: 20 additions & 0 deletions keyratelimit.go
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"time"

"golang.org/x/exp/maps"
)

// Options of MultiLimiter
Expand Down Expand Up @@ -74,6 +76,24 @@ func (m *MultiLimiter) Take(key string) error {
return nil
}

// Stop internal limiters with defined keys or all if no key is provided
func (m *MultiLimiter) Stop(keys ...string) {
if len(keys) > 0 {
m.stopWithKeys(keys...)
} else {
m.stopWithKeys(maps.Keys(m.limiters)...)
}
}

// stopWithKeys stops the internal limiters matching keys
func (m *MultiLimiter) stopWithKeys(keys ...string) {
for _, key := range keys {
if limiter, ok := m.limiters[key]; ok {
limiter.Stop()
}
}
}

// SleepandReset stops timer removes all tokens and resets with new limit (used for Adaptive Ratelimiting)
func (m *MultiLimiter) SleepandReset(SleepTime time.Duration, opts *Options) error {
if err := opts.Validate(); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions ratelimit.go
Expand Up @@ -68,6 +68,13 @@ func (ratelimiter *Limiter) SleepandReset(sleepTime time.Duration, newLimit uint
go ratelimiter.run(ctx)
}

// Stop the rate limiter canceling the internal context
func (ratelimiter *Limiter) Stop() {
if ratelimiter.cancelFunc != nil {
ratelimiter.cancelFunc()
}
}

// New creates a new limiter instance with the tokens amount and the interval
func New(ctx context.Context, max uint, duration time.Duration) *Limiter {
internalctx, cancel := context.WithCancel(context.TODO())
Expand Down

0 comments on commit aa416f1

Please sign in to comment.