From 8fe87c81575f9ae929108507b1e0cb7994a285cc Mon Sep 17 00:00:00 2001 From: Ivan Andreev Date: Tue, 10 Sep 2019 21:16:35 +0300 Subject: [PATCH] mailru: skip extra http request if data fits in hash --- backend/mailru/mailru.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/backend/mailru/mailru.go b/backend/mailru/mailru.go index d25223aff..31820ccc8 100644 --- a/backend/mailru/mailru.go +++ b/backend/mailru/mailru.go @@ -1581,23 +1581,28 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op } var ( - fileBuf []byte - fileHash []byte - newHash []byte + fileBuf []byte + fileHash []byte + newHash []byte + trySpeedup bool ) - // Request hash from source - if srcHash, err := src.Hash(ctx, hash.Mailru); err == nil && srcHash != "" { - fileHash, _ = mrhash.DecodeString(srcHash) - } - - // Try speedup method if it's globally enabled and source hash is available - trySpeedup := o.fs.opt.SpeedupEnable - if trySpeedup && fileHash != nil { - if o.putByHash(ctx, fileHash, src, "source") { - return nil + // Don't disturb the source if file fits in hash. + // Skip an extra speedup request if file fits in hash. + if size > mrhash.Size { + // Request hash from source. + if srcHash, err := src.Hash(ctx, hash.Mailru); err == nil && srcHash != "" { + fileHash, _ = mrhash.DecodeString(srcHash) + } + + // Try speedup if it's globally enabled and source hash is available. + trySpeedup = o.fs.opt.SpeedupEnable + if trySpeedup && fileHash != nil { + if o.putByHash(ctx, fileHash, src, "source") { + return nil + } + trySpeedup = false // speedup failed, force upload } - trySpeedup = false // speedup failed, force upload } // Need to calculate hash, check whether file is still eligible for speedup