You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

768 lines
21 KiB

p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "math"
  5. "sync"
  6. "time"
  7. "github.com/pkg/errors"
  8. "github.com/tendermint/tendermint/config"
  9. cmn "github.com/tendermint/tendermint/libs/common"
  10. "github.com/tendermint/tendermint/p2p/conn"
  11. )
  12. const (
  13. // wait a random amount of time from this interval
  14. // before dialing peers or reconnecting to help prevent DoS
  15. dialRandomizerIntervalMilliseconds = 3000
  16. // repeatedly try to reconnect for a few minutes
  17. // ie. 5 * 20 = 100s
  18. reconnectAttempts = 20
  19. reconnectInterval = 5 * time.Second
  20. // then move into exponential backoff mode for ~1day
  21. // ie. 3**10 = 16hrs
  22. reconnectBackOffAttempts = 10
  23. reconnectBackOffBaseSeconds = 3
  24. )
  25. // MConnConfig returns an MConnConfig with fields updated
  26. // from the P2PConfig.
  27. func MConnConfig(cfg *config.P2PConfig) conn.MConnConfig {
  28. mConfig := conn.DefaultMConnConfig()
  29. mConfig.FlushThrottle = cfg.FlushThrottleTimeout
  30. mConfig.SendRate = cfg.SendRate
  31. mConfig.RecvRate = cfg.RecvRate
  32. mConfig.MaxPacketMsgPayloadSize = cfg.MaxPacketMsgPayloadSize
  33. return mConfig
  34. }
  35. //-----------------------------------------------------------------------------
  36. // An AddrBook represents an address book from the pex package, which is used
  37. // to store peer addresses.
  38. type AddrBook interface {
  39. AddAddress(addr *NetAddress, src *NetAddress) error
  40. AddOurAddress(*NetAddress)
  41. OurAddress(*NetAddress) bool
  42. MarkGood(ID)
  43. RemoveAddress(*NetAddress)
  44. HasAddress(*NetAddress) bool
  45. Save()
  46. }
  47. // PeerFilterFunc to be implemented by filter hooks after a new Peer has been
  48. // fully setup.
  49. type PeerFilterFunc func(IPeerSet, Peer) error
  50. //-----------------------------------------------------------------------------
  51. // Switch handles peer connections and exposes an API to receive incoming messages
  52. // on `Reactors`. Each `Reactor` is responsible for handling incoming messages of one
  53. // or more `Channels`. So while sending outgoing messages is typically performed on the peer,
  54. // incoming messages are received on the reactor.
  55. type Switch struct {
  56. cmn.BaseService
  57. config *config.P2PConfig
  58. reactors map[string]Reactor
  59. chDescs []*conn.ChannelDescriptor
  60. reactorsByCh map[byte]Reactor
  61. peers *PeerSet
  62. dialing *cmn.CMap
  63. reconnecting *cmn.CMap
  64. nodeInfo NodeInfo // our node info
  65. nodeKey *NodeKey // our node privkey
  66. addrBook AddrBook
  67. // peers addresses with whom we'll maintain constant connection
  68. persistentPeersAddrs []*NetAddress
  69. transport Transport
  70. filterTimeout time.Duration
  71. peerFilters []PeerFilterFunc
  72. rng *cmn.Rand // seed for randomizing dial times and orders
  73. metrics *Metrics
  74. }
  75. // NetAddress returns the address the switch is listening on.
  76. func (sw *Switch) NetAddress() *NetAddress {
  77. addr := sw.transport.NetAddress()
  78. return &addr
  79. }
  80. // SwitchOption sets an optional parameter on the Switch.
  81. type SwitchOption func(*Switch)
  82. // NewSwitch creates a new Switch with the given config.
  83. func NewSwitch(
  84. cfg *config.P2PConfig,
  85. transport Transport,
  86. options ...SwitchOption,
  87. ) *Switch {
  88. sw := &Switch{
  89. config: cfg,
  90. reactors: make(map[string]Reactor),
  91. chDescs: make([]*conn.ChannelDescriptor, 0),
  92. reactorsByCh: make(map[byte]Reactor),
  93. peers: NewPeerSet(),
  94. dialing: cmn.NewCMap(),
  95. reconnecting: cmn.NewCMap(),
  96. metrics: NopMetrics(),
  97. transport: transport,
  98. filterTimeout: defaultFilterTimeout,
  99. persistentPeersAddrs: make([]*NetAddress, 0),
  100. }
  101. // Ensure we have a completely undeterministic PRNG.
  102. sw.rng = cmn.NewRand()
  103. sw.BaseService = *cmn.NewBaseService(nil, "P2P Switch", sw)
  104. for _, option := range options {
  105. option(sw)
  106. }
  107. return sw
  108. }
  109. // SwitchFilterTimeout sets the timeout used for peer filters.
  110. func SwitchFilterTimeout(timeout time.Duration) SwitchOption {
  111. return func(sw *Switch) { sw.filterTimeout = timeout }
  112. }
  113. // SwitchPeerFilters sets the filters for rejection of new peers.
  114. func SwitchPeerFilters(filters ...PeerFilterFunc) SwitchOption {
  115. return func(sw *Switch) { sw.peerFilters = filters }
  116. }
  117. // WithMetrics sets the metrics.
  118. func WithMetrics(metrics *Metrics) SwitchOption {
  119. return func(sw *Switch) { sw.metrics = metrics }
  120. }
  121. //---------------------------------------------------------------------
  122. // Switch setup
  123. // AddReactor adds the given reactor to the switch.
  124. // NOTE: Not goroutine safe.
  125. func (sw *Switch) AddReactor(name string, reactor Reactor) Reactor {
  126. // Validate the reactor.
  127. // No two reactors can share the same channel.
  128. reactorChannels := reactor.GetChannels()
  129. for _, chDesc := range reactorChannels {
  130. chID := chDesc.ID
  131. if sw.reactorsByCh[chID] != nil {
  132. panic(fmt.Sprintf("Channel %X has multiple reactors %v & %v", chID, sw.reactorsByCh[chID], reactor))
  133. }
  134. sw.chDescs = append(sw.chDescs, chDesc)
  135. sw.reactorsByCh[chID] = reactor
  136. }
  137. sw.reactors[name] = reactor
  138. reactor.SetSwitch(sw)
  139. return reactor
  140. }
  141. // Reactors returns a map of reactors registered on the switch.
  142. // NOTE: Not goroutine safe.
  143. func (sw *Switch) Reactors() map[string]Reactor {
  144. return sw.reactors
  145. }
  146. // Reactor returns the reactor with the given name.
  147. // NOTE: Not goroutine safe.
  148. func (sw *Switch) Reactor(name string) Reactor {
  149. return sw.reactors[name]
  150. }
  151. // SetNodeInfo sets the switch's NodeInfo for checking compatibility and handshaking with other nodes.
  152. // NOTE: Not goroutine safe.
  153. func (sw *Switch) SetNodeInfo(nodeInfo NodeInfo) {
  154. sw.nodeInfo = nodeInfo
  155. }
  156. // NodeInfo returns the switch's NodeInfo.
  157. // NOTE: Not goroutine safe.
  158. func (sw *Switch) NodeInfo() NodeInfo {
  159. return sw.nodeInfo
  160. }
  161. // SetNodeKey sets the switch's private key for authenticated encryption.
  162. // NOTE: Not goroutine safe.
  163. func (sw *Switch) SetNodeKey(nodeKey *NodeKey) {
  164. sw.nodeKey = nodeKey
  165. }
  166. //---------------------------------------------------------------------
  167. // Service start/stop
  168. // OnStart implements BaseService. It starts all the reactors and peers.
  169. func (sw *Switch) OnStart() error {
  170. // Start reactors
  171. for _, reactor := range sw.reactors {
  172. err := reactor.Start()
  173. if err != nil {
  174. return cmn.ErrorWrap(err, "failed to start %v", reactor)
  175. }
  176. }
  177. // Start accepting Peers.
  178. go sw.acceptRoutine()
  179. return nil
  180. }
  181. // OnStop implements BaseService. It stops all peers and reactors.
  182. func (sw *Switch) OnStop() {
  183. // Stop peers
  184. for _, p := range sw.peers.List() {
  185. sw.transport.Cleanup(p)
  186. p.Stop()
  187. if sw.peers.Remove(p) {
  188. sw.metrics.Peers.Add(float64(-1))
  189. }
  190. }
  191. // Stop reactors
  192. sw.Logger.Debug("Switch: Stopping reactors")
  193. for _, reactor := range sw.reactors {
  194. reactor.Stop()
  195. }
  196. }
  197. //---------------------------------------------------------------------
  198. // Peers
  199. // Broadcast runs a go routine for each attempted send, which will block trying
  200. // to send for defaultSendTimeoutSeconds. Returns a channel which receives
  201. // success values for each attempted send (false if times out). Channel will be
  202. // closed once msg bytes are sent to all peers (or time out).
  203. //
  204. // NOTE: Broadcast uses goroutines, so order of broadcast may not be preserved.
  205. func (sw *Switch) Broadcast(chID byte, msgBytes []byte) chan bool {
  206. sw.Logger.Debug("Broadcast", "channel", chID, "msgBytes", fmt.Sprintf("%X", msgBytes))
  207. peers := sw.peers.List()
  208. var wg sync.WaitGroup
  209. wg.Add(len(peers))
  210. successChan := make(chan bool, len(peers))
  211. for _, peer := range peers {
  212. go func(p Peer) {
  213. defer wg.Done()
  214. success := p.Send(chID, msgBytes)
  215. successChan <- success
  216. }(peer)
  217. }
  218. go func() {
  219. wg.Wait()
  220. close(successChan)
  221. }()
  222. return successChan
  223. }
  224. // NumPeers returns the count of outbound/inbound and outbound-dialing peers.
  225. func (sw *Switch) NumPeers() (outbound, inbound, dialing int) {
  226. peers := sw.peers.List()
  227. for _, peer := range peers {
  228. if peer.IsOutbound() {
  229. outbound++
  230. } else {
  231. inbound++
  232. }
  233. }
  234. dialing = sw.dialing.Size()
  235. return
  236. }
  237. // MaxNumOutboundPeers returns a maximum number of outbound peers.
  238. func (sw *Switch) MaxNumOutboundPeers() int {
  239. return sw.config.MaxNumOutboundPeers
  240. }
  241. // Peers returns the set of peers that are connected to the switch.
  242. func (sw *Switch) Peers() IPeerSet {
  243. return sw.peers
  244. }
  245. // StopPeerForError disconnects from a peer due to external error.
  246. // If the peer is persistent, it will attempt to reconnect.
  247. // TODO: make record depending on reason.
  248. func (sw *Switch) StopPeerForError(peer Peer, reason interface{}) {
  249. sw.Logger.Error("Stopping peer for error", "peer", peer, "err", reason)
  250. sw.stopAndRemovePeer(peer, reason)
  251. if peer.IsPersistent() {
  252. var addr *NetAddress
  253. if peer.IsOutbound() { // socket address for outbound peers
  254. addr = peer.SocketAddr()
  255. } else { // self-reported address for inbound peers
  256. var err error
  257. addr, err = peer.NodeInfo().NetAddress()
  258. if err != nil {
  259. sw.Logger.Error("Wanted to reconnect to inbound peer, but self-reported address is wrong",
  260. "peer", peer, "addr", addr, "err", err)
  261. return
  262. }
  263. }
  264. go sw.reconnectToPeer(addr)
  265. }
  266. }
  267. // StopPeerGracefully disconnects from a peer gracefully.
  268. // TODO: handle graceful disconnects.
  269. func (sw *Switch) StopPeerGracefully(peer Peer) {
  270. sw.Logger.Info("Stopping peer gracefully")
  271. sw.stopAndRemovePeer(peer, nil)
  272. }
  273. func (sw *Switch) stopAndRemovePeer(peer Peer, reason interface{}) {
  274. if sw.peers.Remove(peer) {
  275. sw.metrics.Peers.Add(float64(-1))
  276. }
  277. sw.transport.Cleanup(peer)
  278. peer.Stop()
  279. for _, reactor := range sw.reactors {
  280. reactor.RemovePeer(peer, reason)
  281. }
  282. }
  283. // reconnectToPeer tries to reconnect to the addr, first repeatedly
  284. // with a fixed interval, then with exponential backoff.
  285. // If no success after all that, it stops trying, and leaves it
  286. // to the PEX/Addrbook to find the peer with the addr again
  287. // NOTE: this will keep trying even if the handshake or auth fails.
  288. // TODO: be more explicit with error types so we only retry on certain failures
  289. // - ie. if we're getting ErrDuplicatePeer we can stop
  290. // because the addrbook got us the peer back already
  291. func (sw *Switch) reconnectToPeer(addr *NetAddress) {
  292. if sw.reconnecting.Has(string(addr.ID)) {
  293. return
  294. }
  295. sw.reconnecting.Set(string(addr.ID), addr)
  296. defer sw.reconnecting.Delete(string(addr.ID))
  297. start := time.Now()
  298. sw.Logger.Info("Reconnecting to peer", "addr", addr)
  299. for i := 0; i < reconnectAttempts; i++ {
  300. if !sw.IsRunning() {
  301. return
  302. }
  303. err := sw.DialPeerWithAddress(addr)
  304. if err == nil {
  305. return // success
  306. } else if _, ok := err.(ErrCurrentlyDialingOrExistingAddress); ok {
  307. return
  308. }
  309. sw.Logger.Info("Error reconnecting to peer. Trying again", "tries", i, "err", err, "addr", addr)
  310. // sleep a set amount
  311. sw.randomSleep(reconnectInterval)
  312. continue
  313. }
  314. sw.Logger.Error("Failed to reconnect to peer. Beginning exponential backoff",
  315. "addr", addr, "elapsed", time.Since(start))
  316. for i := 0; i < reconnectBackOffAttempts; i++ {
  317. if !sw.IsRunning() {
  318. return
  319. }
  320. // sleep an exponentially increasing amount
  321. sleepIntervalSeconds := math.Pow(reconnectBackOffBaseSeconds, float64(i))
  322. sw.randomSleep(time.Duration(sleepIntervalSeconds) * time.Second)
  323. err := sw.DialPeerWithAddress(addr)
  324. if err == nil {
  325. return // success
  326. } else if _, ok := err.(ErrCurrentlyDialingOrExistingAddress); ok {
  327. return
  328. }
  329. sw.Logger.Info("Error reconnecting to peer. Trying again", "tries", i, "err", err, "addr", addr)
  330. }
  331. sw.Logger.Error("Failed to reconnect to peer. Giving up", "addr", addr, "elapsed", time.Since(start))
  332. }
  333. // SetAddrBook allows to set address book on Switch.
  334. func (sw *Switch) SetAddrBook(addrBook AddrBook) {
  335. sw.addrBook = addrBook
  336. }
  337. // MarkPeerAsGood marks the given peer as good when it did something useful
  338. // like contributed to consensus.
  339. func (sw *Switch) MarkPeerAsGood(peer Peer) {
  340. if sw.addrBook != nil {
  341. sw.addrBook.MarkGood(peer.ID())
  342. }
  343. }
  344. //---------------------------------------------------------------------
  345. // Dialing
  346. type privateAddr interface {
  347. PrivateAddr() bool
  348. }
  349. func isPrivateAddr(err error) bool {
  350. te, ok := errors.Cause(err).(privateAddr)
  351. return ok && te.PrivateAddr()
  352. }
  353. // DialPeersAsync dials a list of peers asynchronously in random order.
  354. // Used to dial peers from config on startup or from unsafe-RPC (trusted sources).
  355. // It ignores ErrNetAddressLookup. However, if there are other errors, first
  356. // encounter is returned.
  357. // Nop if there are no peers.
  358. func (sw *Switch) DialPeersAsync(peers []string) error {
  359. netAddrs, errs := NewNetAddressStrings(peers)
  360. // report all the errors
  361. for _, err := range errs {
  362. sw.Logger.Error("Error in peer's address", "err", err)
  363. }
  364. // return first non-ErrNetAddressLookup error
  365. for _, err := range errs {
  366. if _, ok := err.(ErrNetAddressLookup); ok {
  367. continue
  368. }
  369. return err
  370. }
  371. sw.dialPeersAsync(netAddrs)
  372. return nil
  373. }
  374. func (sw *Switch) dialPeersAsync(netAddrs []*NetAddress) {
  375. ourAddr := sw.NetAddress()
  376. // TODO: this code feels like it's in the wrong place.
  377. // The integration tests depend on the addrBook being saved
  378. // right away but maybe we can change that. Recall that
  379. // the addrBook is only written to disk every 2min
  380. if sw.addrBook != nil {
  381. // add peers to `addrBook`
  382. for _, netAddr := range netAddrs {
  383. // do not add our address or ID
  384. if !netAddr.Same(ourAddr) {
  385. if err := sw.addrBook.AddAddress(netAddr, ourAddr); err != nil {
  386. if isPrivateAddr(err) {
  387. sw.Logger.Debug("Won't add peer's address to addrbook", "err", err)
  388. } else {
  389. sw.Logger.Error("Can't add peer's address to addrbook", "err", err)
  390. }
  391. }
  392. }
  393. }
  394. // Persist some peers to disk right away.
  395. // NOTE: integration tests depend on this
  396. sw.addrBook.Save()
  397. }
  398. // permute the list, dial them in random order.
  399. perm := sw.rng.Perm(len(netAddrs))
  400. for i := 0; i < len(perm); i++ {
  401. go func(i int) {
  402. j := perm[i]
  403. addr := netAddrs[j]
  404. if addr.Same(ourAddr) {
  405. sw.Logger.Debug("Ignore attempt to connect to ourselves", "addr", addr, "ourAddr", ourAddr)
  406. return
  407. }
  408. sw.randomSleep(0)
  409. err := sw.DialPeerWithAddress(addr)
  410. if err != nil {
  411. switch err.(type) {
  412. case ErrSwitchConnectToSelf, ErrSwitchDuplicatePeerID, ErrCurrentlyDialingOrExistingAddress:
  413. sw.Logger.Debug("Error dialing peer", "err", err)
  414. default:
  415. sw.Logger.Error("Error dialing peer", "err", err)
  416. }
  417. }
  418. }(i)
  419. }
  420. }
  421. // DialPeerWithAddress dials the given peer and runs sw.addPeer if it connects
  422. // and authenticates successfully.
  423. // If we're currently dialing this address or it belongs to an existing peer,
  424. // ErrCurrentlyDialingOrExistingAddress is returned.
  425. func (sw *Switch) DialPeerWithAddress(addr *NetAddress) error {
  426. if sw.IsDialingOrExistingAddress(addr) {
  427. return ErrCurrentlyDialingOrExistingAddress{addr.String()}
  428. }
  429. sw.dialing.Set(string(addr.ID), addr)
  430. defer sw.dialing.Delete(string(addr.ID))
  431. return sw.addOutboundPeerWithConfig(addr, sw.config)
  432. }
  433. // sleep for interval plus some random amount of ms on [0, dialRandomizerIntervalMilliseconds]
  434. func (sw *Switch) randomSleep(interval time.Duration) {
  435. r := time.Duration(sw.rng.Int63n(dialRandomizerIntervalMilliseconds)) * time.Millisecond
  436. time.Sleep(r + interval)
  437. }
  438. // IsDialingOrExistingAddress returns true if switch has a peer with the given
  439. // address or dialing it at the moment.
  440. func (sw *Switch) IsDialingOrExistingAddress(addr *NetAddress) bool {
  441. return sw.dialing.Has(string(addr.ID)) ||
  442. sw.peers.Has(addr.ID) ||
  443. (!sw.config.AllowDuplicateIP && sw.peers.HasIP(addr.IP))
  444. }
  445. // AddPersistentPeers allows you to set persistent peers. It ignores
  446. // ErrNetAddressLookup. However, if there are other errors, first encounter is
  447. // returned.
  448. func (sw *Switch) AddPersistentPeers(addrs []string) error {
  449. sw.Logger.Info("Adding persistent peers", "addrs", addrs)
  450. netAddrs, errs := NewNetAddressStrings(addrs)
  451. // report all the errors
  452. for _, err := range errs {
  453. sw.Logger.Error("Error in peer's address", "err", err)
  454. }
  455. // return first non-ErrNetAddressLookup error
  456. for _, err := range errs {
  457. if _, ok := err.(ErrNetAddressLookup); ok {
  458. continue
  459. }
  460. return err
  461. }
  462. sw.persistentPeersAddrs = netAddrs
  463. return nil
  464. }
  465. func (sw *Switch) isPeerPersistentFn() func(*NetAddress) bool {
  466. return func(na *NetAddress) bool {
  467. for _, pa := range sw.persistentPeersAddrs {
  468. if pa.Equals(na) {
  469. return true
  470. }
  471. }
  472. return false
  473. }
  474. }
  475. func (sw *Switch) acceptRoutine() {
  476. for {
  477. p, err := sw.transport.Accept(peerConfig{
  478. chDescs: sw.chDescs,
  479. onPeerError: sw.StopPeerForError,
  480. reactorsByCh: sw.reactorsByCh,
  481. metrics: sw.metrics,
  482. isPersistent: sw.isPeerPersistentFn(),
  483. })
  484. if err != nil {
  485. switch err := err.(type) {
  486. case ErrRejected:
  487. if err.IsSelf() {
  488. // Remove the given address from the address book and add to our addresses
  489. // to avoid dialing in the future.
  490. addr := err.Addr()
  491. sw.addrBook.RemoveAddress(&addr)
  492. sw.addrBook.AddOurAddress(&addr)
  493. }
  494. sw.Logger.Info(
  495. "Inbound Peer rejected",
  496. "err", err,
  497. "numPeers", sw.peers.Size(),
  498. )
  499. continue
  500. case ErrFilterTimeout:
  501. sw.Logger.Error(
  502. "Peer filter timed out",
  503. "err", err,
  504. )
  505. continue
  506. case ErrTransportClosed:
  507. sw.Logger.Error(
  508. "Stopped accept routine, as transport is closed",
  509. "numPeers", sw.peers.Size(),
  510. )
  511. default:
  512. sw.Logger.Error(
  513. "Accept on transport errored",
  514. "err", err,
  515. "numPeers", sw.peers.Size(),
  516. )
  517. // We could instead have a retry loop around the acceptRoutine,
  518. // but that would need to stop and let the node shutdown eventually.
  519. // So might as well panic and let process managers restart the node.
  520. // There's no point in letting the node run without the acceptRoutine,
  521. // since it won't be able to accept new connections.
  522. panic(fmt.Errorf("accept routine exited: %v", err))
  523. }
  524. break
  525. }
  526. // Ignore connection if we already have enough peers.
  527. _, in, _ := sw.NumPeers()
  528. if in >= sw.config.MaxNumInboundPeers {
  529. sw.Logger.Info(
  530. "Ignoring inbound connection: already have enough inbound peers",
  531. "address", p.SocketAddr(),
  532. "have", in,
  533. "max", sw.config.MaxNumInboundPeers,
  534. )
  535. sw.transport.Cleanup(p)
  536. continue
  537. }
  538. if err := sw.addPeer(p); err != nil {
  539. sw.transport.Cleanup(p)
  540. if p.IsRunning() {
  541. _ = p.Stop()
  542. }
  543. sw.Logger.Info(
  544. "Ignoring inbound connection: error while adding peer",
  545. "err", err,
  546. "id", p.ID(),
  547. )
  548. }
  549. }
  550. }
  551. // dial the peer; make secret connection; authenticate against the dialed ID;
  552. // add the peer.
  553. // if dialing fails, start the reconnect loop. If handshake fails, it's over.
  554. // If peer is started successfully, reconnectLoop will start when
  555. // StopPeerForError is called.
  556. func (sw *Switch) addOutboundPeerWithConfig(
  557. addr *NetAddress,
  558. cfg *config.P2PConfig,
  559. ) error {
  560. sw.Logger.Info("Dialing peer", "address", addr)
  561. // XXX(xla): Remove the leakage of test concerns in implementation.
  562. if cfg.TestDialFail {
  563. go sw.reconnectToPeer(addr)
  564. return fmt.Errorf("dial err (peerConfig.DialFail == true)")
  565. }
  566. p, err := sw.transport.Dial(*addr, peerConfig{
  567. chDescs: sw.chDescs,
  568. onPeerError: sw.StopPeerForError,
  569. isPersistent: sw.isPeerPersistentFn(),
  570. reactorsByCh: sw.reactorsByCh,
  571. metrics: sw.metrics,
  572. })
  573. if err != nil {
  574. switch e := err.(type) {
  575. case ErrRejected:
  576. if e.IsSelf() {
  577. // Remove the given address from the address book and add to our addresses
  578. // to avoid dialing in the future.
  579. sw.addrBook.RemoveAddress(addr)
  580. sw.addrBook.AddOurAddress(addr)
  581. return err
  582. }
  583. }
  584. // retry persistent peers after
  585. // any dial error besides IsSelf()
  586. if sw.isPeerPersistentFn()(addr) {
  587. go sw.reconnectToPeer(addr)
  588. }
  589. return err
  590. }
  591. if err := sw.addPeer(p); err != nil {
  592. sw.transport.Cleanup(p)
  593. if p.IsRunning() {
  594. _ = p.Stop()
  595. }
  596. return err
  597. }
  598. return nil
  599. }
  600. func (sw *Switch) filterPeer(p Peer) error {
  601. // Avoid duplicate
  602. if sw.peers.Has(p.ID()) {
  603. return ErrRejected{id: p.ID(), isDuplicate: true}
  604. }
  605. errc := make(chan error, len(sw.peerFilters))
  606. for _, f := range sw.peerFilters {
  607. go func(f PeerFilterFunc, p Peer, errc chan<- error) {
  608. errc <- f(sw.peers, p)
  609. }(f, p, errc)
  610. }
  611. for i := 0; i < cap(errc); i++ {
  612. select {
  613. case err := <-errc:
  614. if err != nil {
  615. return ErrRejected{id: p.ID(), err: err, isFiltered: true}
  616. }
  617. case <-time.After(sw.filterTimeout):
  618. return ErrFilterTimeout{}
  619. }
  620. }
  621. return nil
  622. }
  623. // addPeer starts up the Peer and adds it to the Switch. Error is returned if
  624. // the peer is filtered out or failed to start or can't be added.
  625. func (sw *Switch) addPeer(p Peer) error {
  626. if err := sw.filterPeer(p); err != nil {
  627. return err
  628. }
  629. p.SetLogger(sw.Logger.With("peer", p.SocketAddr()))
  630. // Handle the shut down case where the switch has stopped but we're
  631. // concurrently trying to add a peer.
  632. if !sw.IsRunning() {
  633. // XXX should this return an error or just log and terminate?
  634. sw.Logger.Error("Won't start a peer - switch is not running", "peer", p)
  635. return nil
  636. }
  637. // Start the peer's send/recv routines.
  638. // Must start it before adding it to the peer set
  639. // to prevent Start and Stop from being called concurrently.
  640. err := p.Start()
  641. if err != nil {
  642. // Should never happen
  643. sw.Logger.Error("Error starting peer", "err", err, "peer", p)
  644. return err
  645. }
  646. // Add the peer to PeerSet. Do this before starting the reactors
  647. // so that if Receive errors, we will find the peer and remove it.
  648. // Add should not err since we already checked peers.Has().
  649. if err := sw.peers.Add(p); err != nil {
  650. return err
  651. }
  652. sw.metrics.Peers.Add(float64(1))
  653. // Start all the reactor protocols on the peer.
  654. for _, reactor := range sw.reactors {
  655. reactor.AddPeer(p)
  656. }
  657. sw.Logger.Info("Added peer", "peer", p)
  658. return nil
  659. }