Skip to content

Commit

Permalink
fix(swarm): collect maddr by channel
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Overbool <overbool.xu@gmail.com>
  • Loading branch information
overbool committed Oct 25, 2018
1 parent 8c0fe02 commit 66be4f4
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
inet "gx/ipfs/QmXuRkCR7BNQa9uqfpTiFWsTQLzmTWYg91Ja1w95gnqb6u/go-libp2p-net"
iaddr "gx/ipfs/QmZc5PLgxW61uTPG24TroxHDF6xzgbhZZQf5i53ciQC47Y/go-ipfs-addr"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
madns "gx/ipfs/QmeHJXPqCNzSFbVkYM1uQLuM2L5FyJB9zukQ7EeqRP8ZC9/go-multiaddr-dns"
)

const (
Expand Down Expand Up @@ -520,13 +521,27 @@ func resolveAddresses(addrs []string) ([]ma.Multiaddr, error) {
var wg sync.WaitGroup
resolveErrC := make(chan error, len(addrs))

maddrC := make(chan ma.Multiaddr)
go func() {
for {
select {
case maddr, ok := <-maddrC:
if !ok {
return
}
maddrs = append(maddrs, maddr)
}
}
}()

for _, addr := range addrs {
maddr, err := ma.NewMultiaddr(addr)
if err != nil {
return nil, err
}

// check whether address ends in `ipfs/Qm...`
if _, err := maddr.ValueForProtocol(ma.P_IPFS); err != ma.ErrProtocolNotFound {
if _, last := ma.SplitLast(maddr); last.Protocol().Code == ma.P_IPFS {
maddrs = append(maddrs, maddr)
continue
}
Expand All @@ -546,14 +561,17 @@ func resolveAddresses(addrs []string) ([]ma.Multiaddr, error) {
}
// filter out addresses that still doesn't end in `ipfs/Qm...`
for _, raddr := range raddrs {
if _, err := raddr.ValueForProtocol(ma.P_IPFS); err != ma.ErrProtocolNotFound {
maddrs = append(maddrs, raddr)
if _, last := ma.SplitLast(raddr); last.Protocol().Code == ma.P_IPFS {
maddrC <- raddr
continue
}
}
}(maddr)
}
// wait for address resolving
wg.Wait()
// close the channel of collecting multiaddr
close(maddrC)

select {
case err := <-resolveErrC:
Expand Down

0 comments on commit 66be4f4

Please sign in to comment.