switch from shaky ignores to a refractory period on commands

This commit is contained in:
2016-07-05 03:54:02 +01:00
parent 3b4a55bd93
commit 38982bf2c9
3 changed files with 62 additions and 114 deletions

View File

@@ -0,0 +1,55 @@
// package deadline is a Hugot handler that aims to reduce channel noise on a
// wrapped handler. Upon invocation, a deadline is set. Further invocations will
// be ignored until after the deadline has passed.
package deadline
import (
"time"
"github.com/tcolgate/hugot"
"golang.org/x/net/context"
)
type HearsDeadline struct {
hugot.HearsHandler
Deadline time.Time
}
type CommandDeadline struct {
hugot.CommandHandler
Deadline time.Time
}
func NewCommand(next hugot.CommandHandler) hugot.CommandHandler {
return &CommandDeadline{CommandHandler: next, Deadline: time.Now().UTC()}
}
func NewHears(next hugot.HearsHandler) hugot.HearsHandler {
return &HearsDeadline{HearsHandler: next, Deadline: time.Now().UTC()}
}
func (i *HearsDeadline) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, matches [][]string) {
now := time.Now().UTC()
if now.After(i.Deadline) {
i.Deadline = now.Add(2 * time.Minute)
i.HearsHandler.Heard(ctx, w, m, matches)
return
}
// punish channel noise harder if people start spamming
// i.Deadline = i.Deadline.Add(1 * time.Minute)
}
func (i *CommandDeadline) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
now := time.Now().UTC()
if now.After(i.Deadline) {
i.Deadline = now.Add(2 * time.Minute)
return i.CommandHandler.Command(ctx, w, m)
}
// punish channel noise harder if people start spamming
// i.Deadline = i.Deadline.Add(1 * time.Minute)
return nil
}