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

[refactor] - Add DataOrErr #3520

Merged
merged 8 commits into from
Nov 16, 2024
Merged

[refactor] - Add DataOrErr #3520

merged 8 commits into from
Nov 16, 2024

Conversation

ahrav
Copy link
Collaborator

@ahrav ahrav commented Oct 28, 2024

Description:

This PR addresses the second point in this comment.

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

@ahrav ahrav requested a review from a team October 28, 2024 18:51
@ahrav ahrav marked this pull request as ready for review November 13, 2024 20:22
@ahrav ahrav requested a review from a team as a code owner November 13, 2024 20:22
@ahrav ahrav requested a review from a team November 13, 2024 20:22
Copy link
Collaborator

@mcastorina mcastorina left a comment

Choose a reason for hiding this comment

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

I'm still grumpy we can't call it Result, but I'll live I guess

Comment on lines 55 to 61
func (h *defaultHandler) measureLatencyAndHandleErrors(start time.Time, err error) {
func (h *defaultHandler) measureLatencyAndHandleErrors(
ctx logContext.Context,
start time.Time,
err error,
dataErrChan chan DataOrErr,
) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not super familiar with this area of the codebase, but it's starting to smell like this method is doing a little too much.

Am I reading this right, that we're now writing to the dataErrChan instead of just recording metrics? (side nit: make dataErrChan a write-only parameter).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree. I can create a follow-up PR to break this function into smaller parts. You're correct—we need to pass all errors to dataErrChan instead of just logging them and moving on, as we did before.

Comment on lines 83 to 108
func newFileReader(r io.Reader) (fileReader, error) {
var fReader fileReader

// The caller is responsible for closing the reader when it is no longer needed.
func newFileReader(r io.Reader) (fReader fileReader, err error) {
// To detect the MIME type of the input data, we need a reader that supports seeking.
// This allows us to read the data multiple times if necessary without losing the original position.
// We use a BufferedReaderSeeker to wrap the original reader, enabling this functionality.
fReader.BufferedReadSeeker = iobuf.NewBufferedReaderSeeker(r)

mime, err := mimetype.DetectReader(fReader)
// If an error occurs during MIME type detection, it is important we close the BufferedReaderSeeker
// to release any resources it holds (checked out buffers or temp file).
defer func() {
if err != nil {
if closeErr := fReader.Close(); closeErr != nil {
err = fmt.Errorf("%w; error closing reader: %w", err, closeErr)
}
}
}()

var mime *mimetype.MIME
mime, err = mimetype.DetectReader(fReader)
if err != nil {
return fReader, fmt.Errorf("unable to detect MIME type: %w", err)
}
fReader.mime = mime

// Reset the reader to the beginning because DetectReader consumes the reader.
if _, err := fReader.Seek(0, io.SeekStart); err != nil {
if _, err = fReader.Seek(0, io.SeekStart); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

These changes look unrelated to DataOrErr, were they accidentally added to this PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, yes—these changes were part of an earlier PR that I split into three separate PRs as requested. This felt like the most appropriate PR to include the change originally in the initial PR."

Comment on lines +269 to +272
if br.buf == nil {
br.buf = br.bufPool.Get()
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same question here about this being unrelated to DataOrErr.

Copy link
Contributor

@dustin-decker dustin-decker left a comment

Choose a reason for hiding this comment

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

Nice improvement

Base automatically changed from refactor-handlers-error-handling to main November 15, 2024 22:59
@ahrav ahrav merged commit aeaebdd into main Nov 16, 2024
13 checks passed
@ahrav ahrav deleted the refactor-data-or-err branch November 16, 2024 02:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants