Skip to content

feat: try to get content length with zero copy #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,26 +423,9 @@ func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) (err
return err
}

contentLength := int64(0)
if seeker, ok := stream.(io.Seeker); ok {
contentLength, err = seeker.Seek(0, io.SeekEnd)
if err != nil {
return err
}

_, err = seeker.Seek(0, io.SeekStart)
if err != nil {
return err
}
} else {
buffer := bytes.NewBuffer(make([]byte, 0, 1024*1024 /* 1MB */))

contentLength, err = io.Copy(buffer, stream)
if err != nil {
return err
}

stream = buffer
contentLength, err := GetContentLength(stream)
if err != nil {
return err
}

s, err := c.put(path, stream, contentLength)
Expand Down
26 changes: 26 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,29 @@ func (l *limitedReadCloser) Read(buf []byte) (int, error) {
func (l *limitedReadCloser) Close() error {
return l.rc.Close()
}

func GetContentLength(reader io.Reader) (int64, error) {
contentLength := int64(0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zero is a valid length for some transfers, this should probably return -1 to indicate the length is unknown or return additional bool

switch reader := reader.(type) {
case *bytes.Buffer:
contentLength = int64(reader.Len())
case *bytes.Reader:
contentLength = int64(reader.Len())
case *strings.Reader:
contentLength = int64(reader.Len())
case io.Seeker:
pos, err := reader.Seek(0, io.SeekEnd)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is probably slightly incorrect as the reader could be at a position other than zero:

I think you need to:

  1. startPos, err = reader.Seek(0, io.SeekCurrent) to determine the current position
  2. totalLength, err = reader.Seek(0, io.SeekEnd) to determine the length entire stream
  3. reader.Seek(0, startPos) to restore the original starting position.
  4. return totalLength - startPos - this is how many bytes are remaining

if err != nil {
return 0, err
}

_, err = reader.Seek(0, io.SeekStart)
if err != nil {
return 0, err
}

contentLength = pos
}

return contentLength, nil
}
Loading