-
Notifications
You must be signed in to change notification settings - Fork 3
/
metrics.go
45 lines (37 loc) · 1.28 KB
/
metrics.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
37
38
39
40
41
42
43
44
45
package gating
import (
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
)
type ConnectionGaterMetrics interface {
RecordDial(allow bool)
RecordAccept(allow bool)
}
type MeteredConnectionGater struct {
BlockingConnectionGater
m ConnectionGaterMetrics
}
func AddMetering(gater BlockingConnectionGater, m ConnectionGaterMetrics) *MeteredConnectionGater {
return &MeteredConnectionGater{BlockingConnectionGater: gater, m: m}
}
func (g *MeteredConnectionGater) InterceptPeerDial(p peer.ID) (allow bool) {
allow = g.BlockingConnectionGater.InterceptPeerDial(p)
g.m.RecordDial(allow)
return allow
}
func (g *MeteredConnectionGater) InterceptAddrDial(id peer.ID, ma multiaddr.Multiaddr) (allow bool) {
allow = g.BlockingConnectionGater.InterceptAddrDial(id, ma)
g.m.RecordDial(allow)
return allow
}
func (g *MeteredConnectionGater) InterceptAccept(mas network.ConnMultiaddrs) (allow bool) {
allow = g.BlockingConnectionGater.InterceptAccept(mas)
g.m.RecordAccept(allow)
return allow
}
func (g *MeteredConnectionGater) InterceptSecured(dir network.Direction, id peer.ID, mas network.ConnMultiaddrs) (allow bool) {
allow = g.BlockingConnectionGater.InterceptSecured(dir, id, mas)
g.m.RecordAccept(allow)
return allow
}