Skip to content

Commit

Permalink
Merge pull request #33 from erigontech/peers_enable
Browse files Browse the repository at this point in the history
Peers enable
  • Loading branch information
dvovk authored Oct 16, 2024
2 parents 3672b16 + 6e40a01 commit ff0e9b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,7 @@ func (t *Torrent) openNewConns(lock bool) (initiated int) {
receivedHolepunchConnect: false,
HeaderObfuscationPolicy: t.cl.config.HeaderObfuscationPolicy,
}
initiateConn(opts, false, false)
initiateConn(opts, false, false, false)
initiated++
}

Expand Down Expand Up @@ -2533,6 +2533,10 @@ func (t *Torrent) wantOutgoingConns(lock bool) bool {
conns := t.peerConnsAsSlice(lock)
defer conns.free()

if len(conns) < t.maxEstablishedConns {
return true
}

numOutgoingConns := conns.numOutgoingConns()
numIncomingConns := len(conns) - numOutgoingConns

Expand Down Expand Up @@ -3103,21 +3107,29 @@ func (t *Torrent) addHalfOpen(addrStr string, attemptKey *PeerInfo, lock bool) {
if path.Exists() {
panic("should be unique")
}

path.Set(attemptKey)
t.cl.numHalfOpen++
}

// Start the process of connecting to the given peer for the given torrent if appropriate. I'm not
// sure all the PeerInfo fields are being used.
func initiateConn(opts outgoingConnOpts, ignoreLimits bool, lock bool) {
func initiateConn(opts outgoingConnOpts, ignoreLimits bool, lock bool, lockCLient bool) {
t := opts.t
peer := opts.peerInfo

if lockCLient {
t.cl.rLock()
defer t.cl.rUnlock()
}

if peer.Id == t.cl.peerID {
return
}
if t.cl.badPeerAddr(peer.Addr) && !peer.Trusted {
return
}

addr := peer.Addr
addrStr := addr.String()
if !ignoreLimits {
Expand All @@ -3130,6 +3142,7 @@ func initiateConn(opts outgoingConnOpts, ignoreLimits bool, lock bool) {
}
attemptKey := &peer
t.addHalfOpen(addrStr, attemptKey, lock)

go t.cl.outgoingConnection(
opts,
attemptKey,
Expand Down Expand Up @@ -3651,12 +3664,14 @@ func (t *Torrent) handleReceivedUtHolepunchMsg(msg utHolepunch.Msg, sender *Peer
case utHolepunch.Connect:
holepunchAddr := msg.AddrPort
t.logger.Printf("got holepunch connect request for %v from %p", holepunchAddr, sender)
t.cl.lock()
if g.MapContains(t.cl.undialableWithoutHolepunch, holepunchAddr) {
setAdd(&t.cl.undialableWithoutHolepunchDialedAfterHolepunchConnect, holepunchAddr)
if g.MapContains(t.cl.accepted, holepunchAddr) {
setAdd(&t.cl.probablyOnlyConnectedDueToHolepunch, holepunchAddr)
}
}
t.cl.unlock()
opts := outgoingConnOpts{
peerInfo: PeerInfo{
Addr: msg.AddrPort,
Expand All @@ -3671,7 +3686,7 @@ func (t *Torrent) handleReceivedUtHolepunchMsg(msg utHolepunch.Msg, sender *Peer
// encryption. So we will act normally.
HeaderObfuscationPolicy: t.cl.config.HeaderObfuscationPolicy,
}
initiateConn(opts, true, true)
initiateConn(opts, true, true, true)
return nil
case utHolepunch.Error:
torrent.Add("holepunch error messages received", 1)
Expand Down
2 changes: 1 addition & 1 deletion ut-holepunching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestHolepunchConnect(t *testing.T) {
requireRendezvous: true,
skipHolepunchRendezvous: false,
HeaderObfuscationPolicy: llg.cl.config.HeaderObfuscationPolicy,
}, true, true)
}, true, true, true)
llg.cl.unlock()
wg.Wait()

Expand Down

0 comments on commit ff0e9b2

Please sign in to comment.