aboutsummaryrefslogtreecommitdiffstats
path: root/p2p
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-05-04 22:35:49 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-05-07 20:30:56 +0800
commit4d5a719f256d7dfbaab2cc9c632cd7996067508f (patch)
treed4841e10035f4fd557ef09f2f4c2f478dc3bf762 /p2p
parentaf932177755f5f839ab29b16dc490d3e1bb3708d (diff)
downloadgo-tangerine-4d5a719f256d7dfbaab2cc9c632cd7996067508f.tar
go-tangerine-4d5a719f256d7dfbaab2cc9c632cd7996067508f.tar.gz
go-tangerine-4d5a719f256d7dfbaab2cc9c632cd7996067508f.tar.bz2
go-tangerine-4d5a719f256d7dfbaab2cc9c632cd7996067508f.tar.lz
go-tangerine-4d5a719f256d7dfbaab2cc9c632cd7996067508f.tar.xz
go-tangerine-4d5a719f256d7dfbaab2cc9c632cd7996067508f.tar.zst
go-tangerine-4d5a719f256d7dfbaab2cc9c632cd7996067508f.zip
cmd, eth, p2p: introduce pending peer cli arg, add tests
Diffstat (limited to 'p2p')
-rw-r--r--p2p/server.go25
-rw-r--r--p2p/server_test.go130
2 files changed, 148 insertions, 7 deletions
diff --git a/p2p/server.go b/p2p/server.go
index 164aaba37..77f66f167 100644
--- a/p2p/server.go
+++ b/p2p/server.go
@@ -22,9 +22,7 @@ const (
refreshPeersInterval = 30 * time.Second
staticPeerCheckInterval = 15 * time.Second
- // This is the maximum number of inbound connection
- // that are allowed to linger between 'accepted' and
- // 'added as peer'.
+ // Maximum number of concurrently handshaking inbound connections.
maxAcceptConns = 10
// Maximum number of concurrently dialing outbound connections.
@@ -55,6 +53,11 @@ type Server struct {
// connected. It must be greater than zero.
MaxPeers int
+ // MaxPendingPeers is the maximum number of peers that can be pending in the
+ // handshake phase, counted separately for inbound and outbound connections.
+ // Zero defaults to preset values.
+ MaxPendingPeers int
+
// Name sets the node name of this server.
// Use common.MakeName to create a name that follows existing conventions.
Name string
@@ -334,8 +337,12 @@ func (srv *Server) listenLoop() {
// This channel acts as a semaphore limiting
// active inbound connections that are lingering pre-handshake.
// If all slots are taken, no further connections are accepted.
- slots := make(chan struct{}, maxAcceptConns)
- for i := 0; i < maxAcceptConns; i++ {
+ tokens := maxAcceptConns
+ if srv.MaxPendingPeers > 0 {
+ tokens = srv.MaxPendingPeers
+ }
+ slots := make(chan struct{}, tokens)
+ for i := 0; i < tokens; i++ {
slots <- struct{}{}
}
@@ -405,8 +412,12 @@ func (srv *Server) dialLoop() {
defer refresh.Stop()
// Limit the number of concurrent dials
- slots := make(chan struct{}, maxDialingConns)
- for i := 0; i < maxDialingConns; i++ {
+ tokens := maxAcceptConns
+ if srv.MaxPendingPeers > 0 {
+ tokens = srv.MaxPendingPeers
+ }
+ slots := make(chan struct{}, tokens)
+ for i := 0; i < tokens; i++ {
slots <- struct{}{}
}
dial := func(dest *discover.Node) {
diff --git a/p2p/server_test.go b/p2p/server_test.go
index 3f9db343c..85a5a98cd 100644
--- a/p2p/server_test.go
+++ b/p2p/server_test.go
@@ -369,6 +369,136 @@ func TestServerTrustedPeers(t *testing.T) {
}
}
+// Tests that a failed dial will temporarily throttle a peer.
+func TestServerMaxPendingDials(t *testing.T) {
+ defer testlog(t).detach()
+
+ // Start a simple test server
+ server := &Server{
+ ListenAddr: "127.0.0.1:0",
+ PrivateKey: newkey(),
+ MaxPeers: 10,
+ MaxPendingPeers: 1,
+ }
+ if err := server.Start(); err != nil {
+ t.Fatal("failed to start test server: %v", err)
+ }
+ defer server.Stop()
+
+ // Simulate two separate remote peers
+ peers := make(chan *discover.Node, 2)
+ conns := make(chan net.Conn, 2)
+ for i := 0; i < 2; i++ {
+ listener, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatalf("listener %d: failed to setup: %v", i, err)
+ }
+ defer listener.Close()
+
+ addr := listener.Addr().(*net.TCPAddr)
+ peers <- &discover.Node{
+ ID: discover.PubkeyID(&newkey().PublicKey),
+ IP: addr.IP,
+ TCP: uint16(addr.Port),
+ }
+ go func() {
+ conn, err := listener.Accept()
+ if err == nil {
+ conns <- conn
+ }
+ }()
+ }
+ // Request a dial for both peers
+ go func() {
+ for i := 0; i < 2; i++ {
+ server.staticDial <- <-peers // hack piggybacking the static implementation
+ }
+ }()
+
+ // Make sure only one outbound connection goes through
+ var conn net.Conn
+
+ select {
+ case conn = <-conns:
+ case <-time.After(100 * time.Millisecond):
+ t.Fatalf("first dial timeout")
+ }
+ select {
+ case conn = <-conns:
+ t.Fatalf("second dial completed prematurely")
+ case <-time.After(100 * time.Millisecond):
+ }
+ // Finish the first dial, check the second
+ conn.Close()
+ select {
+ case conn = <-conns:
+ conn.Close()
+
+ case <-time.After(100 * time.Millisecond):
+ t.Fatalf("second dial timeout")
+ }
+}
+
+func TestServerMaxPendingAccepts(t *testing.T) {
+ defer testlog(t).detach()
+
+ // Start a test server and a peer sink for synchronization
+ started := make(chan *Peer)
+ server := &Server{
+ ListenAddr: "127.0.0.1:0",
+ PrivateKey: newkey(),
+ MaxPeers: 10,
+ MaxPendingPeers: 1,
+ NoDial: true,
+ newPeerHook: func(p *Peer) { started <- p },
+ }
+ if err := server.Start(); err != nil {
+ t.Fatal("failed to start test server: %v", err)
+ }
+ defer server.Stop()
+
+ // Try and connect to the server on multiple threads concurrently
+ conns := make([]net.Conn, 2)
+ for i := 0; i < 2; i++ {
+ dialer := &net.Dialer{Deadline: time.Now().Add(3 * time.Second)}
+
+ conn, err := dialer.Dial("tcp", server.ListenAddr)
+ if err != nil {
+ t.Fatalf("failed to dial server: %v", err)
+ }
+ conns[i] = conn
+ }
+ // Check that a handshake on the second doesn't pass
+ go func() {
+ key := newkey()
+ shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
+ if _, err := setupConn(conns[1], key, shake, server.Self(), false, server.trustedNodes); err != nil {
+ t.Fatalf("failed to run handshake: %v", err)
+ }
+ }()
+ select {
+ case <-started:
+ t.Fatalf("handshake on second connection accepted")
+
+ case <-time.After(100 * time.Millisecond):
+ }
+ // Shake on first, check that both go through
+ go func() {
+ key := newkey()
+ shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
+ if _, err := setupConn(conns[0], key, shake, server.Self(), false, server.trustedNodes); err != nil {
+ t.Fatalf("failed to run handshake: %v", err)
+ }
+ }()
+ for i := 0; i < 2; i++ {
+ select {
+ case <-started:
+ case <-time.After(100 * time.Millisecond):
+ t.Fatalf("peer %d: handshake timeout", i)
+ }
+ }
+}
+
func newkey() *ecdsa.PrivateKey {
key, err := crypto.GenerateKey()
if err != nil {