Commit afe85ce1 authored by Jeromy's avatar Jeromy Committed by Juan Batiz-Benet

add in basic bandwidth tracking to the muxer

parent 4189d50d
...@@ -36,6 +36,12 @@ type Muxer struct { ...@@ -36,6 +36,12 @@ type Muxer struct {
ctx context.Context ctx context.Context
wg sync.WaitGroup wg sync.WaitGroup
bwiLock sync.Mutex
bwIn uint64
bwoLock sync.Mutex
bwOut uint64
*msg.Pipe *msg.Pipe
} }
...@@ -76,6 +82,17 @@ func (m *Muxer) Start(ctx context.Context) error { ...@@ -76,6 +82,17 @@ func (m *Muxer) Start(ctx context.Context) error {
return nil return nil
} }
func (m *Muxer) GetBandwidthTotals() (in uint64, out uint64) {
m.bwiLock.Lock()
in = m.bwIn
m.bwiLock.Unlock()
m.bwoLock.Lock()
out = m.bwOut
m.bwoLock.Unlock()
return
}
// Stop stops muxer activity. // Stop stops muxer activity.
func (m *Muxer) Stop() { func (m *Muxer) Stop() {
if m.cancel == nil { if m.cancel == nil {
...@@ -125,6 +142,11 @@ func (m *Muxer) handleIncomingMessages() { ...@@ -125,6 +142,11 @@ func (m *Muxer) handleIncomingMessages() {
// handleIncomingMessage routes message to the appropriate protocol. // handleIncomingMessage routes message to the appropriate protocol.
func (m *Muxer) handleIncomingMessage(m1 msg.NetMessage) { func (m *Muxer) handleIncomingMessage(m1 msg.NetMessage) {
m.bwiLock.Lock()
// TODO: compensate for overhead
m.bwIn += uint64(len(m1.Data()))
m.bwiLock.Unlock()
data, pid, err := unwrapData(m1.Data()) data, pid, err := unwrapData(m1.Data())
if err != nil { if err != nil {
log.Error("muxer de-serializing error: %v", err) log.Error("muxer de-serializing error: %v", err)
...@@ -173,6 +195,11 @@ func (m *Muxer) handleOutgoingMessage(pid ProtocolID, m1 msg.NetMessage) { ...@@ -173,6 +195,11 @@ func (m *Muxer) handleOutgoingMessage(pid ProtocolID, m1 msg.NetMessage) {
return return
} }
m.bwoLock.Lock()
// TODO: compensate for overhead
m.bwOut += uint64(len(data))
m.bwoLock.Unlock()
m2 := msg.New(m1.Peer(), data) m2 := msg.New(m1.Peer(), data)
select { select {
case m.GetPipe().Outgoing <- m2: case m.GetPipe().Outgoing <- m2:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment