diff --git a/examples/go.mod b/examples/go.mod index 71bafddf70..18ae819b18 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -9,7 +9,7 @@ require ( github.com/ipfs/go-log/v2 v2.3.0 github.com/libp2p/go-libp2p v0.14.4 github.com/libp2p/go-libp2p-connmgr v0.2.4 - github.com/libp2p/go-libp2p-core v0.11.0 + github.com/libp2p/go-libp2p-core v0.11.1-0.20211024101752-b18a4c9c5629 github.com/libp2p/go-libp2p-discovery v0.5.1 github.com/libp2p/go-libp2p-kad-dht v0.13.0 github.com/libp2p/go-libp2p-noise v0.3.0 diff --git a/examples/go.sum b/examples/go.sum index 2b98a68356..a3757488f9 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -461,8 +461,9 @@ github.com/libp2p/go-libp2p-core v0.8.2/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJB github.com/libp2p/go-libp2p-core v0.8.6/go.mod h1:dgHr0l0hIKfWpGpqAMbpo19pen9wJfdCGv51mTmdpmM= github.com/libp2p/go-libp2p-core v0.9.0/go.mod h1:ESsbz31oC3C1AvMJoGx26RTuCkNhmkSRCqZ0kQtJ2/8= github.com/libp2p/go-libp2p-core v0.10.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= -github.com/libp2p/go-libp2p-core v0.11.0 h1:75jAgdA+IChNa+/mZXogfmrGkgwxkVvxmIC7pV+F6sI= github.com/libp2p/go-libp2p-core v0.11.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= +github.com/libp2p/go-libp2p-core v0.11.1-0.20211024101752-b18a4c9c5629 h1:Gxw2x2hSzZlveZyLMMuOS04Irmu9QVkPaoEBT6KzFHA= +github.com/libp2p/go-libp2p-core v0.11.1-0.20211024101752-b18a4c9c5629/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= github.com/libp2p/go-libp2p-discovery v0.5.1 h1:CJylx+h2+4+s68GvrM4pGNyfNhOYviWBPtVv5PA7sfo= github.com/libp2p/go-libp2p-discovery v0.5.1/go.mod h1:+srtPIU9gDaBNu//UHvcdliKBIcr4SfDcm0/PfPJLug= github.com/libp2p/go-libp2p-kad-dht v0.13.0 h1:qBNYzee8BVS6RkD8ukIAGRG6LmVz8+kkeponyI7W+yA= @@ -484,8 +485,8 @@ github.com/libp2p/go-libp2p-noise v0.3.0/go.mod h1:JNjHbociDJKHD64KTkzGnzqJ0FEV5 github.com/libp2p/go-libp2p-peerstore v0.1.4/go.mod h1:+4BDbDiiKf4PzpANZDAT+knVdLxvqh7hXOujessqdzs= github.com/libp2p/go-libp2p-peerstore v0.2.6/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= github.com/libp2p/go-libp2p-peerstore v0.2.8/go.mod h1:gGiPlXdz7mIHd2vfAsHzBNAMqSDkt2UBFwgcITgw1lA= -github.com/libp2p/go-libp2p-peerstore v0.3.0 h1:wp/G0+37+GLr7tu+wE+4GWNrA3uxKg6IPRigIMSS5oQ= -github.com/libp2p/go-libp2p-peerstore v0.3.0/go.mod h1:fNX9WlOENMvdx/YD7YO/5Hkrn8+lQIk5A39BHa1HIrM= +github.com/libp2p/go-libp2p-peerstore v0.3.1-0.20211024102052-973c2a6b79f6 h1:Pc/COUPE2mQFlOF360E1RbPJwQH6LJ32e8A5/tfmIAM= +github.com/libp2p/go-libp2p-peerstore v0.3.1-0.20211024102052-973c2a6b79f6/go.mod h1:byzexbCYQe5B100abDCT/BiCOLV75ZTZ3rebkFUJKTA= github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6na5f0/k= github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA= github.com/libp2p/go-libp2p-quic-transport v0.11.2/go.mod h1:wlanzKtIh6pHrq+0U3p3DY9PJfGqxMgPaGKaK5LifwQ= diff --git a/examples/ipfs-camp-2019/go.mod b/examples/ipfs-camp-2019/go.mod index e95aa94a9c..458ff629ba 100644 --- a/examples/ipfs-camp-2019/go.mod +++ b/examples/ipfs-camp-2019/go.mod @@ -5,7 +5,7 @@ go 1.16 require ( github.com/gogo/protobuf v1.3.2 github.com/libp2p/go-libp2p v0.14.4 - github.com/libp2p/go-libp2p-core v0.11.0 + github.com/libp2p/go-libp2p-core v0.11.1-0.20211024101752-b18a4c9c5629 github.com/libp2p/go-libp2p-discovery v0.5.1 github.com/libp2p/go-libp2p-kad-dht v0.13.0 github.com/libp2p/go-libp2p-mplex v0.4.1 diff --git a/examples/ipfs-camp-2019/go.sum b/examples/ipfs-camp-2019/go.sum index 783427db80..be313db277 100644 --- a/examples/ipfs-camp-2019/go.sum +++ b/examples/ipfs-camp-2019/go.sum @@ -462,8 +462,9 @@ github.com/libp2p/go-libp2p-core v0.8.2/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJB github.com/libp2p/go-libp2p-core v0.8.6/go.mod h1:dgHr0l0hIKfWpGpqAMbpo19pen9wJfdCGv51mTmdpmM= github.com/libp2p/go-libp2p-core v0.9.0/go.mod h1:ESsbz31oC3C1AvMJoGx26RTuCkNhmkSRCqZ0kQtJ2/8= github.com/libp2p/go-libp2p-core v0.10.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= -github.com/libp2p/go-libp2p-core v0.11.0 h1:75jAgdA+IChNa+/mZXogfmrGkgwxkVvxmIC7pV+F6sI= github.com/libp2p/go-libp2p-core v0.11.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= +github.com/libp2p/go-libp2p-core v0.11.1-0.20211024101752-b18a4c9c5629 h1:Gxw2x2hSzZlveZyLMMuOS04Irmu9QVkPaoEBT6KzFHA= +github.com/libp2p/go-libp2p-core v0.11.1-0.20211024101752-b18a4c9c5629/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= github.com/libp2p/go-libp2p-discovery v0.5.1 h1:CJylx+h2+4+s68GvrM4pGNyfNhOYviWBPtVv5PA7sfo= github.com/libp2p/go-libp2p-discovery v0.5.1/go.mod h1:+srtPIU9gDaBNu//UHvcdliKBIcr4SfDcm0/PfPJLug= github.com/libp2p/go-libp2p-kad-dht v0.13.0 h1:qBNYzee8BVS6RkD8ukIAGRG6LmVz8+kkeponyI7W+yA= @@ -485,8 +486,8 @@ github.com/libp2p/go-libp2p-noise v0.3.0/go.mod h1:JNjHbociDJKHD64KTkzGnzqJ0FEV5 github.com/libp2p/go-libp2p-peerstore v0.1.4/go.mod h1:+4BDbDiiKf4PzpANZDAT+knVdLxvqh7hXOujessqdzs= github.com/libp2p/go-libp2p-peerstore v0.2.6/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= github.com/libp2p/go-libp2p-peerstore v0.2.8/go.mod h1:gGiPlXdz7mIHd2vfAsHzBNAMqSDkt2UBFwgcITgw1lA= -github.com/libp2p/go-libp2p-peerstore v0.3.0 h1:wp/G0+37+GLr7tu+wE+4GWNrA3uxKg6IPRigIMSS5oQ= -github.com/libp2p/go-libp2p-peerstore v0.3.0/go.mod h1:fNX9WlOENMvdx/YD7YO/5Hkrn8+lQIk5A39BHa1HIrM= +github.com/libp2p/go-libp2p-peerstore v0.3.1-0.20211024102052-973c2a6b79f6 h1:Pc/COUPE2mQFlOF360E1RbPJwQH6LJ32e8A5/tfmIAM= +github.com/libp2p/go-libp2p-peerstore v0.3.1-0.20211024102052-973c2a6b79f6/go.mod h1:byzexbCYQe5B100abDCT/BiCOLV75ZTZ3rebkFUJKTA= github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6na5f0/k= github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA= github.com/libp2p/go-libp2p-pubsub v0.5.3 h1:XCn5xvgA/AKpbbaeqbomfKtQCbT9QsU39tYsVj0IndQ= diff --git a/examples/pubsub/chat/go.mod b/examples/pubsub/chat/go.mod index 41c21561d1..791f7d1718 100644 --- a/examples/pubsub/chat/go.mod +++ b/examples/pubsub/chat/go.mod @@ -5,7 +5,7 @@ go 1.16 require ( github.com/gdamore/tcell/v2 v2.1.0 github.com/libp2p/go-libp2p v0.14.1 - github.com/libp2p/go-libp2p-core v0.11.0 + github.com/libp2p/go-libp2p-core v0.11.1-0.20211024101752-b18a4c9c5629 github.com/libp2p/go-libp2p-pubsub v0.5.3 github.com/rivo/tview v0.0.0-20210125085121-dbc1f32bb1d0 ) diff --git a/examples/pubsub/chat/go.sum b/examples/pubsub/chat/go.sum index 3398651a55..141c6e3589 100644 --- a/examples/pubsub/chat/go.sum +++ b/examples/pubsub/chat/go.sum @@ -439,8 +439,9 @@ github.com/libp2p/go-libp2p-core v0.8.2/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJB github.com/libp2p/go-libp2p-core v0.8.6/go.mod h1:dgHr0l0hIKfWpGpqAMbpo19pen9wJfdCGv51mTmdpmM= github.com/libp2p/go-libp2p-core v0.9.0/go.mod h1:ESsbz31oC3C1AvMJoGx26RTuCkNhmkSRCqZ0kQtJ2/8= github.com/libp2p/go-libp2p-core v0.10.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= -github.com/libp2p/go-libp2p-core v0.11.0 h1:75jAgdA+IChNa+/mZXogfmrGkgwxkVvxmIC7pV+F6sI= github.com/libp2p/go-libp2p-core v0.11.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= +github.com/libp2p/go-libp2p-core v0.11.1-0.20211024101752-b18a4c9c5629 h1:Gxw2x2hSzZlveZyLMMuOS04Irmu9QVkPaoEBT6KzFHA= +github.com/libp2p/go-libp2p-core v0.11.1-0.20211024101752-b18a4c9c5629/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= github.com/libp2p/go-libp2p-discovery v0.5.1 h1:CJylx+h2+4+s68GvrM4pGNyfNhOYviWBPtVv5PA7sfo= github.com/libp2p/go-libp2p-discovery v0.5.1/go.mod h1:+srtPIU9gDaBNu//UHvcdliKBIcr4SfDcm0/PfPJLug= github.com/libp2p/go-libp2p-loggables v0.1.0/go.mod h1:EyumB2Y6PrYjr55Q3/tiJ/o3xoDasoRYM7nOzEpoa90= @@ -456,8 +457,8 @@ github.com/libp2p/go-libp2p-noise v0.3.0 h1:NCVH7evhVt9njbTQshzT7N1S3Q6fjj9M11FC github.com/libp2p/go-libp2p-noise v0.3.0/go.mod h1:JNjHbociDJKHD64KTkzGnzqJ0FEV5gHJa6AB00kbCNQ= github.com/libp2p/go-libp2p-peerstore v0.2.6/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= github.com/libp2p/go-libp2p-peerstore v0.2.8/go.mod h1:gGiPlXdz7mIHd2vfAsHzBNAMqSDkt2UBFwgcITgw1lA= -github.com/libp2p/go-libp2p-peerstore v0.3.0 h1:wp/G0+37+GLr7tu+wE+4GWNrA3uxKg6IPRigIMSS5oQ= -github.com/libp2p/go-libp2p-peerstore v0.3.0/go.mod h1:fNX9WlOENMvdx/YD7YO/5Hkrn8+lQIk5A39BHa1HIrM= +github.com/libp2p/go-libp2p-peerstore v0.3.1-0.20211024102052-973c2a6b79f6 h1:Pc/COUPE2mQFlOF360E1RbPJwQH6LJ32e8A5/tfmIAM= +github.com/libp2p/go-libp2p-peerstore v0.3.1-0.20211024102052-973c2a6b79f6/go.mod h1:byzexbCYQe5B100abDCT/BiCOLV75ZTZ3rebkFUJKTA= github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6na5f0/k= github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA= github.com/libp2p/go-libp2p-pubsub v0.5.3 h1:XCn5xvgA/AKpbbaeqbomfKtQCbT9QsU39tYsVj0IndQ= diff --git a/go.mod b/go.mod index 650b85056c..6872eefe3e 100644 --- a/go.mod +++ b/go.mod @@ -23,13 +23,13 @@ require ( github.com/libp2p/go-libp2p-autonat v0.5.0 github.com/libp2p/go-libp2p-blankhost v0.2.0 github.com/libp2p/go-libp2p-circuit v0.4.0 - github.com/libp2p/go-libp2p-core v0.11.0 + github.com/libp2p/go-libp2p-core v0.11.1-0.20211024101752-b18a4c9c5629 github.com/libp2p/go-libp2p-discovery v0.5.1 github.com/libp2p/go-libp2p-mplex v0.4.1 github.com/libp2p/go-libp2p-nat v0.1.0 github.com/libp2p/go-libp2p-netutil v0.1.0 github.com/libp2p/go-libp2p-noise v0.3.0 - github.com/libp2p/go-libp2p-peerstore v0.3.0 + github.com/libp2p/go-libp2p-peerstore v0.3.1-0.20211024102052-973c2a6b79f6 github.com/libp2p/go-libp2p-quic-transport v0.14.0 github.com/libp2p/go-libp2p-swarm v0.7.0 github.com/libp2p/go-libp2p-testing v0.5.0 diff --git a/go.sum b/go.sum index bc9543ae3a..44a07595ca 100644 --- a/go.sum +++ b/go.sum @@ -430,8 +430,9 @@ github.com/libp2p/go-libp2p-core v0.8.2/go.mod h1:FfewUH/YpvWbEB+ZY9AQRQ4TAD8sJB github.com/libp2p/go-libp2p-core v0.8.6/go.mod h1:dgHr0l0hIKfWpGpqAMbpo19pen9wJfdCGv51mTmdpmM= github.com/libp2p/go-libp2p-core v0.9.0/go.mod h1:ESsbz31oC3C1AvMJoGx26RTuCkNhmkSRCqZ0kQtJ2/8= github.com/libp2p/go-libp2p-core v0.10.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= -github.com/libp2p/go-libp2p-core v0.11.0 h1:75jAgdA+IChNa+/mZXogfmrGkgwxkVvxmIC7pV+F6sI= github.com/libp2p/go-libp2p-core v0.11.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= +github.com/libp2p/go-libp2p-core v0.11.1-0.20211024101752-b18a4c9c5629 h1:Gxw2x2hSzZlveZyLMMuOS04Irmu9QVkPaoEBT6KzFHA= +github.com/libp2p/go-libp2p-core v0.11.1-0.20211024101752-b18a4c9c5629/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg= github.com/libp2p/go-libp2p-discovery v0.5.1 h1:CJylx+h2+4+s68GvrM4pGNyfNhOYviWBPtVv5PA7sfo= github.com/libp2p/go-libp2p-discovery v0.5.1/go.mod h1:+srtPIU9gDaBNu//UHvcdliKBIcr4SfDcm0/PfPJLug= github.com/libp2p/go-libp2p-loggables v0.1.0/go.mod h1:EyumB2Y6PrYjr55Q3/tiJ/o3xoDasoRYM7nOzEpoa90= @@ -447,8 +448,8 @@ github.com/libp2p/go-libp2p-noise v0.3.0 h1:NCVH7evhVt9njbTQshzT7N1S3Q6fjj9M11FC github.com/libp2p/go-libp2p-noise v0.3.0/go.mod h1:JNjHbociDJKHD64KTkzGnzqJ0FEV5gHJa6AB00kbCNQ= github.com/libp2p/go-libp2p-peerstore v0.2.6/go.mod h1:ss/TWTgHZTMpsU/oKVVPQCGuDHItOpf2W8RxAi50P2s= github.com/libp2p/go-libp2p-peerstore v0.2.8/go.mod h1:gGiPlXdz7mIHd2vfAsHzBNAMqSDkt2UBFwgcITgw1lA= -github.com/libp2p/go-libp2p-peerstore v0.3.0 h1:wp/G0+37+GLr7tu+wE+4GWNrA3uxKg6IPRigIMSS5oQ= -github.com/libp2p/go-libp2p-peerstore v0.3.0/go.mod h1:fNX9WlOENMvdx/YD7YO/5Hkrn8+lQIk5A39BHa1HIrM= +github.com/libp2p/go-libp2p-peerstore v0.3.1-0.20211024102052-973c2a6b79f6 h1:Pc/COUPE2mQFlOF360E1RbPJwQH6LJ32e8A5/tfmIAM= +github.com/libp2p/go-libp2p-peerstore v0.3.1-0.20211024102052-973c2a6b79f6/go.mod h1:byzexbCYQe5B100abDCT/BiCOLV75ZTZ3rebkFUJKTA= github.com/libp2p/go-libp2p-pnet v0.2.0 h1:J6htxttBipJujEjz1y0a5+eYoiPcFHhSYHH6na5f0/k= github.com/libp2p/go-libp2p-pnet v0.2.0/go.mod h1:Qqvq6JH/oMZGwqs3N1Fqhv8NVhrdYcO0BW4wssv21LA= github.com/libp2p/go-libp2p-quic-transport v0.13.0/go.mod h1:39/ZWJ1TW/jx1iFkKzzUg00W6tDJh73FC0xYudjr7Hc= diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index 958dd8763b..6b90edeb47 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -350,10 +350,11 @@ func (h *BasicHost) updateLocalIpAddr() { } } -// Start starts background tasks in the host +// Start starts watchForAddrChanges tasks in the host func (h *BasicHost) Start() { - h.refCount.Add(1) - go h.background() + h.refCount.Add(2) + go h.watchForAddrChanges() + go h.gcPeerstore() } // newStreamHandler is the remote-opened stream handler for network.Network @@ -459,7 +460,7 @@ func (h *BasicHost) makeSignedPeerRecord(evt *event.EvtLocalAddressesUpdated) (* return record.Seal(rec, h.signKey) } -func (h *BasicHost) background() { +func (h *BasicHost) watchForAddrChanges() { defer h.refCount.Done() var lastAddrs []ma.Multiaddr @@ -521,6 +522,28 @@ func (h *BasicHost) background() { } } +func (h *BasicHost) gcPeerstore() { + defer h.refCount.Done() + sub, err := h.EventBus().Subscribe(&event.EvtPeerConnectednessChanged{}) + if err != nil { + log.Warnw("failed to listen for peer connectedness changed events", "error", err) + return + } + defer sub.Close() + for { + // Note that this might shut down before the swarm has closed all connections. + select { + case <-h.ctx.Done(): + return + case e := <-sub.Out(): + ev := e.(event.EvtPeerConnectednessChanged) + if ev.Connectedness == network.NotConnected { + h.Peerstore().RemovePeer(ev.Peer) + } + } + } +} + // ID returns the (local) peer.ID associated with this Host func (h *BasicHost) ID() peer.ID { return h.Network().LocalPeer() @@ -627,7 +650,7 @@ func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.I }, nil } - // Negotiate the protocol in the background, obeying the context. + // Negotiate the protocol in the watchForAddrChanges, obeying the context. var selected string errCh := make(chan error, 1) go func() { diff --git a/p2p/host/basic/basic_host_test.go b/p2p/host/basic/basic_host_test.go index 9d05dfa1b5..fa0ccaf07c 100644 --- a/p2p/host/basic/basic_host_test.go +++ b/p2p/host/basic/basic_host_test.go @@ -723,7 +723,7 @@ func TestHostAddrChangeDetection(t *testing.T) { require.NoError(t, err) defer sub.Close() - // wait for the host background thread to start + // wait for the host watchForAddrChanges thread to start time.Sleep(1 * time.Second) // host should start with no addrs (addrSet 0) addrs := h.Addrs()