Skip to content

Commit

Permalink
p2p/node: replace meta error count with atomic uint32
Browse files Browse the repository at this point in the history
  • Loading branch information
cpl committed May 2, 2019
1 parent 99d8de0 commit e6ff1db
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
11 changes: 6 additions & 5 deletions p2p/node/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package node

import (
"errors"
"sync/atomic"

"cpl.li/go/cryptor/crypt"
"cpl.li/go/cryptor/p2p/noise"
Expand All @@ -15,20 +16,20 @@ func (n *Node) Handshake(p *peer.Peer) (err error) {
n.state.RLock()
if !n.state.isConnected {
n.state.RUnlock()
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return errors.New("can't handshake peer, node not connected")
}
n.state.RUnlock()

// check for nil peer
if p == nil {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return errors.New("peer is nil")
}

// check if peer is missing address
if p.AddrUDP() == nil {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return errors.New("peer address is nil")
}

Expand All @@ -37,7 +38,7 @@ func (n *Node) Handshake(p *peer.Peer) (err error) {
p.RLock()
if p.Handshake != nil {
p.RUnlock()
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return errors.New("peer handshake already exists")
}
p.RUnlock()
Expand Down Expand Up @@ -67,7 +68,7 @@ func (n *Node) Handshake(p *peer.Peer) (err error) {
// prepare handshake message
pack.Payload, err = msg.MarshalBinary()
if err != nil {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return err
}

Expand Down
5 changes: 3 additions & 2 deletions p2p/node/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package node
import (
"encoding/binary"
"net"
"sync/atomic"

"cpl.li/go/cryptor/p2p"
"cpl.li/go/cryptor/p2p/noise"
Expand All @@ -15,7 +16,7 @@ func (n *Node) run() {
// pick up and display errors
case err := <-n.comm.err:
if err != nil {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
n.logger.Println("err", err)
}
// listen for exit signal
Expand Down Expand Up @@ -85,7 +86,7 @@ func (n *Node) listen() {
continue
} else {
// attempt safe disconnect on failed connection
n.Disconnect()
n.comm.err <- n.Disconnect()
continue
}
}
Expand Down
2 changes: 2 additions & 0 deletions p2p/node/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ func TestKillNetwork(t *testing.T) {
tests.AssertNil(t, n.Disconnect())

tests.AssertNil(t, n.Stop())

tests.AssertEqual(t, n.ErrCount(), uint32(4), "unexpected error count")
}
29 changes: 15 additions & 14 deletions p2p/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"os"
"sync"
"sync/atomic"

"cpl.li/go/cryptor/crypt/ppk"
"cpl.li/go/cryptor/p2p"
Expand Down Expand Up @@ -68,7 +69,7 @@ type Node struct {

// metadata
meta struct {
errCount int
errCount uint32
}
}

Expand Down Expand Up @@ -118,7 +119,7 @@ func (n *Node) Start() error {

// ignore if node is already running
if n.state.isRunning {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return errors.New("can't start, node already running")
}

Expand All @@ -141,15 +142,15 @@ func (n *Node) Stop() error {

// ignore if node is not running
if !n.state.isRunning {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return errors.New("can't stop, node not running")
}

// disconnect node if connected before continuing
if n.state.isConnected {
if err := n.disconnect(); err != nil {
// on error, cancel stopping routine
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return err
}
}
Expand All @@ -171,13 +172,13 @@ func (n *Node) Connect() error {

// ignore if node is not running
if !n.state.isRunning {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return errors.New("can't connect to network, node not running")
}

// ignore if node is already connected
if n.state.isConnected {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return errors.New("can't connect to network, already connected")
}

Expand Down Expand Up @@ -206,19 +207,19 @@ func (n *Node) Disconnect() error {

// ignore if node is not running
if !n.state.isRunning {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return errors.New("can't disconnect, node not running")
}

// ignore if node is not connected
if !n.state.isConnected {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return errors.New("can't disconnect, node not connected")
}

// attempt disconnect
if err := n.disconnect(); err != nil {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return err
}

Expand Down Expand Up @@ -254,7 +255,7 @@ func (n *Node) SetAddr(addr string) (err error) {

// ignore if node is connected
if n.state.isConnected {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return errors.New("can't change address while node is connected")
}

Expand All @@ -265,13 +266,13 @@ func (n *Node) SetAddr(addr string) (err error) {
// resolve address (no change yet, validating and checking errors first)
newaddr, err := net.ResolveUDPAddr(p2p.Network, addr)
if err != nil {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return err
}

// reject change if address matches
if newaddr.String() == n.net.addr.String() {
n.meta.errCount++
atomic.AddUint32(&n.meta.errCount, 1)
return errors.New("can't change address, same as current address")
}

Expand All @@ -286,6 +287,6 @@ func (n *Node) SetAddr(addr string) (err error) {
}

// ErrCount returns the number of errors which occurred during runtime.
func (n *Node) ErrCount() int {
return n.meta.errCount
func (n *Node) ErrCount() uint32 {
return atomic.LoadUint32(&n.meta.errCount)
}
2 changes: 1 addition & 1 deletion p2p/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

var zeroKey ppk.PrivateKey

func assertErrCount(t *testing.T, n *node.Node, expected int) {
func assertErrCount(t *testing.T, n *node.Node, expected uint32) {
// check error count
tests.AssertEqual(t, n.ErrCount(), expected, "unexpected error count")
}
Expand Down

0 comments on commit e6ff1db

Please sign in to comment.