lib/connections: Use own KCP fork, move listener setup earlier (ref #4446)
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4452
This commit is contained in:
56
vendor/github.com/AudriusButkevicius/kcp-go/blacklist.go
generated
vendored
Normal file
56
vendor/github.com/AudriusButkevicius/kcp-go/blacklist.go
generated
vendored
Normal file
@@ -0,0 +1,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.Lock()
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -151,6 +151,7 @@ func newUDPSession(conv uint32, dataShards, parityShards int, l *Listener, conn
|
||||
}
|
||||
})
|
||||
sess.kcp.SetMtu(IKCP_MTU_DEF - sess.headerSize)
|
||||
blacklist.add(remote.String(), conv)
|
||||
|
||||
// add current session to the global updater,
|
||||
// which periodically calls sess.update()
|
||||
@@ -751,7 +752,7 @@ func (l *Listener) monitor() {
|
||||
}
|
||||
|
||||
if !ok { // new session
|
||||
if len(l.chAccepts) < cap(l.chAccepts) && len(l.sessions) < 4096 { // do not let new session overwhelm accept queue and connection count
|
||||
if !blacklist.has(from.String(), conv) && len(l.chAccepts) < cap(l.chAccepts) && len(l.sessions) < 4096 { // do not let new session overwhelm accept queue and connection count
|
||||
s := newUDPSession(conv, l.dataShards, l.parityShards, l, l.conn, from, l.block)
|
||||
s.kcpInput(data)
|
||||
l.sessions[key] = s
|
||||
Reference in New Issue
Block a user