micro/network/network.go

109 lines
2.4 KiB
Go
Raw Normal View History

2019-06-18 13:56:11 +03:00
// Package network is a package for defining a network overlay
2019-06-17 18:57:53 +03:00
package network
import (
"github.com/micro/go-micro/config/options"
2019-07-01 13:55:15 +03:00
"github.com/micro/go-micro/network/proxy"
"github.com/micro/go-micro/network/proxy/mucp"
"github.com/micro/go-micro/network/resolver"
"github.com/micro/go-micro/network/resolver/registry"
"github.com/micro/go-micro/network/router"
2019-06-17 18:57:53 +03:00
)
2019-07-02 00:41:27 +03:00
// Network defines a network interface. The network is a single
// shared network between all nodes connected to it. The network
// is responsible for routing messages to the correct services.
2019-06-17 18:57:53 +03:00
type Network interface {
options.Options
2019-07-02 00:41:27 +03:00
// Id of the network
2019-06-22 18:51:20 +03:00
Id() string
// Connect to the network
Connect() (Node, error)
// Peer with a neighboring network
Peer(Network) (Link, error)
2019-06-17 18:57:53 +03:00
}
2019-06-22 18:51:20 +03:00
// Node represents a single node on a network
2019-06-17 18:57:53 +03:00
type Node interface {
2019-07-01 13:55:15 +03:00
// Id of the node
Id() string
2019-06-22 21:02:57 +03:00
// Address of the node
Address() string
2019-07-01 13:55:15 +03:00
// The network of the node
2019-07-02 00:41:27 +03:00
Network() string
2019-06-22 21:02:57 +03:00
// Close the network connection
Close() error
// Accept messages on the network
Accept() (*Message, error)
// Send a message to the network
Send(*Message) error
2019-06-17 18:57:53 +03:00
}
2019-06-22 18:51:20 +03:00
// Link is a connection between one network and another
2019-06-18 13:56:11 +03:00
type Link interface {
2019-06-22 18:51:20 +03:00
// remote node the link is to
2019-06-18 13:56:11 +03:00
Node
2019-07-01 13:55:15 +03:00
// length defines the speed or distance of the link
2019-06-18 13:56:11 +03:00
Length() int
2019-07-01 13:55:15 +03:00
// weight defines the saturation or usage of the link
2019-06-18 13:56:11 +03:00
Weight() int
2019-06-17 18:57:53 +03:00
}
2019-06-17 20:25:42 +03:00
2019-06-18 13:56:11 +03:00
// Message is the base type for opaque data
2019-06-22 18:51:20 +03:00
type Message struct {
// Headers which provide local/remote info
Header map[string]string
// The opaque data being sent
2019-07-01 13:55:15 +03:00
Body []byte
2019-06-22 18:51:20 +03:00
}
2019-06-18 13:56:11 +03:00
2019-06-17 20:25:42 +03:00
var (
// The default network ID is local
2019-07-01 13:55:15 +03:00
DefaultId = "local"
// just the standard network element
DefaultNetwork = NewNetwork()
2019-06-17 20:25:42 +03:00
)
2019-07-01 13:55:15 +03:00
// NewNetwork returns a new network interface
func NewNetwork(opts ...options.Option) Network {
options := options.NewOptions(opts...)
2019-07-01 13:55:15 +03:00
// new network instance
net := &network{
id: DefaultId,
}
// get network id
id, ok := options.Values().Get("network.id")
if ok {
net.id = id.(string)
}
// get router
2019-07-01 13:55:15 +03:00
r, ok := options.Values().Get("network.router")
if ok {
net.router = r.(router.Router)
} else {
net.router = router.DefaultRouter
}
// get proxy
2019-07-01 13:55:15 +03:00
p, ok := options.Values().Get("network.proxy")
if ok {
net.proxy = p.(proxy.Proxy)
} else {
net.proxy = new(mucp.Proxy)
}
2019-07-01 13:55:15 +03:00
// get resolver
res, ok := options.Values().Get("network.resolver")
if ok {
net.resolver = res.(resolver.Resolver)
} else {
net.resolver = new(registry.Resolver)
}
2019-07-01 13:55:15 +03:00
return net
}