Simple connection pruner
This commit is contained in:
parent
eacc624115
commit
dd240b66c4
23
client.go
23
client.go
@ -1301,9 +1301,32 @@ func (cl *Client) AddMagnet(uri string) (t Torrent, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Close()
|
t.Close()
|
||||||
}
|
}
|
||||||
|
go cl.connectionPruner(t.torrent)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cl *Client) connectionPruner(t *torrent) {
|
||||||
|
for {
|
||||||
|
time.Sleep(15 * time.Second)
|
||||||
|
cl.mu.Lock()
|
||||||
|
license := len(t.Conns) - (socketsPerTorrent+1)/2
|
||||||
|
for _, c := range t.Conns {
|
||||||
|
if license <= 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if time.Now().Sub(c.lastUsefulChunkReceived) < time.Minute {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if time.Now().Sub(c.completedHandshake) < time.Minute {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
c.Close()
|
||||||
|
license--
|
||||||
|
}
|
||||||
|
cl.mu.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (me *Client) DropTorrent(infoHash InfoHash) (err error) {
|
func (me *Client) DropTorrent(infoHash InfoHash) (err error) {
|
||||||
me.mu.Lock()
|
me.mu.Lock()
|
||||||
defer me.mu.Unlock()
|
defer me.mu.Unlock()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user