mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-27 16:07:42 +08:00
keep track of number of same connections
This commit is contained in:
@ -60,6 +60,7 @@ func NewDiagnostics(self peer.ID, h host.Host) *Diagnostics {
|
||||
type connDiagInfo struct {
|
||||
Latency time.Duration
|
||||
ID string
|
||||
Count int
|
||||
}
|
||||
|
||||
type DiagInfo struct {
|
||||
@ -97,19 +98,13 @@ func (di *DiagInfo) Marshal() []byte {
|
||||
return b
|
||||
}
|
||||
|
||||
func (d *Diagnostics) getPeers() []peer.ID {
|
||||
peers := d.host.Network().Peers()
|
||||
seen := make(map[peer.ID]struct{})
|
||||
out := make([]peer.ID, 0, len(peers))
|
||||
for _, p := range peers {
|
||||
_, ok := seen[p]
|
||||
if !ok {
|
||||
out = append(out, p)
|
||||
seen[p] = struct{}{}
|
||||
}
|
||||
func (d *Diagnostics) getPeers() map[peer.ID]int {
|
||||
counts := make(map[peer.ID]int)
|
||||
for _, p := range d.host.Network().Peers() {
|
||||
counts[p]++
|
||||
}
|
||||
|
||||
return out
|
||||
return counts
|
||||
}
|
||||
|
||||
func (d *Diagnostics) getDiagInfo() *DiagInfo {
|
||||
@ -121,8 +116,12 @@ func (d *Diagnostics) getDiagInfo() *DiagInfo {
|
||||
|
||||
// di.BwIn, di.BwOut = d.host.BandwidthTotals() //TODO fix this.
|
||||
|
||||
for _, p := range d.getPeers() {
|
||||
d := connDiagInfo{d.host.Peerstore().LatencyEWMA(p), p.Pretty()}
|
||||
for p, n := range d.getPeers() {
|
||||
d := connDiagInfo{
|
||||
Latency: d.host.Peerstore().LatencyEWMA(p),
|
||||
ID: p.Pretty(),
|
||||
Count: n,
|
||||
}
|
||||
di.Connections = append(di.Connections, d)
|
||||
}
|
||||
return di
|
||||
@ -157,7 +156,7 @@ func (d *Diagnostics) GetDiagnostic(timeout time.Duration) ([]*DiagInfo, error)
|
||||
|
||||
respdata := make(chan []byte)
|
||||
sends := 0
|
||||
for _, p := range peers {
|
||||
for p, _ := range peers {
|
||||
log.Debugf("Sending getDiagnostic to: %s", p)
|
||||
sends++
|
||||
go func(p peer.ID) {
|
||||
@ -265,7 +264,7 @@ func (d *Diagnostics) handleDiagnostic(p peer.ID, pmes *pb.Message) (*pb.Message
|
||||
|
||||
respdata := make(chan []byte)
|
||||
sendcount := 0
|
||||
for _, p := range d.getPeers() {
|
||||
for p, _ := range d.getPeers() {
|
||||
log.Debugf("Sending diagnostic request to peer: %s", p)
|
||||
sendcount++
|
||||
go func(p peer.ID) {
|
||||
|
Reference in New Issue
Block a user