Skip to content
Snippets Groups Projects
Unverified Commit 005edcd9 authored by Wolfgang Welz's avatar Wolfgang Welz Committed by GitHub
Browse files

Make buffconn statistics immutable (#230)

parent f5015a38
No related branches found
No related tags found
No related merge requests found
...@@ -35,6 +35,7 @@ require ( ...@@ -35,6 +35,7 @@ require (
github.com/stretchr/objx v0.2.0 // indirect github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.4.0 github.com/stretchr/testify v1.4.0
github.com/valyala/fasttemplate v1.1.0 // indirect github.com/valyala/fasttemplate v1.1.0 // indirect
go.uber.org/atomic v1.5.1
go.uber.org/zap v1.13.0 go.uber.org/zap v1.13.0
golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"time" "time"
"github.com/iotaledger/hive.go/events" "github.com/iotaledger/hive.go/events"
"go.uber.org/atomic"
) )
const ( const (
...@@ -40,8 +41,8 @@ type BufferedConnection struct { ...@@ -40,8 +41,8 @@ type BufferedConnection struct {
incomingHeaderBuffer []byte incomingHeaderBuffer []byte
closeOnce sync.Once closeOnce sync.Once
BytesRead int bytesRead *atomic.Uint32
BytesWritten int bytesWritten *atomic.Uint32
} }
// NewBufferedConnection creates a new BufferedConnection from a net.Conn. // NewBufferedConnection creates a new BufferedConnection from a net.Conn.
...@@ -53,6 +54,8 @@ func NewBufferedConnection(conn net.Conn) *BufferedConnection { ...@@ -53,6 +54,8 @@ func NewBufferedConnection(conn net.Conn) *BufferedConnection {
}, },
conn: conn, conn: conn,
incomingHeaderBuffer: make([]byte, headerSize), incomingHeaderBuffer: make([]byte, headerSize),
bytesRead: atomic.NewUint32(0),
bytesWritten: atomic.NewUint32(0),
} }
} }
...@@ -77,6 +80,16 @@ func (c *BufferedConnection) RemoteAddr() net.Addr { ...@@ -77,6 +80,16 @@ func (c *BufferedConnection) RemoteAddr() net.Addr {
return c.conn.RemoteAddr() return c.conn.RemoteAddr()
} }
// BytesRead returns the total number of bytes read.
func (c *BufferedConnection) BytesRead() uint32 {
return c.bytesRead.Load()
}
// BytesWritten returns the total number of bytes written.
func (c *BufferedConnection) BytesWritten() uint32 {
return c.bytesWritten.Load()
}
// Read starts reading on the connection, it only returns when an error occurred or when Close has been called. // Read starts reading on the connection, it only returns when an error occurred or when Close has been called.
// If a complete message has been received and ReceiveMessage event is triggered with its complete payload. // If a complete message has been received and ReceiveMessage event is triggered with its complete payload.
// If read leads to an error, the loop will be stopped and that error returned. // If read leads to an error, the loop will be stopped and that error returned.
...@@ -114,7 +127,7 @@ func (c *BufferedConnection) Write(msg []byte) (int, error) { ...@@ -114,7 +127,7 @@ func (c *BufferedConnection) Write(msg []byte) (int, error) {
for bytesWritten := 0; bytesWritten < toWrite; { for bytesWritten := 0; bytesWritten < toWrite; {
n, err := c.conn.Write(buffer[bytesWritten:]) n, err := c.conn.Write(buffer[bytesWritten:])
bytesWritten += n bytesWritten += n
c.BytesWritten += n c.bytesWritten.Add(uint32(n))
if err != nil { if err != nil {
return bytesWritten, err return bytesWritten, err
} }
...@@ -127,7 +140,7 @@ func (c *BufferedConnection) read(buffer []byte) (int, error) { ...@@ -127,7 +140,7 @@ func (c *BufferedConnection) read(buffer []byte) (int, error) {
for bytesRead := 0; bytesRead < toRead; { for bytesRead := 0; bytesRead < toRead; {
n, err := c.conn.Read(buffer[bytesRead:]) n, err := c.conn.Read(buffer[bytesRead:])
bytesRead += n bytesRead += n
c.BytesRead += n c.bytesRead.Add(uint32(n))
if err != nil { if err != nil {
return bytesRead, err return bytesRead, err
} }
......
...@@ -161,8 +161,8 @@ type neighbormetric struct { ...@@ -161,8 +161,8 @@ type neighbormetric struct {
ID string `json:"id"` ID string `json:"id"`
Address string `json:"address"` Address string `json:"address"`
ConnectionOrigin string `json:"connection_origin"` ConnectionOrigin string `json:"connection_origin"`
BytesRead int `json:"bytes_read"` BytesRead uint32 `json:"bytes_read"`
BytesWritten int `json:"bytes_written"` BytesWritten uint32 `json:"bytes_written"`
} }
func neighborMetrics() []neighbormetric { func neighborMetrics() []neighbormetric {
...@@ -186,8 +186,8 @@ func neighborMetrics() []neighbormetric { ...@@ -186,8 +186,8 @@ func neighborMetrics() []neighbormetric {
stats = append(stats, neighbormetric{ stats = append(stats, neighbormetric{
ID: neighbor.Peer.ID().String(), ID: neighbor.Peer.ID().String(),
Address: neighbor.Peer.Services().Get(service.GossipKey).String(), Address: neighbor.Peer.Services().Get(service.GossipKey).String(),
BytesRead: neighbor.BytesRead, BytesRead: neighbor.BytesRead(),
BytesWritten: neighbor.BytesWritten, BytesWritten: neighbor.BytesWritten(),
ConnectionOrigin: origin, ConnectionOrigin: origin,
}) })
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment