diff options
author | Kevin Chabowski <kevin@kch42.de> | 2014-03-27 15:17:37 +0100 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2014-03-27 23:51:08 +0100 |
commit | ccdba88b73eae78984e31831079ff8798c2ddd59 (patch) | |
tree | b3a40da8b6d55dc173b05bb4b98e7a3ef205fdbd /floodstop.go | |
parent | addb75b981a2044a47adb7ff26850d6ff12b6144 (diff) | |
download | simplechat-ccdba88b73eae78984e31831079ff8798c2ddd59.tar.gz simplechat-ccdba88b73eae78984e31831079ff8798c2ddd59.tar.bz2 simplechat-ccdba88b73eae78984e31831079ff8798c2ddd59.zip |
Prevent users from flooding the server
Diffstat (limited to 'floodstop.go')
-rw-r--r-- | floodstop.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/floodstop.go b/floodstop.go new file mode 100644 index 0000000..ad4af0f --- /dev/null +++ b/floodstop.go @@ -0,0 +1,47 @@ +package main + +import ( + "time" +) + +type Floodstop struct { + ask chan chan bool + stop chan struct{} +} + +func NewFloodstop(reset time.Duration, countMax int) (fs *Floodstop) { + fs = &Floodstop{ + ask: make(chan chan bool), + stop: make(chan struct{}), + } + + ticker := time.NewTicker(reset) + counter := 0 + + go func() { + defer ticker.Stop() + for { + select { + case <-fs.stop: + return + case retCh := <-fs.ask: + counter++ + retCh <- (counter < countMax) + case <-ticker.C: + counter = 0 + } + } + }() + + return +} + +func (fs *Floodstop) Stop() { + fs.stop <- struct{}{} +} + +func (fs *Floodstop) Ask() bool { + ch := make(chan bool) + fs.ask <- ch + return <-ch +} |