From 74795150993538d263c46ec429d13e3c8a34eeda Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Thu, 29 Aug 2019 14:53:30 +0100 Subject: [PATCH] add the ability to provide seed nodes to the network --- network/default.go | 13 ++++++++++++- network/options.go | 10 ++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/network/default.go b/network/default.go index ca8328b3..204bcd81 100644 --- a/network/default.go +++ b/network/default.go @@ -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 diff --git a/network/options.go b/network/options.go index 5894b857..e670cf4c 100644 --- a/network/options.go +++ b/network/options.go @@ -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) {