2019-08-20 14:48:51 +03:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2019-08-20 23:12:21 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-08-20 23:15:02 +03:00
|
|
|
"github.com/golang/protobuf/proto"
|
2019-08-20 14:48:51 +03:00
|
|
|
"github.com/micro/go-micro/client"
|
2019-08-21 21:16:18 +03:00
|
|
|
rtr "github.com/micro/go-micro/client/selector/router"
|
2019-08-20 23:12:21 +03:00
|
|
|
"github.com/micro/go-micro/proxy"
|
|
|
|
"github.com/micro/go-micro/router"
|
|
|
|
pb "github.com/micro/go-micro/router/proto"
|
2019-08-20 14:48:51 +03:00
|
|
|
"github.com/micro/go-micro/server"
|
2019-08-20 23:12:21 +03:00
|
|
|
"github.com/micro/go-micro/transport"
|
|
|
|
"github.com/micro/go-micro/tunnel"
|
2019-08-21 21:16:18 +03:00
|
|
|
trn "github.com/micro/go-micro/tunnel/transport"
|
2019-08-20 23:12:21 +03:00
|
|
|
"github.com/micro/go-micro/util/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ControlChannel is the name of the tunnel channel for passing contron message
|
2019-08-21 21:16:18 +03:00
|
|
|
ControlChannel = "control"
|
2019-08-27 13:36:46 +03:00
|
|
|
// DefaultLink is default network link
|
|
|
|
DefaultLink = "network"
|
2019-08-20 14:48:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// network implements Network interface
|
|
|
|
type network struct {
|
|
|
|
// options configure the network
|
|
|
|
options Options
|
2019-08-20 23:12:21 +03:00
|
|
|
// rtr is network router
|
|
|
|
router.Router
|
|
|
|
// prx is network proxy
|
|
|
|
proxy.Proxy
|
|
|
|
// tun is network tunnel
|
|
|
|
tunnel.Tunnel
|
2019-08-28 01:08:35 +03:00
|
|
|
// server is network server
|
|
|
|
server server.Server
|
2019-08-20 23:12:21 +03:00
|
|
|
// client is network client
|
|
|
|
client client.Client
|
|
|
|
|
|
|
|
sync.RWMutex
|
|
|
|
// connected marks the network as connected
|
|
|
|
connected bool
|
|
|
|
// closed closes the network
|
|
|
|
closed chan bool
|
2019-08-20 14:48:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newNetwork returns a new network node
|
|
|
|
func newNetwork(opts ...Option) Network {
|
|
|
|
options := DefaultOptions()
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
// init tunnel address to the network bind address
|
|
|
|
options.Tunnel.Init(
|
|
|
|
tunnel.Address(options.Address),
|
|
|
|
)
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
// init router Id to the network id
|
|
|
|
options.Router.Init(
|
|
|
|
router.Id(options.Id),
|
|
|
|
)
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
// create tunnel client with tunnel transport
|
2019-08-27 15:21:36 +03:00
|
|
|
tunTransport := trn.NewTransport(
|
2019-08-21 21:16:18 +03:00
|
|
|
trn.WithTunnel(options.Tunnel),
|
2019-08-20 23:12:21 +03:00
|
|
|
)
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
// server is network server
|
|
|
|
server := server.NewServer(
|
|
|
|
server.Id(options.Id),
|
2019-08-27 15:21:36 +03:00
|
|
|
server.Address(options.Address),
|
2019-08-21 21:22:50 +03:00
|
|
|
server.Name(options.Name),
|
2019-08-20 23:12:21 +03:00
|
|
|
server.Transport(tunTransport),
|
|
|
|
)
|
|
|
|
|
|
|
|
// client is network client
|
|
|
|
client := client.NewClient(
|
|
|
|
client.Transport(tunTransport),
|
2019-08-21 21:16:18 +03:00
|
|
|
client.Selector(
|
|
|
|
rtr.NewSelector(
|
|
|
|
rtr.WithRouter(options.Router),
|
|
|
|
),
|
|
|
|
),
|
2019-08-20 23:12:21 +03:00
|
|
|
)
|
|
|
|
|
2019-08-20 14:48:51 +03:00
|
|
|
return &network{
|
|
|
|
options: options,
|
2019-08-20 23:12:21 +03:00
|
|
|
Router: options.Router,
|
|
|
|
Proxy: options.Proxy,
|
|
|
|
Tunnel: options.Tunnel,
|
2019-08-28 01:08:35 +03:00
|
|
|
server: server,
|
2019-08-20 23:12:21 +03:00
|
|
|
client: client,
|
2019-08-20 14:48:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns network name
|
|
|
|
func (n *network) Name() string {
|
|
|
|
return n.options.Name
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
// Address returns network bind address
|
|
|
|
func (n *network) Address() string {
|
2019-08-21 21:16:18 +03:00
|
|
|
return n.Tunnel.Address()
|
2019-08-20 23:12:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) resolveNodes() ([]string, error) {
|
|
|
|
// resolve the network address to network nodes
|
|
|
|
records, err := n.options.Resolver.Resolve(n.options.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect network node addresses
|
|
|
|
nodes := make([]string, len(records))
|
|
|
|
for i, record := range records {
|
|
|
|
nodes[i] = record.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) resolve() {
|
|
|
|
resolve := time.NewTicker(ResolveTime)
|
|
|
|
defer resolve.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
case <-resolve.C:
|
|
|
|
nodes, err := n.resolveNodes()
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Network failed to resolve nodes: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// initialize the tunnel
|
|
|
|
n.Tunnel.Init(
|
|
|
|
tunnel.Nodes(nodes...),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 15:17:32 +03:00
|
|
|
func (n *network) handleConn(conn tunnel.Conn, msg chan *transport.Message) {
|
2019-08-20 23:12:21 +03:00
|
|
|
for {
|
|
|
|
m := new(transport.Message)
|
2019-08-22 15:17:32 +03:00
|
|
|
if err := conn.Recv(m); err != nil {
|
2019-08-20 23:12:21 +03:00
|
|
|
// TODO: should we bail here?
|
2019-08-22 15:17:32 +03:00
|
|
|
log.Debugf("Network tunnel advert receive error: %v", err)
|
2019-08-20 23:12:21 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-22 15:17:32 +03:00
|
|
|
select {
|
|
|
|
case msg <- m:
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 23:12:21 +03:00
|
|
|
|
2019-08-22 15:17:32 +03:00
|
|
|
func (n *network) process(l tunnel.Listener) {
|
|
|
|
// receive control message queue
|
|
|
|
recv := make(chan *transport.Message, 128)
|
|
|
|
|
|
|
|
// accept a connection
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: handle this
|
|
|
|
log.Debugf("Network tunnel accept error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go n.handleConn(conn, recv)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m := <-recv:
|
|
|
|
// switch on type of message and take action
|
|
|
|
switch m.Header["Micro-Method"] {
|
|
|
|
case "advert":
|
|
|
|
pbAdvert := &pb.Advert{}
|
|
|
|
if err := proto.Unmarshal(m.Body, pbAdvert); err != nil {
|
|
|
|
continue
|
2019-08-20 23:12:21 +03:00
|
|
|
}
|
2019-08-22 15:17:32 +03:00
|
|
|
|
|
|
|
var events []*router.Event
|
|
|
|
for _, event := range pbAdvert.Events {
|
|
|
|
route := router.Route{
|
|
|
|
Service: event.Route.Service,
|
|
|
|
Address: event.Route.Address,
|
|
|
|
Gateway: event.Route.Gateway,
|
|
|
|
Network: event.Route.Network,
|
|
|
|
Link: event.Route.Link,
|
|
|
|
Metric: int(event.Route.Metric),
|
|
|
|
}
|
|
|
|
e := &router.Event{
|
|
|
|
Type: router.EventType(event.Type),
|
|
|
|
Timestamp: time.Unix(0, pbAdvert.Timestamp),
|
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
|
|
|
advert := &router.Advert{
|
|
|
|
Id: pbAdvert.Id,
|
|
|
|
Type: router.AdvertType(pbAdvert.Type),
|
2019-08-20 23:12:21 +03:00
|
|
|
Timestamp: time.Unix(0, pbAdvert.Timestamp),
|
2019-08-22 15:17:32 +03:00
|
|
|
TTL: time.Duration(pbAdvert.Ttl),
|
|
|
|
Events: events,
|
2019-08-20 23:12:21 +03:00
|
|
|
}
|
|
|
|
|
2019-08-22 15:17:32 +03:00
|
|
|
if err := n.Router.Process(advert); err != nil {
|
|
|
|
log.Debugf("Network failed to process advert %s: %v", advert.Id, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-08-20 23:12:21 +03:00
|
|
|
}
|
2019-08-22 15:17:32 +03:00
|
|
|
case <-n.closed:
|
|
|
|
return
|
2019-08-20 23:12:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// advertise advertises routes to the network
|
|
|
|
func (n *network) advertise(client transport.Client, advertChan <-chan *router.Advert) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// process local adverts and randomly fire them at other nodes
|
|
|
|
case advert := <-advertChan:
|
|
|
|
// create a proto advert
|
|
|
|
var events []*pb.Event
|
|
|
|
for _, event := range advert.Events {
|
2019-08-23 23:08:18 +03:00
|
|
|
// NOTE: we override the Gateway and Link fields here
|
2019-08-20 23:12:21 +03:00
|
|
|
route := &pb.Route{
|
|
|
|
Service: event.Route.Service,
|
|
|
|
Address: event.Route.Address,
|
2019-08-23 18:01:57 +03:00
|
|
|
Gateway: n.options.Address,
|
2019-08-23 23:08:18 +03:00
|
|
|
Network: event.Route.Network,
|
2019-08-27 13:36:46 +03:00
|
|
|
Link: DefaultLink,
|
2019-08-20 23:12:21 +03:00
|
|
|
Metric: int64(event.Route.Metric),
|
|
|
|
}
|
|
|
|
e := &pb.Event{
|
|
|
|
Type: pb.EventType(event.Type),
|
|
|
|
Timestamp: event.Timestamp.UnixNano(),
|
|
|
|
Route: route,
|
|
|
|
}
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
|
|
|
pbAdvert := &pb.Advert{
|
|
|
|
Id: advert.Id,
|
|
|
|
Type: pb.AdvertType(advert.Type),
|
|
|
|
Timestamp: advert.Timestamp.UnixNano(),
|
|
|
|
Events: events,
|
|
|
|
}
|
|
|
|
body, err := proto.Marshal(pbAdvert)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: should we bail here?
|
|
|
|
log.Debugf("Network failed to marshal message: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// create transport message and chuck it down the pipe
|
|
|
|
m := transport.Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Micro-Method": "advert",
|
|
|
|
},
|
|
|
|
Body: body,
|
|
|
|
}
|
2019-08-23 18:01:57 +03:00
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
if err := client.Send(&m); err != nil {
|
|
|
|
log.Debugf("Network failed to send advert %s: %v", pbAdvert.Id, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case <-n.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 14:48:51 +03:00
|
|
|
// Connect connects the network
|
|
|
|
func (n *network) Connect() error {
|
2019-08-20 23:12:21 +03:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
// return if already connected
|
|
|
|
if n.connected {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to resolve network nodes
|
|
|
|
nodes, err := n.resolveNodes()
|
|
|
|
if err != nil {
|
2019-08-23 19:48:14 +03:00
|
|
|
log.Debugf("Network failed to resolve nodes: %v", err)
|
2019-08-20 23:12:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// connect network tunnel
|
|
|
|
if err := n.Tunnel.Connect(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize the tunnel to resolved nodes
|
|
|
|
n.Tunnel.Init(
|
|
|
|
tunnel.Nodes(nodes...),
|
|
|
|
)
|
|
|
|
|
|
|
|
// dial into ControlChannel to send route adverts
|
|
|
|
client, err := n.Tunnel.Dial(ControlChannel)
|
|
|
|
if err != nil {
|
2019-08-22 15:17:32 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// listen on ControlChannel
|
|
|
|
listener, err := n.Tunnel.Listen(ControlChannel)
|
|
|
|
if err != nil {
|
2019-08-20 23:12:21 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create closed channel
|
|
|
|
n.closed = make(chan bool)
|
|
|
|
|
|
|
|
// keep resolving network nodes
|
|
|
|
go n.resolve()
|
|
|
|
|
2019-08-21 21:16:18 +03:00
|
|
|
// start the router
|
|
|
|
if err := n.options.Router.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
// start advertising routes
|
|
|
|
advertChan, err := n.options.Router.Advertise()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// advertise routes
|
|
|
|
go n.advertise(client, advertChan)
|
2019-08-22 15:17:32 +03:00
|
|
|
// accept and process routes
|
|
|
|
go n.process(listener)
|
2019-08-20 23:12:21 +03:00
|
|
|
|
2019-08-21 21:16:18 +03:00
|
|
|
// start the server
|
2019-08-28 01:08:35 +03:00
|
|
|
if err := n.server.Start(); err != nil {
|
2019-08-21 21:16:18 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
// set connected to true
|
|
|
|
n.connected = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) close() error {
|
2019-08-21 21:16:18 +03:00
|
|
|
// stop the server
|
2019-08-28 01:08:35 +03:00
|
|
|
if err := n.server.Stop(); err != nil {
|
2019-08-21 21:16:18 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
// stop the router
|
|
|
|
if err := n.Router.Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the tunnel
|
|
|
|
if err := n.Tunnel.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 14:48:51 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes network connection
|
|
|
|
func (n *network) Close() error {
|
2019-08-20 23:12:21 +03:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
if !n.connected {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-n.closed:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(n.closed)
|
|
|
|
// set connected to false
|
|
|
|
n.connected = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return n.close()
|
2019-08-20 14:48:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client returns network client
|
|
|
|
func (n *network) Client() client.Client {
|
2019-08-20 23:12:21 +03:00
|
|
|
return n.client
|
2019-08-20 14:48:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Server returns network server
|
|
|
|
func (n *network) Server() server.Server {
|
2019-08-28 01:08:35 +03:00
|
|
|
return n.server
|
2019-08-20 14:48:51 +03:00
|
|
|
}
|