add the ability to provide seed nodes to the network
This commit is contained in:
parent
6e3d53e1ee
commit
7479515099
@ -75,6 +75,7 @@ func newNetwork(opts ...Option) Network {
|
||||
// init tunnel address to the network bind address
|
||||
options.Tunnel.Init(
|
||||
tunnel.Address(options.Address),
|
||||
tunnel.Nodes(options.Nodes...),
|
||||
)
|
||||
|
||||
// init router Id to the network id
|
||||
@ -135,10 +136,20 @@ func (n *network) resolveNodes() ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nodeMap := make(map[string]bool)
|
||||
|
||||
// collect network node addresses
|
||||
nodes := make([]string, len(records))
|
||||
var nodes []string
|
||||
for i, record := range records {
|
||||
nodes[i] = record.Address
|
||||
nodeMap[record.Address] = true
|
||||
}
|
||||
|
||||
// append seed nodes if we have them
|
||||
for _, node := range n.options.Nodes {
|
||||
if _, ok := nodeMap[node]; !ok {
|
||||
nodes = append(nodes, node)
|
||||
}
|
||||
}
|
||||
|
||||
return nodes, nil
|
||||
|
@ -20,6 +20,8 @@ type Options struct {
|
||||
Name string
|
||||
// Address to bind to
|
||||
Address string
|
||||
// Nodes is a list of seed nodes
|
||||
Nodes []string
|
||||
// Tunnel is network tunnel
|
||||
Tunnel tunnel.Tunnel
|
||||
// Router is network router
|
||||
@ -51,6 +53,14 @@ func Address(a string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// Nodes is a list of seed nodes used along
|
||||
// with resolved node
|
||||
func Nodes(n ...string) Option {
|
||||
return func(o *Options) {
|
||||
o.Nodes = n
|
||||
}
|
||||
}
|
||||
|
||||
// Tunnel sets the network tunnel
|
||||
func Tunnel(t tunnel.Tunnel) Option {
|
||||
return func(o *Options) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user