First commit. Outline of the default network.

This commit is contained in:
Milos Gajdos 2019-08-20 12:48:51 +01:00
parent 718780367e
commit 6beae23afd
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
9 changed files with 150 additions and 17 deletions

50
network/default.go Normal file
View File

@ -0,0 +1,50 @@
package network
import (
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/server"
)
// network implements Network interface
type network struct {
// options configure the network
options Options
}
// newNetwork returns a new network node
func newNetwork(opts ...Option) Network {
options := DefaultOptions()
for _, o := range opts {
o(&options)
}
return &network{
options: options,
}
}
// Name returns network name
func (n *network) Name() string {
return n.options.Name
}
// Connect connects the network
func (n *network) Connect() error {
return nil
}
// Close closes network connection
func (n *network) Close() error {
return nil
}
// Client returns network client
func (n *network) Client() client.Client {
return nil
}
// Server returns network server
func (n *network) Server() server.Server {
return nil
}

View File

@ -8,8 +8,13 @@ import (
"github.com/micro/go-micro/transport" "github.com/micro/go-micro/transport"
) )
var (
// ErrLinkClosed is returned when attempting i/o operation on the closed link
ErrLinkClosed = errors.New("link closed")
)
// Link is a layer on top of a transport socket with the // Link is a layer on top of a transport socket with the
// buffering send and recv queue's with the ability to // buffering send and recv queues with the ability to
// measure the actual transport link and reconnect if // measure the actual transport link and reconnect if
// an address is specified. // an address is specified.
type Link interface { type Link interface {
@ -28,10 +33,6 @@ type Link interface {
Length() int Length() int
} }
var (
ErrLinkClosed = errors.New("link closed")
)
// NewLink creates a new link on top of a socket // NewLink creates a new link on top of a socket
func NewLink(opts ...options.Option) Link { func NewLink(opts ...options.Option) Link {
return newLink(options.NewOptions(opts...)) return newLink(options.NewOptions(opts...))

View File

@ -1,2 +1,33 @@
// Package network is for creating internetworks // Package network is for creating internetworks
package network package network
import (
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/server"
)
var (
// DefaultName is default network name
DefaultName = "go.micro.network"
// DefaultAddress is default network address
DefaultAddress = ":0"
)
// Network is micro network
type Network interface {
// Name of the network
Name() string
// Connect starts the resolver and tunnel server
Connect() error
// Close stops the tunnel and resolving
Close() error
// Client is micro client
Client() client.Client
// Server is micro server
Server() server.Server
}
// NewNetwork returns a new network interface
func NewNetwork(opts ...Option) Network {
return newNetwork(opts...)
}

48
network/options.go Normal file
View File

@ -0,0 +1,48 @@
package network
import (
"github.com/micro/go-micro/network/resolver"
"github.com/micro/go-micro/network/resolver/dns"
)
type Option func(*Options)
// Options configure network
type Options struct {
// Name of the network
Name string
// Address to bind to
Address string
// Resolver is network resolver
Resolver resolver.Resolver
}
// Name is the network name
func Name(n string) Option {
return func(o *Options) {
o.Name = n
}
}
// Address is the network address
func Address(a string) Option {
return func(o *Options) {
o.Address = a
}
}
// Resolver is the network resolver
func Resolver(r resolver.Resolver) Option {
return func(o *Options) {
o.Resolver = r
}
}
// DefaultOptions returns network default options
func DefaultOptions() Options {
return Options{
Name: DefaultName,
Address: DefaultAddress,
Resolver: &dns.Resolver{},
}
}

View File

@ -8,6 +8,7 @@ import (
"github.com/micro/go-micro/network/resolver" "github.com/micro/go-micro/network/resolver"
) )
// Resolver is a DNS network resolve
type Resolver struct{} type Resolver struct{}
// Resolve assumes ID is a domain name e.g micro.mu // Resolve assumes ID is a domain name e.g micro.mu

View File

@ -10,6 +10,7 @@ import (
"github.com/micro/go-micro/network/resolver" "github.com/micro/go-micro/network/resolver"
) )
// Resolver is a HTTP network resolver
type Resolver struct { type Resolver struct {
// If not set, defaults to http // If not set, defaults to http
Proto string Proto string

View File

@ -6,6 +6,7 @@ import (
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
) )
// Resolver is a registry network resolver
type Resolver struct { type Resolver struct {
// Registry is the registry to use otherwise we use the defaul // Registry is the registry to use otherwise we use the defaul
Registry registry.Registry Registry registry.Registry

View File

@ -5,7 +5,7 @@ package resolver
// via the name to connect to. This is done based on Network.Name(). // via the name to connect to. This is done based on Network.Name().
// Before we can be part of any network, we have to connect to it. // Before we can be part of any network, we have to connect to it.
type Resolver interface { type Resolver interface {
// Resolve returns a list of addresses for an name // Resolve returns a list of addresses for a name
Resolve(name string) ([]*Record, error) Resolve(name string) ([]*Record, error)
} }

View File

@ -5,6 +5,17 @@ import (
"time" "time"
) )
var (
// DefaultAddress is default router address
DefaultAddress = ":9093"
// DefaultName is default router service name
DefaultName = "go.micro.router"
// DefaultNetwork is default micro network
DefaultNetwork = "go.micro"
// DefaultRouter is default network router
DefaultRouter = NewRouter()
)
// Router is an interface for a routing control plane // Router is an interface for a routing control plane
type Router interface { type Router interface {
// Init initializes the router with options // Init initializes the router with options
@ -125,17 +136,6 @@ type Advert struct {
Events []*Event Events []*Event
} }
var (
// DefaultAddress is default router address
DefaultAddress = ":9093"
// DefaultName is default router service name
DefaultName = "go.micro.router"
// DefaultNetwork is default micro network
DefaultNetwork = "go.micro"
// DefaultRouter is default network router
DefaultRouter = NewRouter()
)
// NewRouter creates new Router and returns it // NewRouter creates new Router and returns it
func NewRouter(opts ...Option) Router { func NewRouter(opts ...Option) Router {
return newRouter(opts...) return newRouter(opts...)