2019-08-20 12:48:51 +01:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2020-08-29 17:44:49 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2021-01-22 23:32:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/meter"
|
2020-11-02 13:25:29 +03:00
|
|
|
"github.com/unistack-org/micro/v3/network/tunnel"
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/proxy"
|
|
|
|
"github.com/unistack-org/micro/v3/router"
|
2021-01-22 23:32:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/tracer"
|
2021-08-20 22:40:48 +03:00
|
|
|
"github.com/unistack-org/micro/v3/util/id"
|
2019-08-20 12:48:51 +01:00
|
|
|
)
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// Option func
|
2019-08-20 12:48:51 +01:00
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
// Options configure network
|
|
|
|
type Options struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Router used for routing
|
|
|
|
Router router.Router
|
|
|
|
// Proxy holds the proxy
|
|
|
|
Proxy proxy.Proxy
|
|
|
|
// Logger used for logging
|
|
|
|
Logger logger.Logger
|
|
|
|
// Meter used for metrics
|
|
|
|
Meter meter.Meter
|
|
|
|
// Tracer used for tracing
|
|
|
|
Tracer tracer.Tracer
|
|
|
|
// Tunnel used for transfer data
|
|
|
|
Tunnel tunnel.Tunnel
|
2019-08-27 23:08:35 +01:00
|
|
|
// Id of the node
|
|
|
|
Id string
|
2019-08-20 12:48:51 +01:00
|
|
|
// Name of the network
|
|
|
|
Name string
|
|
|
|
// Address to bind to
|
|
|
|
Address string
|
2019-09-18 18:56:02 +01:00
|
|
|
// Advertise sets the address to advertise
|
|
|
|
Advertise string
|
2019-12-07 23:28:39 +00:00
|
|
|
// Nodes is a list of nodes to connect to
|
|
|
|
Nodes []string
|
2019-08-20 12:48:51 +01:00
|
|
|
}
|
|
|
|
|
2019-08-27 23:08:35 +01:00
|
|
|
// Id sets the id of the network node
|
|
|
|
func Id(id string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Id = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name sets the network name
|
2019-08-20 12:48:51 +01:00
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 23:08:35 +01:00
|
|
|
// Address sets the network address
|
2019-08-20 12:48:51 +01:00
|
|
|
func Address(a string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Address = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 18:56:02 +01:00
|
|
|
// Advertise sets the address to advertise
|
|
|
|
func Advertise(a string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Advertise = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 23:28:39 +00:00
|
|
|
// Nodes is a list of nodes to connect to
|
|
|
|
func Nodes(n ...string) Option {
|
2019-08-29 14:53:30 +01:00
|
|
|
return func(o *Options) {
|
2019-12-07 23:28:39 +00:00
|
|
|
o.Nodes = n
|
2019-08-29 14:53:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 21:12:21 +01:00
|
|
|
// Tunnel sets the network tunnel
|
|
|
|
func Tunnel(t tunnel.Tunnel) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tunnel = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Router sets the network router
|
|
|
|
func Router(r router.Router) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Router = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy sets the network proxy
|
|
|
|
func Proxy(p proxy.Proxy) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Proxy = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 17:44:49 +03:00
|
|
|
// Logger sets the network logger
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:32:33 +03:00
|
|
|
// Meter sets the meter
|
|
|
|
func Meter(m meter.Meter) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracer to be used for tracing
|
|
|
|
func Tracer(t tracer.Tracer) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tracer = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewOptions returns network default options
|
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
2021-08-20 22:40:48 +03:00
|
|
|
Id: id.Must(),
|
2020-08-18 21:38:29 +01:00
|
|
|
Name: "go.micro",
|
|
|
|
Address: ":0",
|
2021-01-22 23:32:33 +03:00
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
Tracer: tracer.DefaultTracer,
|
2019-08-20 12:48:51 +01:00
|
|
|
}
|
2021-01-22 23:32:33 +03:00
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return options
|
2019-08-20 12:48:51 +01:00
|
|
|
}
|