Skip to content

Commit

Permalink
Fix issues in limiterFilter
Browse files Browse the repository at this point in the history
Signed-off-by: aliwoto <aminnimaj@gmail.com>
  • Loading branch information
ALiwoto committed Nov 9, 2021
1 parent 9fc189d commit 4477665
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
4 changes: 4 additions & 0 deletions ratelimiter/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func (l *Limiter) limiterFilter(msg *gotgbot.Message) bool {
return false
}

if l.isIgnoredException(msg) {
return true
}

if l.isException(msg) {
return false
}
Expand Down
62 changes: 60 additions & 2 deletions ratelimiter/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ func (l *Limiter) AddCustomIgnore(id int64, d time.Duration, ignoreExceptions bo
}
l.userMap[id] = status
l.mutex.Unlock()
if ignoreExceptions {
l.addIgnoredExceptions(id)
}
return
}
l.mutex.Unlock()
Expand All @@ -301,15 +304,22 @@ func (l *Limiter) AddCustomIgnore(id int64, d time.Duration, ignoreExceptions bo
duration: d,
ignoreException: ignoreExceptions,
}
if ignoreExceptions {
l.addIgnoredExceptions(id)
}
}

func (l *Limiter) RemoveCustomIgnore(id int64) {
l.mutex.Lock()
status := l.userMap[id]
l.mutex.Unlock()
if status == nil {
if status == nil || status.custom == nil {
return
}

if status.custom.ignoreException {
l.removeFromIgnoredExceptions(id)
}
status.custom = nil
}

Expand All @@ -334,7 +344,7 @@ func (l *Limiter) runTriggers(b *gotgbot.Bot, ctx *ext.Context) {
}
}

// isException will check and see if msg can be ignore because
// isException will check and see if msg can be ignored because
// it's id is in the exception list or not. This method's usage
// is internal-only.
func (l *Limiter) isException(msg *gotgbot.Message) bool {
Expand All @@ -358,6 +368,54 @@ func (l *Limiter) isException(msg *gotgbot.Message) bool {
return false
}

// isIgnoredException will check and see if msg cannot be ignored because
// it's id is in the exception list or not. This method's usage
// is internal-only.
func (l *Limiter) isIgnoredException(msg *gotgbot.Message) bool {
if len(l.ignoredExceptions) == 0 {
return false
}

for _, ex := range l.ignoredExceptions {
if msg.From != nil {
if ex == msg.From.Id || ex == msg.Chat.Id {
return true
}
} else {
if ex == msg.Chat.Id {
return true
}
}
}

return false
}

func (l *Limiter) addIgnoredExceptions(id int64) {
if len(l.ignoredExceptions) == 0 {
l.ignoredExceptions = append(l.ignoredExceptions, id)
return
}
for _, ex := range l.ignoredExceptions {
if ex == id {
return
}
}
l.ignoredExceptions = append(l.ignoredExceptions, id)
}

func (l *Limiter) removeFromIgnoredExceptions(id int64) {
if len(l.ignoredExceptions) == 0 {
return
}
for i, ex := range l.ignoredExceptions {
if ex == id {
l.ignoredExceptions = append(l.ignoredExceptions[:i], l.ignoredExceptions[i+1:]...)
return
}
}
}

// checker should be run in a new goroutine as it blocks its goroutine
// with a for-loop. This method's duty is to clear the old user's status
// from the cache using `l.maxTimeout` parameter.
Expand Down
7 changes: 4 additions & 3 deletions ratelimiter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ type Limiter struct {
// it should remain private.
msgHandler *handlers.Message

exceptions []filters.Message
conditions []filters.Message
exceptionIDs []int64
exceptions []filters.Message
conditions []filters.Message
exceptionIDs []int64
ignoredExceptions []int64

// timeout is the floodwait checking time. a user is allowed to
// send `maxCount` messages per `timeout`.
Expand Down

0 comments on commit 4477665

Please sign in to comment.