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.

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