summaryrefslogtreecommitdiff
path: root/rate.go
blob: a4b630e2c8def4eb9419661d1e898dff77320310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main

import (
	"sync"
	"time"
)

// TODO rate limiting

type WatchList struct {
	sync.Mutex
	n    int
	d    time.Duration
	list map[string]int
}

func NewWatchList(n int, d time.Duration) WatchList {
	return WatchList{n: n, d: d, list: make(map[string]int)}
}

func (w WatchList) Add(kick chan string, nick string) {
	w.Lock()
	defer w.Unlock()
	if _, ok := w.list[nick]; !ok {
		go func() {
			<-time.After(w.d)
			w.Lock()
			delete(w.list, nick)
			w.Unlock()
		}()
	}
	w.list[nick]++
	if w.list[nick] > w.n {
		kick <- nick
	}
	return
}