Skip to content
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

Enhanced MultiReader #168

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 27 additions & 11 deletions pkg/download/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package download

import (
"context"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -14,6 +15,7 @@ import (

"github.com/replicate/pget/pkg/client"
"github.com/replicate/pget/pkg/logging"
"github.com/replicate/pget/pkg/multireader"
)

const defaultMinChunkSize = 16 * humanize.MiByte
Expand Down Expand Up @@ -65,19 +67,34 @@ type firstReqResult struct {
err error
}

func readBody(reader *multireader.BufferedReader, resp *http.Response) error {
expectedBytes := resp.ContentLength
_ = reader.SetSize(expectedBytes)
n, err := reader.ReadFrom(resp.Body)
if errors.Is(err, io.EOF) {
reader.Err = fmt.Errorf("error reading response for %s: %w", resp.Request.URL.String(), err)
return reader.Err
}
if n != expectedBytes {
reader.Err = fmt.Errorf("downloaded %d bytes instead of %d for %s", n, expectedBytes, resp.Request.URL.String())
return reader.Err
}
return nil
}
Comment on lines +70 to +83
Copy link
Member

Choose a reason for hiding this comment

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

I think I must be missing some important context. Why do we need our own custom type/implementation if this is what we're doing with it. This appears to be reading the entire body into a bytes.Buffer in one go and then checking the length. At that point how is it different from:

func readBody(buf *bytes.Buffer, resp *http.Response) error {
	n, err := buf.ReadFrom(resp.Body)
 	if err != nil {
		return err
	}
	if n != resp.ContentLength {
		return fmt.Errorf("downloaded %d bytes instead of %d for %s", n, expectedBytes, resp.Request.URL.String())
	}
	return nil
}

(By the by, BufferedReader.ReadFrom effectively delegates to bytes.Buffer.ReadFrom, which means it cannot return io.EOF.)

(Oh, and this code also just completely ignores the case where err != nil, which doesn't seem safe.)


func (m *BufferMode) Fetch(ctx context.Context, url string) (io.Reader, int64, error) {
logger := logging.GetLogger()

br := newBufferedReader(m.minChunkSize())
br := multireader.NewBufferedReader(m.minChunkSize())

firstReqResultCh := make(chan firstReqResult)
m.queue.submit(func() {
m.sem.Go(func() error {
defer close(firstReqResultCh)
defer br.done()
defer br.Done()
firstChunkResp, err := m.DoRequest(ctx, 0, m.minChunkSize()-1, url)
if err != nil {
br.err = err
br.Err = err
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused by this. Are you using the BufferedReader's Err field as an indirect signalling mechanism allowing readBody to return that error? If so, that seems... wrong. Why don't we just return an error here?

firstReqResultCh <- firstReqResult{err: err}
return err
}
Expand All @@ -95,8 +112,7 @@ func (m *BufferMode) Fetch(ctx context.Context, url string) (io.Reader, int64, e
return err
}
firstReqResultCh <- firstReqResult{fileSize: fileSize, trueURL: trueURL}

return br.downloadBody(firstChunkResp)
return readBody(br, firstChunkResp)
})
})

Expand Down Expand Up @@ -127,7 +143,7 @@ func (m *BufferMode) Fetch(ctx context.Context, url string) (io.Reader, int64, e
numChunks = m.maxConcurrency()
}

readersCh := make(chan io.Reader, m.maxConcurrency()+1)
readersCh := make(chan *multireader.BufferedReader, m.maxConcurrency()+1)
readersCh <- br

startOffset := m.minChunkSize()
Expand All @@ -153,23 +169,23 @@ func (m *BufferMode) Fetch(ctx context.Context, url string) (io.Reader, int64, e
end = fileSize - 1
}

br := newBufferedReader(end - start + 1)
br := multireader.NewBufferedReader(end - start + 1)
readersCh <- br

m.sem.Go(func() error {
defer br.done()
defer br.Done()
resp, err := m.DoRequest(ctx, start, end, trueURL)
if err != nil {
br.err = err
br.Err = err
return err
}
defer resp.Body.Close()
return br.downloadBody(resp)
return readBody(br, resp)
})
}
})

return newChanMultiReader(readersCh), fileSize, nil
return multireader.NewMultiReader(readersCh), fileSize, nil
}

func (m *BufferMode) DoRequest(ctx context.Context, start, end int64, trueURL string) (*http.Response, error) {
Expand Down
55 changes: 0 additions & 55 deletions pkg/download/buffered_reader.go

This file was deleted.

42 changes: 0 additions & 42 deletions pkg/download/chan_multi_reader.go

This file was deleted.

21 changes: 11 additions & 10 deletions pkg/download/consistent_hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/replicate/pget/pkg/config"
"github.com/replicate/pget/pkg/consistent"
"github.com/replicate/pget/pkg/logging"
"github.com/replicate/pget/pkg/multireader"
)

type ConsistentHashingMode struct {
Expand Down Expand Up @@ -104,15 +105,15 @@ func (m *ConsistentHashingMode) Fetch(ctx context.Context, urlString string) (io
return m.FallbackStrategy.Fetch(ctx, urlString)
}

br := newBufferedReader(m.minChunkSize())
br := multireader.NewBufferedReader(m.minChunkSize())
firstReqResultCh := make(chan firstReqResult)
m.queue.submit(func() {
m.sem.Go(func() error {
defer close(firstReqResultCh)
defer br.done()
defer br.Done()
firstChunkResp, err := m.DoRequest(ctx, 0, m.minChunkSize()-1, urlString)
if err != nil {
br.err = err
br.Err = err
firstReqResultCh <- firstReqResult{err: err}
return err
}
Expand All @@ -125,7 +126,7 @@ func (m *ConsistentHashingMode) Fetch(ctx context.Context, urlString string) (io
}
firstReqResultCh <- firstReqResult{fileSize: fileSize}

return br.downloadBody(firstChunkResp)
return readBody(br, firstChunkResp)
})
})
firstReqResult, ok := <-firstReqResultCh
Expand Down Expand Up @@ -173,7 +174,7 @@ func (m *ConsistentHashingMode) Fetch(ctx context.Context, urlString string) (io
chunksPerSlice = append([]int64{0}, EqualSplit(int64(concurrency), totalSlices-1)...)
}

readersCh := make(chan io.Reader, m.maxConcurrency()+1)
readersCh := make(chan *multireader.BufferedReader, m.maxConcurrency()+1)
readersCh <- br

logger.Debug().Str("url", urlString).
Expand Down Expand Up @@ -213,10 +214,10 @@ func (m *ConsistentHashingMode) Fetch(ctx context.Context, urlString string) (io
chunkStart := startFrom
chunkEnd := startFrom + chunkSize - 1

br := newBufferedReader(chunkSize)
br := multireader.NewBufferedReader(chunkSize)
readersCh <- br
m.sem.Go(func() error {
defer br.done()
defer br.Done()
logger.Debug().Int64("start", chunkStart).Int64("end", chunkEnd).Msg("starting request")
resp, err := m.DoRequest(ctx, chunkStart, chunkEnd, urlString)
if err != nil {
Expand All @@ -233,20 +234,20 @@ func (m *ConsistentHashingMode) Fetch(ctx context.Context, urlString string) (io
resp, err = m.FallbackStrategy.DoRequest(ctx, chunkStart, chunkEnd, urlString)
}
if err != nil {
br.err = err
br.Err = err
return err
}
}
defer resp.Body.Close()
return br.downloadBody(resp)
return readBody(br, resp)
})

startFrom = startFrom + chunkSize
}
}
})

return newChanMultiReader(readersCh), fileSize, nil
return multireader.NewMultiReader(readersCh), fileSize, nil
}

func (m *ConsistentHashingMode) DoRequest(ctx context.Context, start, end int64, urlString string) (*http.Response, error) {
Expand Down
Loading