From 6beae23afdd9536130dc42894094c960236e5be4 Mon Sep 17 00:00:00 2001 From: Milos Gajdos Date: Tue, 20 Aug 2019 12:48:51 +0100 Subject: [PATCH] First commit. Outline of the default network. --- network/default.go | 50 +++++++++++++++++++++++++++ network/link/link.go | 11 +++--- network/network.go | 31 +++++++++++++++++ network/options.go | 48 +++++++++++++++++++++++++ network/resolver/dns/dns.go | 1 + network/resolver/http/http.go | 1 + network/resolver/registry/registry.go | 1 + network/resolver/resolver.go | 2 +- router/router.go | 22 ++++++------ 9 files changed, 150 insertions(+), 17 deletions(-) create mode 100644 network/default.go create mode 100644 network/options.go diff --git a/network/default.go b/network/default.go new file mode 100644 index 00000000..29265a86 --- /dev/null +++ b/network/default.go @@ -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 +} diff --git a/network/link/link.go b/network/link/link.go index 3acfd772..1c42831a 100644 --- a/network/link/link.go +++ b/network/link/link.go @@ -8,8 +8,13 @@ import ( "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 -// 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 // an address is specified. type Link interface { @@ -28,10 +33,6 @@ type Link interface { Length() int } -var ( - ErrLinkClosed = errors.New("link closed") -) - // NewLink creates a new link on top of a socket func NewLink(opts ...options.Option) Link { return newLink(options.NewOptions(opts...)) diff --git a/network/network.go b/network/network.go index 15d8155f..44fcbee4 100644 --- a/network/network.go +++ b/network/network.go @@ -1,2 +1,33 @@ // Package network is for creating internetworks 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...) +} diff --git a/network/options.go b/network/options.go new file mode 100644 index 00000000..02bc3b1e --- /dev/null +++ b/network/options.go @@ -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{}, + } +} diff --git a/network/resolver/dns/dns.go b/network/resolver/dns/dns.go index ba109656..3cfa2746 100644 --- a/network/resolver/dns/dns.go +++ b/network/resolver/dns/dns.go @@ -8,6 +8,7 @@ import ( "github.com/micro/go-micro/network/resolver" ) +// Resolver is a DNS network resolve type Resolver struct{} // Resolve assumes ID is a domain name e.g micro.mu diff --git a/network/resolver/http/http.go b/network/resolver/http/http.go index b6025a59..055662a5 100644 --- a/network/resolver/http/http.go +++ b/network/resolver/http/http.go @@ -10,6 +10,7 @@ import ( "github.com/micro/go-micro/network/resolver" ) +// Resolver is a HTTP network resolver type Resolver struct { // If not set, defaults to http Proto string diff --git a/network/resolver/registry/registry.go b/network/resolver/registry/registry.go index 9fe80bc5..20fffc18 100644 --- a/network/resolver/registry/registry.go +++ b/network/resolver/registry/registry.go @@ -6,6 +6,7 @@ import ( "github.com/micro/go-micro/registry" ) +// Resolver is a registry network resolver type Resolver struct { // Registry is the registry to use otherwise we use the defaul Registry registry.Registry diff --git a/network/resolver/resolver.go b/network/resolver/resolver.go index 2eb0b2a2..369269df 100644 --- a/network/resolver/resolver.go +++ b/network/resolver/resolver.go @@ -5,7 +5,7 @@ package resolver // 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. 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) } diff --git a/router/router.go b/router/router.go index 7d7ab0de..c5320590 100644 --- a/router/router.go +++ b/router/router.go @@ -5,6 +5,17 @@ import ( "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 type Router interface { // Init initializes the router with options @@ -125,17 +136,6 @@ type Advert struct { 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 func NewRouter(opts ...Option) Router { return newRouter(opts...)