Browse Source

:gear: retry when failed dialing in receiving files

Ettore Di Giacinto 3 years ago
parent
commit
6fdfc87bf6
2 changed files with 5 additions and 2 deletions
  1. 1 1
      pkg/node/options.go
  2. 4 1
      pkg/services/files.go

+ 1 - 1
pkg/node/options.go

@@ -82,7 +82,7 @@ func Handlers(h ...Handler) func(cfg *Config) error {
 	}
 }
 
-// StreamHandlers adds a handler to the list that is called on each received message
+// WithStreamHandler adds a handler to the list that is called on each received message
 func WithStreamHandler(id protocol.ID, h types.StreamHandler) func(cfg *Config) error {
 	return func(cfg *Config) error {
 		cfg.StreamHandlers[id] = h

+ 4 - 1
pkg/services/files.go

@@ -148,8 +148,10 @@ func ReceiveFile(ctx context.Context, ledger *blockchain.Ledger, node types.Node
 				// Open a stream
 				stream, err := node.Host().NewStream(context.Background(), d, protocol.FileProtocol.ID())
 				if err != nil {
-					return err
+					l.Debugf("failed to dial %s, retrying in 5 seconds", d)
+					continue
 				}
+
 				l.Infof("Saving file %s to %s", fileID, path)
 
 				f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
@@ -162,6 +164,7 @@ func ReceiveFile(ctx context.Context, ledger *blockchain.Ledger, node types.Node
 				f.Close()
 
 				l.Infof("Received file %s to %s", fileID, path)
+				return nil
 			}
 		}
 	}