1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-28 00:39:31 +08:00

keep track of number of same connections

This commit is contained in:
Jeromy
2015-01-12 06:14:57 +00:00
committed by Juan Batiz-Benet
parent 5d86ce1f71
commit 6cb46a706c

View File

@ -60,6 +60,7 @@ func NewDiagnostics(self peer.ID, h host.Host) *Diagnostics {
type connDiagInfo struct { type connDiagInfo struct {
Latency time.Duration Latency time.Duration
ID string ID string
Count int
} }
type DiagInfo struct { type DiagInfo struct {
@ -97,19 +98,13 @@ func (di *DiagInfo) Marshal() []byte {
return b return b
} }
func (d *Diagnostics) getPeers() []peer.ID { func (d *Diagnostics) getPeers() map[peer.ID]int {
peers := d.host.Network().Peers() counts := make(map[peer.ID]int)
seen := make(map[peer.ID]struct{}) for _, p := range d.host.Network().Peers() {
out := make([]peer.ID, 0, len(peers)) counts[p]++
for _, p := range peers {
_, ok := seen[p]
if !ok {
out = append(out, p)
seen[p] = struct{}{}
}
} }
return out return counts
} }
func (d *Diagnostics) getDiagInfo() *DiagInfo { func (d *Diagnostics) getDiagInfo() *DiagInfo {
@ -121,8 +116,12 @@ func (d *Diagnostics) getDiagInfo() *DiagInfo {
// di.BwIn, di.BwOut = d.host.BandwidthTotals() //TODO fix this. // di.BwIn, di.BwOut = d.host.BandwidthTotals() //TODO fix this.
for _, p := range d.getPeers() { for p, n := range d.getPeers() {
d := connDiagInfo{d.host.Peerstore().LatencyEWMA(p), p.Pretty()} d := connDiagInfo{
Latency: d.host.Peerstore().LatencyEWMA(p),
ID: p.Pretty(),
Count: n,
}
di.Connections = append(di.Connections, d) di.Connections = append(di.Connections, d)
} }
return di return di
@ -157,7 +156,7 @@ func (d *Diagnostics) GetDiagnostic(timeout time.Duration) ([]*DiagInfo, error)
respdata := make(chan []byte) respdata := make(chan []byte)
sends := 0 sends := 0
for _, p := range peers { for p, _ := range peers {
log.Debugf("Sending getDiagnostic to: %s", p) log.Debugf("Sending getDiagnostic to: %s", p)
sends++ sends++
go func(p peer.ID) { 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) respdata := make(chan []byte)
sendcount := 0 sendcount := 0
for _, p := range d.getPeers() { for p, _ := range d.getPeers() {
log.Debugf("Sending diagnostic request to peer: %s", p) log.Debugf("Sending diagnostic request to peer: %s", p)
sendcount++ sendcount++
go func(p peer.ID) { go func(p peer.ID) {