@@ -24,7 +24,6 @@ import (
24
24
"github.com/ipfs/interface-go-ipfs-core/options"
25
25
ipath "github.com/ipfs/interface-go-ipfs-core/path"
26
26
peer "github.com/libp2p/go-libp2p-core/peer"
27
- ma "github.com/multiformats/go-multiaddr"
28
27
)
29
28
30
29
const (
@@ -35,7 +34,7 @@ const (
35
34
type IpfsFetcher struct {
36
35
distPath string
37
36
limit int64
38
- peers []string
37
+ peers []peer. AddrInfo
39
38
40
39
openOnce sync.Once
41
40
openErr error
@@ -51,7 +50,7 @@ type IpfsFetcher struct {
51
50
//
52
51
// Specifying "" for distPath sets the default IPNS path.
53
52
// Specifying 0 for fetchLimit sets the default, -1 means no limit.
54
- func NewIpfsFetcher (distPath string , fetchLimit int64 , peers []string ) * IpfsFetcher {
53
+ func NewIpfsFetcher (distPath string , fetchLimit int64 , peers []peer. AddrInfo ) * IpfsFetcher {
55
54
f := & IpfsFetcher {
56
55
limit : defaultFetchLimit ,
57
56
distPath : migrations .LatestIpfsDist ,
@@ -167,7 +166,7 @@ func initTempNode(ctx context.Context) (string, error) {
167
166
return dir , nil
168
167
}
169
168
170
- func startTempNode (repoDir string , peers []string ) (iface.CoreAPI , func (), error ) {
169
+ func startTempNode (repoDir string , peers []peer. AddrInfo ) (iface.CoreAPI , func (), error ) {
171
170
// Open the repo
172
171
r , err := fsrepo .Open (repoDir )
173
172
if err != nil {
@@ -202,13 +201,9 @@ func startTempNode(repoDir string, peers []string) (iface.CoreAPI, func(), error
202
201
<- node .Context ().Done ()
203
202
}
204
203
204
+ // Parse peer addresses and asynchronously connect to peers
205
205
if len (peers ) != 0 {
206
- // Asynchronously connect to any specified peers
207
- go func () {
208
- if err := connect (ctxIpfsLife , ifaceCore , peers ); err != nil {
209
- fmt .Fprintf (os .Stderr , "failed to connect to peers: %s" , err )
210
- }
211
- }()
206
+ connectPeers (ctxIpfsLife , ifaceCore , peers )
212
207
}
213
208
214
209
return ifaceCore , stopFunc , nil
@@ -269,46 +264,20 @@ func setupPlugins() error {
269
264
return nil
270
265
}
271
266
272
- func connect (ctx context.Context , ipfs iface.CoreAPI , peers []string ) error {
273
- pinfos := make (map [peer.ID ]* peer.AddrInfo , len (peers ))
274
- for _ , addrStr := range peers {
275
- addr , err := ma .NewMultiaddr (addrStr )
276
- if err != nil {
277
- return err
278
- }
279
- pii , err := peer .AddrInfoFromP2pAddr (addr )
280
- if err != nil {
281
- return err
282
- }
283
- pi , ok := pinfos [pii .ID ]
284
- if ! ok {
285
- pi = & peer.AddrInfo {ID : pii .ID }
286
- pinfos [pi .ID ] = pi
287
- }
288
- pi .Addrs = append (pi .Addrs , pii .Addrs ... )
289
- }
290
-
291
- connErrs := make (chan error )
292
- for _ , pi := range pinfos {
293
- go func (pi * peer.AddrInfo ) {
294
- if err := ipfs .Swarm ().Connect (ctx , * pi ); err != nil {
295
- connErrs <- fmt .Errorf ("cound not connec to %q: %s" , pi .ID , err )
296
- } else {
297
- connErrs <- nil
298
- }
299
- }(pi )
300
- }
301
-
302
- var fails []string
303
- for i := 0 ; i < len (pinfos ); i ++ {
304
- err := <- connErrs
305
- if err != nil {
306
- fails = append (fails , err .Error ())
267
+ func connectPeers (ctx context.Context , ipfs iface.CoreAPI , peers []peer.AddrInfo ) {
268
+ // Asynchronously connect to each peer
269
+ //
270
+ // Do not return an error if there is a failure to connect to a peer, since
271
+ // node may still be able to operate. Only write the errors to stderr.
272
+ go func () {
273
+ for i := range peers {
274
+ go func (pi peer.AddrInfo ) {
275
+ if err := ipfs .Swarm ().Connect (ctx , pi ); err != nil {
276
+ fmt .Fprintf (os .Stderr , "cound not connec to %q: %s\n " , pi .ID , err )
277
+ } else {
278
+ fmt .Fprintf (os .Stderr , "conneced to peer %q\n " , pi .ID )
279
+ }
280
+ }(peers [i ])
307
281
}
308
- }
309
- if len (fails ) != 0 {
310
- return fmt .Errorf (strings .Join (fails , ", " ))
311
- }
312
-
313
- return nil
282
+ }()
314
283
}
0 commit comments