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",
"FullName": "Ale Pale",
"CommandChar": "!",
"RatePerSec": 10.0,
"QuoteChance": 0.99,
"QuoteTime": 1,
"LogLength": 50,

View File

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

13
main.go
View File

@ -3,15 +3,16 @@
package main
import (
"code.google.com/p/velour/irc"
"flag"
"github.com/chrissexton/alepale/bot"
"github.com/chrissexton/alepale/config"
"github.com/chrissexton/alepale/plugins"
"io"
"log"
"os"
"time"
"code.google.com/p/velour/irc"
"github.com/chrissexton/alepale/bot"
"github.com/chrissexton/alepale/config"
"github.com/chrissexton/alepale/plugins"
)
const (
@ -90,6 +91,9 @@ func handleConnection() {
}
}()
ratePerSec := Config.RatePerSec
throttle := time.Tick(time.Second * time.Duration(1.0/ratePerSec))
for {
select {
case msg, ok := <-Client.In:
@ -101,6 +105,7 @@ 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)