-
Notifications
You must be signed in to change notification settings - Fork 3
/
statistic.go
36 lines (31 loc) · 1.27 KB
/
statistic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package peer
import (
"github.com/assetcloud/chain/system/p2p/dht/protocol"
"github.com/assetcloud/chain/types"
"github.com/libp2p/go-libp2p/core/network"
)
const (
//p2p statistical information
statisticalInfo = "/chain/statistical/1.0.0"
)
// handlerStreamStatistical 返回当前连接的节点信息以及网络带宽信息
func (p *Protocol) handlerStreamStatistical(stream network.Stream) {
defer protocol.CloseStream(stream)
var statistical types.Statistical
statistical.Peers = p.PeerInfoManager.FetchAll()
insize, outsize := p.ConnManager.BoundSize()
statistical.Nodeinfo = &types.NodeNetInfo{}
statistical.Nodeinfo.Inbounds = int32(insize)
statistical.Nodeinfo.Outbounds = int32(outsize)
statistical.Nodeinfo.Peerstore = int32(len(p.Host.Peerstore().PeersWithAddrs()))
statistical.Nodeinfo.Routingtable = int32(p.RoutingTable.Size())
netstat := p.ConnManager.GetNetRate()
statistical.Nodeinfo.Ratein = p.ConnManager.RateCalculate(netstat.RateIn)
statistical.Nodeinfo.Rateout = p.ConnManager.RateCalculate(netstat.RateOut)
statistical.Nodeinfo.Ratetotal = p.ConnManager.RateCalculate(netstat.RateOut + netstat.RateIn)
err := protocol.WriteStream(&statistical, stream)
if err != nil {
log.Error("handleStreamPeerInfoOld", "WriteStream error", err)
return
}
}