add router selector and network defaults

This commit is contained in:
Asim Aslam
2019-06-26 16:12:57 +01:00
parent 1a62c11166
commit ac098e4d78
6 changed files with 292 additions and 2 deletions

95
network/default.go Normal file
View File

@@ -0,0 +1,95 @@
package network
import (
"sync"
"github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/network/router"
"github.com/micro/go-micro/network/proxy"
)
type network struct {
options.Options
// router
r router.Router
// proxy
p proxy.Proxy
// id of this network
id string
// links maintained for this network
mtx sync.RWMutex
links []Link
}
type node struct {
*network
// address of this node
address string
}
type link struct {
// the embedded node
*node
// length and weight of the link
mtx sync.RWMutex
length, weight int
}
// network methods
func (n *network) Id() string {
return n.id
}
func (n *network) Connect() (Node, error) {
return nil, nil
}
func (n *network) Peer(Network) (Link, error) {
return nil, nil
}
func (n *network) Links() ([]Link, error) {
n.mtx.RLock()
defer n.mtx.RUnlock()
return n.links, nil
}
// node methods
func (n *node) Address() string {
return n.address
}
func (n *node) Close() error {
return nil
}
func (n *node) Accept() (*Message, error) {
return nil, nil
}
func (n *node) Send(*Message) error {
return nil
}
// link methods
func (l *link) Length() int {
l.mtx.RLock()
defer l.mtx.RUnlock()
return l.length
}
func (l *link) Weight() int {
l.mtx.RLock()
defer l.mtx.RUnlock()
return l.weight
}

View File

@@ -51,6 +51,25 @@ type Message struct {
}
var (
// TODO: set default network
DefaultNetwork Network
// The default network ID is local
DefaultNetworkId = "local"
// just the standard network element
DefaultNetwork = NewNetwork()
)
// NewNetwork returns a new network
func NewNetwork(opts ...options.Option) Network {
options := options.NewOptions(opts...)
// get router
// get proxy
return &network{
Options: options,
// fill the blanks
// router: r,
// proxy: p,
}
}

View File

@@ -7,10 +7,12 @@ import (
"strings"
"github.com/micro/go-micro/client"
rselect "github.com/micro/go-micro/client/selector/router"
"github.com/micro/go-micro/codec"
"github.com/micro/go-micro/codec/bytes"
"github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/network/proxy"
"github.com/micro/go-micro/network/router"
"github.com/micro/go-micro/server"
)
@@ -162,5 +164,18 @@ func NewProxy(opts ...options.Option) proxy.Proxy {
p.Client = c.(client.Client)
}
// get router
r, ok := p.Options.Values().Get("proxy.router")
if ok {
// set the router in the client
p.Client.Init(
// pass new selector as an option to the client
client.Selector(rselect.NewSelector(
// set the router in the selector
rselect.WithRouter(r.(router.Router)),
)),
)
}
return p
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/network/router"
"github.com/micro/go-micro/server"
)
@@ -29,3 +30,8 @@ func WithEndpoint(e string) options.Option {
func WithClient(c client.Client) options.Option {
return options.WithValue("proxy.client", c)
}
// WithRouter specifies the router to use
func WithRouter(r router.Router) options.Option {
return options.WithValue("proxy.router", r)
}