better connection handling (at close) in leech

This commit is contained in:
Bora M. Alper 2018-12-25 18:36:51 +03:00
parent 67d2110b6d
commit 22049380c5
No known key found for this signature in database
GPG Key ID: 8F1A9504E1BD114D

View File

@ -2,7 +2,6 @@ package metadata
import ( import (
"bytes" "bytes"
"crypto/rand"
"crypto/sha1" "crypto/sha1"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
@ -47,6 +46,8 @@ type Leech struct {
ut_metadata uint8 ut_metadata uint8
metadataReceived, metadataSize uint metadataReceived, metadataSize uint
metadata []byte metadata []byte
connClosed bool
} }
type LeechEventHandlers struct { type LeechEventHandlers struct {
@ -54,16 +55,13 @@ type LeechEventHandlers struct {
OnError func([20]byte, error) // must be supplied. args: infohash, error OnError func([20]byte, error) // must be supplied. args: infohash, error
} }
func NewLeech(infoHash [20]byte, peerAddr *net.TCPAddr, ev LeechEventHandlers) *Leech { func NewLeech(infoHash [20]byte, peerAddr *net.TCPAddr, clientID []byte, ev LeechEventHandlers) *Leech {
l := new(Leech) l := new(Leech)
l.infoHash = infoHash l.infoHash = infoHash
l.peerAddr = peerAddr l.peerAddr = peerAddr
copy(l.clientID[:], clientID)
l.ev = ev l.ev = ev
if _, err := rand.Read(l.clientID[:]); err != nil {
panic(err.Error())
}
return l return l
} }
@ -286,17 +284,26 @@ func (l *Leech) connect(deadline time.Time) error {
return nil return nil
} }
func (l *Leech) closeConn() {
if l.connClosed {
return
}
if err := l.conn.Close(); err != nil {
zap.L().Panic("couldn't close leech connection!", zap.Error(err))
return
}
l.connClosed = true
}
func (l *Leech) Do(deadline time.Time) { func (l *Leech) Do(deadline time.Time) {
err := l.connect(deadline) err := l.connect(deadline)
if err != nil { if err != nil {
l.OnError(errors.Wrap(err, "connect")) l.OnError(errors.Wrap(err, "connect"))
return return
} }
defer func() { defer l.closeConn()
if err := l.conn.Close(); err != nil {
zap.L().Panic("couldn't close leech connection!", zap.Error(err))
}
}()
err = l.doBtHandshake() err = l.doBtHandshake()
if err != nil { if err != nil {
@ -371,6 +378,10 @@ func (l *Leech) Do(deadline time.Time) {
} }
} }
// We are done with the transfer, close socket as soon as possible (i.e. NOW) to avoid hitting "too many open files"
// error.
l.closeConn()
// Verify the checksum // Verify the checksum
sha1Sum := sha1.Sum(l.metadata) sha1Sum := sha1.Sum(l.metadata)
if !bytes.Equal(sha1Sum[:], l.infoHash[:]) { if !bytes.Equal(sha1Sum[:], l.infoHash[:]) {