File: blacklist.go

package info (click to toggle)
golang-github-audriusbutkevicius-kcp-go 20160629+git20171025.8ae5f52-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 268 kB
  • sloc: makefile: 2
file content (56 lines) | stat: -rw-r--r-- 958 bytes parent folder | download
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package kcp

import (
	"sync"
	"time"
)

var (
	BlacklistDuration time.Duration
	blacklist         = blacklistMap{
		entries: make(map[sessionKey]time.Time),
	}
)

// a global map for blacklisting conversations
type blacklistMap struct {
	entries map[sessionKey]time.Time
	reapAt  time.Time
	mut     sync.Mutex
}

func (m *blacklistMap) add(address string, conv uint32) {
	if BlacklistDuration == 0 {
		return
	}
	m.mut.Lock()
	timeout := time.Now().Add(BlacklistDuration)
	m.entries[sessionKey{
		addr:   address,
		convID: conv,
	}] = timeout
	m.reap()
	m.mut.Unlock()
}

func (m *blacklistMap) has(address string, conv uint32) bool {
	if BlacklistDuration == 0 {
		return false
	}
	m.mut.Lock()
	t, ok := m.entries[sessionKey{
		addr:   address,
		convID: conv,
	}]
	m.mut.Unlock()
	return ok && t.After(time.Now())
}

func (m *blacklistMap) reap() {
	now := time.Now()
	for k, t := range m.entries {
		if t.Before(now) {
			delete(m.entries, k)
		}
	}
}