Skip to content

Commit

Permalink
remove peers from the peer store when they disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Oct 25, 2021
1 parent b49a645 commit 7fcb3c2
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion examples/ipfs-camp-2019/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions examples/ipfs-camp-2019/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion examples/pubsub/chat/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
7 changes: 4 additions & 3 deletions examples/pubsub/chat/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
32 changes: 27 additions & 5 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -521,6 +522,27 @@ 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
}
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()
Expand Down Expand Up @@ -627,7 +649,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() {
Expand Down
2 changes: 1 addition & 1 deletion p2p/host/basic/basic_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7fcb3c2

Please sign in to comment.