Fix the next panic

This commit is contained in:
Asim Aslam 2020-01-15 21:38:37 +00:00
parent b32ebddf85
commit 6562154573

View File

@ -21,15 +21,15 @@ var (
ErrPeerNotFound = errors.New("peer not found") ErrPeerNotFound = errors.New("peer not found")
) )
// nerr tracks node errors // nodeError tracks node errors
type nerr struct { type nodeError struct {
sync.RWMutex sync.RWMutex
count int count int
msg error msg error
} }
// Increment increments node error count // Increment increments node error count
func (e *nerr) Update(err error) { func (e *nodeError) Update(err error) {
e.Lock() e.Lock()
defer e.Unlock() defer e.Unlock()
@ -38,14 +38,14 @@ func (e *nerr) Update(err error) {
} }
// Count returns node error count // Count returns node error count
func (e *nerr) Count() int { func (e *nodeError) Count() int {
e.RLock() e.RLock()
defer e.RUnlock() defer e.RUnlock()
return e.count return e.count
} }
func (e *nerr) Msg() string { func (e *nodeError) Msg() string {
e.RLock() e.RLock()
defer e.RUnlock() defer e.RUnlock()
@ -59,19 +59,19 @@ func (e *nerr) Msg() string {
// status returns node status // status returns node status
type status struct { type status struct {
sync.RWMutex sync.RWMutex
err *nerr err *nodeError
} }
// newStatus creates // newStatus creates
func newStatus() *status { func newStatus() *status {
return &status{ return &status{
err: new(nerr), err: new(nodeError),
} }
} }
func newPeerStatus(peer *pb.Peer) *status { func newPeerStatus(peer *pb.Peer) *status {
status := &status{ status := &status{
err: new(nerr), err: new(nodeError),
} }
// if Node.Status is nil, return empty status // if Node.Status is nil, return empty status
@ -92,7 +92,7 @@ func (s *status) Error() Error {
s.RLock() s.RLock()
defer s.RUnlock() defer s.RUnlock()
return &nerr{ return &nodeError{
count: s.err.count, count: s.err.count,
msg: s.err.msg, msg: s.err.msg,
} }
@ -140,7 +140,7 @@ func (n *node) Status() Status {
defer n.RUnlock() defer n.RUnlock()
return &status{ return &status{
err: &nerr{ err: &nodeError{
count: n.status.err.count, count: n.status.err.count,
msg: n.status.err.msg, msg: n.status.err.msg,
}, },
@ -441,15 +441,10 @@ func (n *node) Peers() []Node {
// UnpackPeerTopology unpacks pb.Peer into node topology of given depth // UnpackPeerTopology unpacks pb.Peer into node topology of given depth
func UnpackPeerTopology(pbPeer *pb.Peer, lastSeen time.Time, depth uint) *node { func UnpackPeerTopology(pbPeer *pb.Peer, lastSeen time.Time, depth uint) *node {
peerNode := &node{ peerNode := &node{
id: pbPeer.Node.Id, id: pbPeer.Node.Id,
address: pbPeer.Node.Address, address: pbPeer.Node.Address,
peers: make(map[string]*node), peers: make(map[string]*node),
status: &status{ status: newPeerStatus(pbPeer),
err: &nerr{
count: int(pbPeer.Node.Status.Error.Count),
msg: errors.New(pbPeer.Node.Status.Error.Msg),
},
},
lastSeen: lastSeen, lastSeen: lastSeen,
} }