Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
remove the ConnHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Sep 21, 2021
1 parent 88ef86a commit e52190f
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 71 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/ipfs/go-log v1.0.5
github.com/libp2p/go-addr-util v0.1.0
github.com/libp2p/go-conn-security-multistream v0.3.0
github.com/libp2p/go-libp2p-core v0.10.0
github.com/libp2p/go-libp2p-core v0.10.1-0.20210921155317-58d03a60d506
github.com/libp2p/go-libp2p-peerstore v0.2.8
github.com/libp2p/go-libp2p-quic-transport v0.13.0
github.com/libp2p/go-libp2p-testing v0.5.0
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ github.com/libp2p/go-libp2p-core v0.7.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJB
github.com/libp2p/go-libp2p-core v0.8.0/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8=
github.com/libp2p/go-libp2p-core v0.8.1/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJBt/G1rVvhz5XT8=
github.com/libp2p/go-libp2p-core v0.8.6/go.mod h1:dgHr0l0hIKfWpGpqAMbpo19pen9wJfdCGv51mTmdpmM=
github.com/libp2p/go-libp2p-core v0.10.0 h1:jFy7v5Muq58GTeYkPhGzIH8Qq4BFfziqc0ixPd/pP9k=
github.com/libp2p/go-libp2p-core v0.10.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg=
github.com/libp2p/go-libp2p-core v0.10.1-0.20210921155317-58d03a60d506 h1:fnpQJSed/soGntK3qmAsqogzp+WnS0sTqGGbviXV84M=
github.com/libp2p/go-libp2p-core v0.10.1-0.20210921155317-58d03a60d506/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg=
github.com/libp2p/go-libp2p-mplex v0.4.1 h1:/pyhkP1nLwjG3OM+VuaNJkQT/Pqq73WzB3aDN3Fx1sc=
github.com/libp2p/go-libp2p-mplex v0.4.1/go.mod h1:cmy+3GfqfM1PceHTLL7zQzAAYaryDu6iPSC+CIb094g=
github.com/libp2p/go-libp2p-peerstore v0.2.8 h1:nJghUlUkFVvyk7ccsM67oFA6kqUkwyCM1G4WPVMCWYA=
Expand Down
17 changes: 5 additions & 12 deletions peers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package swarm_test
import (
"context"
"testing"
"time"

. "github.com/libp2p/go-libp2p-swarm"

"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"

ma "github.com/multiformats/go-multiaddr"

. "github.com/libp2p/go-libp2p-swarm"
"github.com/stretchr/testify/require"
)

func TestPeers(t *testing.T) {
Expand All @@ -33,17 +35,8 @@ func TestPeers(t *testing.T) {
// t.Log(s.swarm.Dump())
}

s1GotConn := make(chan struct{})
s2GotConn := make(chan struct{})
s1.SetConnHandler(func(c network.Conn) {
s1GotConn <- struct{}{}
})
s2.SetConnHandler(func(c network.Conn) {
s2GotConn <- struct{}{}
})

connect(s1, s2.LocalPeer(), s2.ListenAddresses()[0])
<-s2GotConn // have to wait here so the other side catches up.
require.Eventually(t, func() bool { return len(s2.Peers()) > 0 }, 3*time.Second, 50*time.Millisecond)
connect(s2, s1.LocalPeer(), s1.ListenAddresses()[0])

for i := 0; i < 100; i++ {
Expand Down
22 changes: 0 additions & 22 deletions swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,6 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn,
c.notifyLk.Unlock()

c.start()

// TODO: Get rid of this. We use it for identify but that happen much
// earlier (really, inside the transport and, if not then, during the
// notifications).
if h := s.ConnHandler(); h != nil {
go h(c)
}

return c, nil
}

Expand All @@ -294,20 +286,6 @@ func (s *Swarm) Peerstore() peerstore.Peerstore {
return s.peers
}

// TODO: We probably don't need the conn handlers.

// SetConnHandler assigns the handler for new connections.
// You will rarely use this. See SetStreamHandler
func (s *Swarm) SetConnHandler(handler network.ConnHandler) {
s.connh.Store(handler)
}

// ConnHandler gets the handler for new connections.
func (s *Swarm) ConnHandler() network.ConnHandler {
handler, _ := s.connh.Load().(network.ConnHandler)
return handler
}

// SetStreamHandler assigns the handler for new streams.
func (s *Swarm) SetStreamHandler(handler network.StreamHandler) {
s.streamh.Store(handler)
Expand Down
35 changes: 0 additions & 35 deletions swarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,41 +241,6 @@ func TestBasicSwarm(t *testing.T) {
SubtestSwarm(t, swarms, msgs)
}

func TestConnHandler(t *testing.T) {
// t.Skip("skipping for another test")
t.Parallel()

ctx := context.Background()
swarms := makeSwarms(t, 5)

gotconn := make(chan struct{}, 10)
swarms[0].SetConnHandler(func(conn network.Conn) {
gotconn <- struct{}{}
})

connectSwarms(t, ctx, swarms)

<-time.After(time.Millisecond)
// should've gotten 5 by now.

swarms[0].SetConnHandler(nil)

expect := 4
for i := 0; i < expect; i++ {
select {
case <-time.After(time.Second):
t.Fatal("failed to get connections")
case <-gotconn:
}
}

select {
case <-gotconn:
t.Fatalf("should have connected to %d swarms, got an extra.", expect)
default:
}
}

func TestConnectionGating(t *testing.T) {
ctx := context.Background()
tcs := map[string]struct {
Expand Down

0 comments on commit e52190f

Please sign in to comment.