From 9952154433df03423855c99240324153b58d5e4a Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 20 Apr 2014 12:54:01 -0400 Subject: [PATCH] Fixes #56: Add rate limit to outgoing messages --- config.json | 1 + config/config.go | 1 + main.go | 13 +++++++++---- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/config.json b/config.json index 2890f7f..75188fa 100644 --- a/config.json +++ b/config.json @@ -9,6 +9,7 @@ "Pass": "AlePaleTest:test", "FullName": "Ale Pale", "CommandChar": "!", + "RatePerSec": 10.0, "QuoteChance": 0.99, "QuoteTime": 1, "LogLength": 50, diff --git a/config/config.go b/config/config.go index ce62557..7fc044b 100644 --- a/config/config.go +++ b/config/config.go @@ -18,6 +18,7 @@ type Config struct { FullName string Version string CommandChar string + RatePerSec float64 QuoteChance float64 QuoteTime int LogLength int diff --git a/main.go b/main.go index 32d25df..6ca5b2f 100644 --- a/main.go +++ b/main.go @@ -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)