Browse Source

remove the ConnHandler

pull/1414/head
Marten Seemann 3 years ago
parent
commit
6d9ce0e3cf
  1. 17
      p2p/net/swarm/peers_test.go
  2. 25
      p2p/net/swarm/swarm.go
  3. 35
      p2p/net/swarm/swarm_test.go

17
p2p/net/swarm/peers_test.go

@ -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) {
@ -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++ {

25
p2p/net/swarm/swarm.go

@ -98,8 +98,7 @@ type Swarm struct {
m map[int]transport.Transport
}
// new connection and stream handlers
connh atomic.Value
// stream handlers
streamh atomic.Value
// dialing helpers
@ -278,14 +277,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
}
@ -294,20 +285,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)

35
p2p/net/swarm/swarm_test.go

@ -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 {

Loading…
Cancel
Save