From 1827765a4d54b46fcf652d32296b99c846b3c692 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 20 Apr 2014 15:08:24 -0400 Subject: [PATCH] Fixes #56 again because I did it wrong. --- bot/bot.go | 17 ++++++++++++++--- main.go | 4 ---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 14da59c..700501b 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -3,18 +3,21 @@ package bot import ( - "code.google.com/p/velour/irc" - "github.com/chrissexton/alepale/config" "html/template" - "labix.org/v2/mgo" "log" "net/http" "strings" "time" + + "code.google.com/p/velour/irc" + "github.com/chrissexton/alepale/config" + "labix.org/v2/mgo" ) const actionPrefix = "\x01ACTION" +var throttle <-chan time.Time + // Bot type provides storage for bot-wide information, configs, and database connections type Bot struct { // Each plugin must be registered in our plugins handler. To come: a map so that this @@ -167,6 +170,14 @@ func (b *Bot) SendMessage(channel, message string) { } else { message = "" } + + if throttle == nil { + ratePerSec := b.Config.RatePerSec + throttle = time.Tick(time.Second / time.Duration(ratePerSec)) + } + + <-throttle + b.Client.Out <- m } } diff --git a/main.go b/main.go index 6ca5b2f..4139c5d 100644 --- a/main.go +++ b/main.go @@ -91,9 +91,6 @@ func handleConnection() { } }() - ratePerSec := Config.RatePerSec - throttle := time.Tick(time.Second * time.Duration(1.0/ratePerSec)) - for { select { case msg, ok := <-Client.In: @@ -105,7 +102,6 @@ func handleConnection() { handleMsg(msg) case <-t.C: - <-throttle // rate limit our Service.Method RPCs Client.Out <- irc.Msg{Cmd: irc.PING, Args: []string{Client.Server}} t = time.NewTimer(pingTime)