channeldb: implement a multi-source AddrSource
In this commit, we implement a version of the AddrSource interface which merges the results of a set of AddrSource implementations. This will later be used to merge the results of the channel db and graph db.
This commit is contained in:
2 files changed
+172
No files matched your search
@@ -13,3 +13,56 @@ type AddrSource interface {
|
||||
// key.
|
||||
AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr, error)
|
||||
}
|
||||
|
||||
// multiAddrSource is an implementation of AddrSource which gathers all the
|
||||
// known addresses for a given node from multiple backends and de-duplicates the
|
||||
// results.
|
||||
type multiAddrSource struct {
|
||||
sources []AddrSource
|
||||
}
|
||||
|
||||
// NewMultiAddrSource constructs a new AddrSource which will query all the
|
||||
// provided sources for a node's addresses and will then de-duplicate the
|
||||
// results.
|
||||
func NewMultiAddrSource(sources ...AddrSource) AddrSource {
|
||||
return &multiAddrSource{
|
||||
sources: sources,
|
||||
}
|
||||
}
|
||||
|
||||
// AddrsForNode returns all known addresses for the target node public key. It
|
||||
// queries all the address sources provided and de-duplicates the results.
|
||||
//
|
||||
// NOTE: this implements the AddrSource interface.
|
||||
func (c *multiAddrSource) AddrsForNode(nodePub *btcec.PublicKey) ([]net.Addr,
|
||||
error) {
|
||||
|
||||
// The multiple address sources will likely contain duplicate addresses,
|
||||
// so we use a map here to de-dup them.
|
||||
dedupedAddrs := make(map[string]net.Addr)
|
||||
|
||||
// Iterate over all the address sources and query each one for the
|
||||
// addresses it has for the node in question.
|
||||
for _, src := range c.sources {
|
||||
addrs, err := src.AddrsForNode(nodePub)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
dedupedAddrs[addr.String()] = addr
|
||||
}
|
||||
}
|
||||
|
||||
// Convert the map into a list we can return.
|
||||
addrs := make([]net.Addr, 0, len(dedupedAddrs))
|
||||
for _, addr := range dedupedAddrs {
|
||||
addrs = append(addrs, addr)
|
||||
}
|
||||
|
||||
return addrs, nil
|
||||
}
|
||||
|
||||
// A compile-time check to ensure that multiAddrSource implements the AddrSource
|
||||
// interface.
|
||||
var _ AddrSource = (*multiAddrSource)(nil)
|
||||
Reference in new issue
Block a user