Skip to content

Commit

Permalink
fix: Various RDVP releated improvements.
Browse files Browse the repository at this point in the history
Signed-off-by: Jorropo <jorropo.pgm@gmail.com>
  • Loading branch information
Jorropo committed Oct 9, 2020
1 parent 1ab2747 commit f85277c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
10 changes: 7 additions & 3 deletions go/internal/ipfsutil/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
ds "github.com/ipfs/go-datastore"
dsync "github.com/ipfs/go-datastore/sync"
config "github.com/ipfs/go-ipfs-config"
ipfs_cfg "github.com/ipfs/go-ipfs-config"
ipfs_core "github.com/ipfs/go-ipfs/core"
ipfs_coreapi "github.com/ipfs/go-ipfs/core/coreapi"
ipfs_node "github.com/ipfs/go-ipfs/core/node"
Expand All @@ -26,7 +25,7 @@ import (
"berty.tech/berty/v2/go/pkg/errcode"
)

type IpfsConfigPatcher func(*ipfs_cfg.Config) error
type IpfsConfigPatcher func(*config.Config) error

type CoreAPIOption func(context.Context, *ipfs_core.IpfsNode, ipfs_interface.CoreAPI) error

Expand All @@ -50,9 +49,14 @@ type CoreAPIConfig struct {
Options []CoreAPIOption
}

// ChainIpfsConfigPatch will execute multiple IpfsConfigPatcher from the first
// to the last. They can be safely be nilled and will just be ignored.
func ChainIpfsConfigPatch(ps ...IpfsConfigPatcher) IpfsConfigPatcher {
return func(c *ipfs_cfg.Config) error {
return func(c *config.Config) error {
for _, p := range ps {
if p == nil {
continue
}
if err := p(c); err != nil {
return err
}
Expand Down
22 changes: 18 additions & 4 deletions go/internal/ipfsutil/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func ParseAndResolveIpfsAddr(ctx context.Context, addr string) (*peer.AddrInfo,
}

func ParseAndResolveRdvpMaddrs(ctx context.Context, log *zap.Logger, addrs []string) ([]*peer.AddrInfo, error) {
outPeers := make([]*peer.AddrInfo, len(addrs))
// Resolve all addresses
outPeersUnmatched := make([]*peer.AddrInfo, len(addrs))
var errs error
var outLock sync.Mutex
var wg sync.WaitGroup
Expand All @@ -81,14 +82,27 @@ func ParseAndResolveRdvpMaddrs(ctx context.Context, log *zap.Logger, addrs []str
fds[i] = zap.String(key, maddr.String())
}
log.Debug("rdvp peer resolved addrs", fds...)
outLock.Lock()
defer outLock.Unlock()
outPeers[j] = rdvpeer
outPeersUnmatched[j] = rdvpeer
}(i, v)
}
wg.Wait()
if errs != nil {
return nil, errs
}
// Match peers by ID
outPeersMatched := make(map[peer.ID][]ma.Multiaddr)
for _, v := range outPeersUnmatched {
outPeersMatched[v.ID] = append(outPeersMatched[v.ID], v.Addrs...)
}

// Create the ultimate *peer.AddrInfo
var outPeers []*peer.AddrInfo
for id, maddrs := range outPeersMatched {
outPeers = append(outPeers, &peer.AddrInfo{
ID: id,
Addrs: maddrs,
})
}

return outPeers, nil
}

0 comments on commit f85277c

Please sign in to comment.