// 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 ( "context" "time" "github.com/tcolgate/hugot" ) 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 }