From dcd925f1e53a9512d9b7b995df9aaf90261bb51c Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Tue, 14 Jan 2020 18:48:42 +0000 Subject: [PATCH] Code cleanup; Indentation. --- network/default.go | 95 +++++++++++++---------------- network/network.go | 2 +- network/node.go | 9 +++ network/service/proto/network.proto | 16 ++--- 4 files changed, 62 insertions(+), 60 deletions(-) diff --git a/network/default.go b/network/default.go index 39e29b4b..06137041 100644 --- a/network/default.go +++ b/network/default.go @@ -876,16 +876,11 @@ func (n *network) processNetChan(listener tunnel.Listener) { log.Debugf("Network received peer message from: %s %s", pbNetPeer.Node.Id, pbNetPeer.Node.Address) peer := &node{ - id: pbNetPeer.Node.Id, - address: pbNetPeer.Node.Address, - link: m.msg.Header["Micro-Link"], - peers: make(map[string]*node), - status: &status{ - err: &nerr{ - count: int(pbNetPeer.Node.Status.Error.Count), - msg: errors.New(pbNetPeer.Node.Status.Error.Msg), - }, - }, + id: pbNetPeer.Node.Id, + address: pbNetPeer.Node.Address, + link: m.msg.Header["Micro-Link"], + peers: make(map[string]*node), + status: newPeerStatus(pbNetPeer), lastSeen: now, } @@ -981,16 +976,11 @@ func (n *network) processNetChan(listener tunnel.Listener) { log.Debugf("Network received sync message from: %s", pbNetSync.Peer.Node.Id) peer := &node{ - id: pbNetSync.Peer.Node.Id, - address: pbNetSync.Peer.Node.Address, - link: m.msg.Header["Micro-Link"], - peers: make(map[string]*node), - status: &status{ - err: &nerr{ - count: int(pbNetSync.Peer.Node.Status.Error.Count), - msg: errors.New(pbNetSync.Peer.Node.Status.Error.Msg), - }, - }, + id: pbNetSync.Peer.Node.Id, + address: pbNetSync.Peer.Node.Address, + link: m.msg.Header["Micro-Link"], + peers: make(map[string]*node), + status: newPeerStatus(pbNetSync.Peer), lastSeen: now, } @@ -1279,38 +1269,41 @@ func (n *network) manage() { // get a list of node peers peers := n.Peers() // pick a random peer from the list of peers and request full sync - if peer := n.node.GetPeerNode(peers[rnd.Intn(len(peers))].Id()); peer != nil { - go func() { - // get node peer graph to send back to the connecting node - node := PeersToProto(n.node, MaxDepth) - - msg := &pbNet.Sync{ - Peer: node, - } - - // get a list of all of our routes - routes, err := n.options.Router.Table().List() - switch err { - case nil: - // encode the routes to protobuf - pbRoutes := make([]*pbRtr.Route, 0, len(routes)) - for _, route := range routes { - pbRoute := pbUtil.RouteToProto(route) - pbRoutes = append(pbRoutes, pbRoute) - } - // pack the routes into the sync message - msg.Routes = pbRoutes - default: - // we can't list the routes - log.Debugf("Network node %s failed listing routes: %v", n.id, err) - } - - // send sync message to the newly connected peer - if err := n.sendTo("sync", NetworkChannel, peer, msg); err != nil { - log.Debugf("Network failed to send sync message: %v", err) - } - }() + peer := n.node.GetPeerNode(peers[rnd.Intn(len(peers))].Id()) + if peer != nil { + continue } + + go func() { + // get node peer graph to send back to the connecting node + node := PeersToProto(n.node, MaxDepth) + + msg := &pbNet.Sync{ + Peer: node, + } + + // get a list of all of our routes + routes, err := n.options.Router.Table().List() + switch err { + case nil: + // encode the routes to protobuf + pbRoutes := make([]*pbRtr.Route, 0, len(routes)) + for _, route := range routes { + pbRoute := pbUtil.RouteToProto(route) + pbRoutes = append(pbRoutes, pbRoute) + } + // pack the routes into the sync message + msg.Routes = pbRoutes + default: + // we can't list the routes + log.Debugf("Network node %s failed listing routes: %v", n.id, err) + } + + // send sync message to the newly connected peer + if err := n.sendTo("sync", NetworkChannel, peer, msg); err != nil { + log.Debugf("Network failed to send sync message: %v", err) + } + }() case <-resolve.C: n.initNodes(false) } diff --git a/network/network.go b/network/network.go index e06cd2c6..535870f0 100644 --- a/network/network.go +++ b/network/network.go @@ -20,7 +20,7 @@ var ( // KeepAliveTime is the time in which we want to have sent a message to a peer KeepAliveTime = 30 * time.Second // SyncTime is the time a network node requests full sync from the network - SyncTime = 5 * time.Minute + SyncTime = 1 * time.Minute // PruneTime defines time interval to periodically check nodes that need to be pruned // due to their not announcing their presence within this time interval PruneTime = 90 * time.Second diff --git a/network/node.go b/network/node.go index a7f3c51a..d632e299 100644 --- a/network/node.go +++ b/network/node.go @@ -69,6 +69,15 @@ func newStatus() *status { } } +func newPeerStatus(peer *pb.Peer) *status { + return &status{ + err: &nerr{ + count: int(peer.Node.Status.Error.Count), + msg: errors.New(peer.Node.Status.Error.Msg), + }, + } +} + func (s *status) Error() Error { s.RLock() defer s.RUnlock() diff --git a/network/service/proto/network.proto b/network/service/proto/network.proto index da08293a..f2bbdb9e 100644 --- a/network/service/proto/network.proto +++ b/network/service/proto/network.proto @@ -6,16 +6,16 @@ import "github.com/micro/go-micro/router/service/proto/router.proto"; // Network service is usesd to gain visibility into networks service Network { - // Connect to the network - rpc Connect(ConnectRequest) returns (ConnectResponse) {}; - // Returns the entire network graph - rpc Graph(GraphRequest) returns (GraphResponse) {}; - // Returns a list of known nodes in the network + // Connect to the network + rpc Connect(ConnectRequest) returns (ConnectResponse) {}; + // Returns the entire network graph + rpc Graph(GraphRequest) returns (GraphResponse) {}; + // Returns a list of known nodes in the network rpc Nodes(NodesRequest) returns (NodesResponse) {}; - // Returns a list of known routes in the network + // Returns a list of known routes in the network rpc Routes(RoutesRequest) returns (RoutesResponse) {}; - // Returns a list of known services based on routes - rpc Services(ServicesRequest) returns (ServicesResponse) {}; + // Returns a list of known services based on routes + rpc Services(ServicesRequest) returns (ServicesResponse) {}; // Status returns network status rpc Status(StatusRequest) returns (StatusResponse) {}; }