-
Notifications
You must be signed in to change notification settings - Fork 37
Introspector in swarm #157
Introspector in swarm #157
Conversation
68ba983
to
2669004
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Little to review here; looks sane so far.
swarm.go
Outdated
@@ -9,6 +9,7 @@ import ( | |||
"sync/atomic" | |||
"time" | |||
|
|||
coreit "github.com/libp2p/go-libp2p-core/introspection" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shorten introspection
to introspect
, and drop the import alias.
@raulk Here are the fields that still need some discussion before we implement sourcing for them: Stream
Connections
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few initial comments, mostly about style.
} | ||
|
||
func (ci *connIntrospector) endPoints() *introspectpb.EndpointPair { | ||
return &introspectpb.EndpointPair{SrcMultiaddr: ci.c.LocalMultiaddr().String(), DstMultiaddr: ci.c.RemoteMultiaddr().String()} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: multi-line init is more readable in this case.
// TODO Number of packets & instantaneous bandwidth ? | ||
// Should we have a separate "flow-metre" for a connection to prepare for a message oriented world ? | ||
func (ci *connIntrospector) traffic() *introspectpb.Traffic { | ||
if ci.s.bwc != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In these cases, prefer short-circuiting to reduce one level of nesting/indentation.
if ci.s.bwc == nil {
return nil
}
<rest>
|
||
// IntrospectTraffic introspects & returns the overall traffic for this peer | ||
func (s *Swarm) IntrospectTraffic() (*introspectpb.Traffic, error) { | ||
if s.bwc != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverse the condition, and outdent the logic outside the if.
t := &introspectpb.Traffic{} | ||
metrics := s.bwc.GetBandwidthTotals() | ||
|
||
t.TrafficIn = &introspectpb.DataGauge{CumBytes: uint64(metrics.TotalIn), InstBw: uint64(metrics.RateIn)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: multi-line init is more readable.
swarm.conns.RLock() | ||
defer swarm.conns.RUnlock() | ||
|
||
containsId := func(ids []introspect.ConnectionID, id string) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More efficient to create a map[introspect.ConnectionID]struct{}
, and make the predicate test membership. This is O(m*n)
worst case scenario; map-based solution would be O(m)
at the expense of a little extra memory.
package swarm_test | ||
|
||
import ( | ||
"context" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regroup imports.
@@ -209,11 +222,13 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn, | |||
} | |||
|
|||
// Wrap and register the connection. | |||
id := uuid.NewV4() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An UUID is pretty inefficient. Requires string comparison. Why not an int32 atomic counter at the swarm level, which you increment with every new connection?
@raulk I'm happy to refactor this PR along with implementing the stream/connection scoped metering if you like. |
For libp2p/go-libp2p#775
Changes for swarm to be able to register as a data provider & then provide the data/metrics when it is asked to do so.