github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/routing/dht/diag.go (about)

     1  package dht
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	peer "github.com/ipfs/go-ipfs/p2p/peer"
     8  )
     9  
    10  type connDiagInfo struct {
    11  	Latency time.Duration
    12  	ID      peer.ID
    13  }
    14  
    15  type diagInfo struct {
    16  	ID          peer.ID
    17  	Connections []connDiagInfo
    18  	Keys        []string
    19  	LifeSpan    time.Duration
    20  	CodeVersion string
    21  }
    22  
    23  func (di *diagInfo) Marshal() []byte {
    24  	b, err := json.Marshal(di)
    25  	if err != nil {
    26  		panic(err)
    27  	}
    28  	//TODO: also consider compressing this. There will be a lot of these
    29  	return b
    30  }
    31  
    32  func (dht *IpfsDHT) getDiagInfo() *diagInfo {
    33  	di := new(diagInfo)
    34  	di.CodeVersion = "github.com/ipfs/go-ipfs"
    35  	di.ID = dht.self
    36  	di.LifeSpan = time.Since(dht.birth)
    37  	di.Keys = nil // Currently no way to query datastore
    38  
    39  	for _, p := range dht.routingTable.ListPeers() {
    40  		d := connDiagInfo{dht.peerstore.LatencyEWMA(p), p}
    41  		di.Connections = append(di.Connections, d)
    42  	}
    43  	return di
    44  }