Fixes #56: Add rate limit to outgoing messages

This commit is contained in:
Chris Sexton 2014-04-20 12:54:01 -04:00
parent b9d4ce273f
commit 9952154433
3 changed files with 11 additions and 4 deletions

View File

@ -9,6 +9,7 @@
"Pass": "AlePaleTest:test", "Pass": "AlePaleTest:test",
"FullName": "Ale Pale", "FullName": "Ale Pale",
"CommandChar": "!", "CommandChar": "!",
"RatePerSec": 10.0,
"QuoteChance": 0.99, "QuoteChance": 0.99,
"QuoteTime": 1, "QuoteTime": 1,
"LogLength": 50, "LogLength": 50,

View File

@ -18,6 +18,7 @@ type Config struct {
FullName string FullName string
Version string Version string
CommandChar string CommandChar string
RatePerSec float64
QuoteChance float64 QuoteChance float64
QuoteTime int QuoteTime int
LogLength int LogLength int

13
main.go
View File

@ -3,15 +3,16 @@
package main package main
import ( import (
"code.google.com/p/velour/irc"
"flag" "flag"
"github.com/chrissexton/alepale/bot"
"github.com/chrissexton/alepale/config"
"github.com/chrissexton/alepale/plugins"
"io" "io"
"log" "log"
"os" "os"
"time" "time"
"code.google.com/p/velour/irc"
"github.com/chrissexton/alepale/bot"
"github.com/chrissexton/alepale/config"
"github.com/chrissexton/alepale/plugins"
) )
const ( const (
@ -90,6 +91,9 @@ func handleConnection() {
} }
}() }()
ratePerSec := Config.RatePerSec
throttle := time.Tick(time.Second * time.Duration(1.0/ratePerSec))
for { for {
select { select {
case msg, ok := <-Client.In: case msg, ok := <-Client.In:
@ -101,6 +105,7 @@ func handleConnection() {
handleMsg(msg) handleMsg(msg)
case <-t.C: case <-t.C:
<-throttle // rate limit our Service.Method RPCs
Client.Out <- irc.Msg{Cmd: irc.PING, Args: []string{Client.Server}} Client.Out <- irc.Msg{Cmd: irc.PING, Args: []string{Client.Server}}
t = time.NewTimer(pingTime) t = time.NewTimer(pingTime)