Skip to content

Commit 6a9bfcf

Browse files
xrgzsGitHub Copilot
andcommitted
fix(drivers/139): move GetSectionReader outside retry loop
- Move ss.GetSectionReader() before retry.Do() so the sequential stream section reader is only called once per chunk - Remove redundant ss.FreeSectionReader() calls from inside the retry closure since the reader is now owned by the outer scope - Fixes 'stream not cached' errors when retrying failed chunk uploads during cross-storage WebDAV COPY operations Co-authored-by: GitHub Copilot <copilot@github.com>
1 parent e75deae commit 6a9bfcf

2 files changed

Lines changed: 11 additions & 25 deletions

File tree

drivers/139/driver.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -902,22 +902,19 @@ func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
902902
start := i * partSize
903903
byteSize := min(size-start, partSize)
904904

905-
var rd io.ReadSeeker
905+
rd, getErr := ss.GetSectionReader(start, byteSize)
906+
if getErr != nil {
907+
return getErr
908+
}
909+
906910
err = retry.Do(
907911
func() error {
908-
var getErr error
909-
rd, getErr = ss.GetSectionReader(start, byteSize)
910-
if getErr != nil {
911-
return getErr
912-
}
913912
if _, err := rd.Seek(0, io.SeekStart); err != nil {
914-
ss.FreeSectionReader(rd)
915913
return err
916914
}
917915
req, reqErr := http.NewRequestWithContext(ctx, http.MethodPost, resp.Data.UploadResult.RedirectionURL,
918916
io.TeeReader(rd, p))
919917
if reqErr != nil {
920-
ss.FreeSectionReader(rd)
921918
return reqErr
922919
}
923920
req.Header.Set("Content-Type", "text/plain;name="+unicode(stream.GetName()))
@@ -929,27 +926,22 @@ func (d *Yun139) Put(ctx context.Context, dstDir model.Obj, stream model.FileStr
929926

930927
res, doErr := base.HttpClient.Do(req)
931928
if doErr != nil {
932-
ss.FreeSectionReader(rd)
933929
return doErr
934930
}
935931
defer res.Body.Close()
936932
bodyBytes, readErr := io.ReadAll(res.Body)
937933
if readErr != nil {
938-
ss.FreeSectionReader(rd)
939934
return fmt.Errorf("error reading response body: %v", readErr)
940935
}
941936
if res.StatusCode != http.StatusOK {
942-
ss.FreeSectionReader(rd)
943937
return fmt.Errorf("unexpected status code: %d, body: %s", res.StatusCode, string(bodyBytes))
944938
}
945939
var result InterLayerUploadResult
946940
xmlErr := xml.Unmarshal(bodyBytes, &result)
947941
if xmlErr != nil {
948-
ss.FreeSectionReader(rd)
949942
return fmt.Errorf("error parsing XML: %v", xmlErr)
950943
}
951944
if result.ResultCode != 0 {
952-
ss.FreeSectionReader(rd)
953945
return fmt.Errorf("upload failed with result code: %d, message: %s", result.ResultCode, result.Msg)
954946
}
955947
return nil

drivers/139/util.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -716,22 +716,18 @@ func (d *Yun139) uploadPersonalParts(ctx context.Context, partInfos []PartInfo,
716716
offset := partInfos[index].ParallelHashCtx.PartOffset
717717
log.Debugf("[139] uploading part %+v/%+v", index, len(partInfos))
718718

719-
var rd io.ReadSeeker
719+
rd, getErr := ss.GetSectionReader(offset, partSize)
720+
if getErr != nil {
721+
return getErr
722+
}
723+
720724
err := retry.Do(
721725
func() error {
722-
var getErr error
723-
rd, getErr = ss.GetSectionReader(offset, partSize)
724-
if getErr != nil {
725-
return getErr
726-
}
727-
_, seekErr := rd.Seek(0, io.SeekStart)
728-
if seekErr != nil {
729-
ss.FreeSectionReader(rd)
726+
if _, seekErr := rd.Seek(0, io.SeekStart); seekErr != nil {
730727
return seekErr
731728
}
732729
req, reqErr := http.NewRequestWithContext(ctx, http.MethodPut, uploadPartInfo.UploadUrl, io.TeeReader(rd, p))
733730
if reqErr != nil {
734-
ss.FreeSectionReader(rd)
735731
return reqErr
736732
}
737733
req.Header.Set("Content-Type", "application/octet-stream")
@@ -742,14 +738,12 @@ func (d *Yun139) uploadPersonalParts(ctx context.Context, partInfos []PartInfo,
742738

743739
res, doErr := base.HttpClient.Do(req)
744740
if doErr != nil {
745-
ss.FreeSectionReader(rd)
746741
return doErr
747742
}
748743
defer res.Body.Close()
749744
log.Debugf("[139] uploaded: %+v", res)
750745
if res.StatusCode != http.StatusOK {
751746
body, _ := io.ReadAll(res.Body)
752-
ss.FreeSectionReader(rd)
753747
return fmt.Errorf("unexpected status code: %d, body: %s", res.StatusCode, string(body))
754748
}
755749
return nil

0 commit comments

Comments
 (0)