From 21c6737b10004117a111092aa227dd5680518e2a Mon Sep 17 00:00:00 2001 From: Ivan Fraixedes Date: Wed, 16 Oct 2019 18:05:22 +0200 Subject: [PATCH] uplink/ecclient: clarify defer logic in putPiece (#3247) Refactoring the 'defer' function logic to just only have what's important to not forget before returning but simplifying its logic for making easy to understand the overall function logic. --- uplink/ecclient/client.go | 46 ++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/uplink/ecclient/client.go b/uplink/ecclient/client.go index ff756b2b0..3cf248fd8 100644 --- a/uplink/ecclient/client.go +++ b/uplink/ecclient/client.go @@ -213,42 +213,44 @@ func (ec *ecClient) putPiece(ctx, parent context.Context, limit *pb.AddressedOrd ) return nil, err } + defer func() { - if ctx.Err() != nil || err != nil { - hash = nil + if err != nil { err = errs.Combine(err, upload.Cancel(ctx)) return } - h, closeErr := upload.Commit(ctx) - hash = h - err = errs.Combine(err, closeErr) + + hash, err = upload.Commit(ctx) }() _, err = sync2.Copy(ctx, upload, data) // Canceled context means the piece upload was interrupted by user or due // to slow connection. No error logging for this case. - if ctx.Err() == context.Canceled { - if parent.Err() == context.Canceled { - ec.log.Info("Upload to node canceled by user", zap.String("NodeID", storageNodeID.String())) + if err != nil { + if errs2.IsCanceled(err) { + if parent.Err() == context.Canceled { + ec.log.Info("Upload to node canceled by user", zap.Stringer("NodeID", storageNodeID)) + } else { + ec.log.Debug("Node cut from upload due to slow connection", zap.Stringer("NodeID", storageNodeID)) + } } else { - ec.log.Debug("Node cut from upload due to slow connection", zap.String("NodeID", storageNodeID.String())) - } - err = context.Canceled - } else if err != nil { - nodeAddress := "nil" - if limit.GetStorageNodeAddress() != nil { - nodeAddress = limit.GetStorageNodeAddress().GetAddress() + nodeAddress := "" + if limit.GetStorageNodeAddress() != nil { + nodeAddress = limit.GetStorageNodeAddress().GetAddress() + } + + ec.log.Debug("Failed uploading piece to node", + zap.Stringer("PieceID", pieceID), + zap.Stringer("NodeID", storageNodeID), + zap.String("Node Address", nodeAddress), + zap.Error(err), + ) } - ec.log.Debug("Failed uploading piece to node", - zap.String("PieceID", pieceID.String()), - zap.String("NodeID", storageNodeID.String()), - zap.String("Node Address", nodeAddress), - zap.Error(err), - ) + return nil, err } - return hash, err + return nil, nil } func (ec *ecClient) Get(ctx context.Context, limits []*pb.AddressedOrderLimit, privateKey storj.PiecePrivateKey, es eestream.ErasureScheme, size int64) (rr ranger.Ranger, err error) {