Skip to content

refactor: extract readRequestBody and parseBoolQueryParam helpers#745

Open
ericcurtin wants to merge 1 commit intomainfrom
de-deup
Open

refactor: extract readRequestBody and parseBoolQueryParam helpers#745
ericcurtin wants to merge 1 commit intomainfrom
de-deup

Conversation

@ericcurtin
Copy link
Contributor

Deduplicate the MaxBytesReader+ReadAll error-handling block (repeated 4x in scheduling/http_handler.go) into a readRequestBody helper, and the bool query-param parsing block (repeated 2x in models/http_handler.go) into a parseBoolQueryParam helper.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • In Configure, the change from backend, err = h.scheduler.ConfigureRunner(...) to backend, err := ... introduces variable shadowing, so the outer backend value will not be updated after the call; this should be reverted to an assignment (=) to preserve the original behavior.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `Configure`, the change from `backend, err = h.scheduler.ConfigureRunner(...)` to `backend, err := ...` introduces variable shadowing, so the outer `backend` value will not be updated after the call; this should be reverted to an assignment (`=`) to preserve the original behavior.

## Individual Comments

### Comment 1
<location path="pkg/inference/scheduling/http_handler.go" line_range="429" />
<code_context>
 	}

-	backend, err = h.scheduler.ConfigureRunner(r.Context(), backend, configureRequest, r.UserAgent())
+	backend, err := h.scheduler.ConfigureRunner(r.Context(), backend, configureRequest, r.UserAgent())
 	if err != nil {
 		if errors.Is(err, errRunnerAlreadyActive) {
</code_context>
<issue_to_address>
**issue (bug_risk):** Avoid shadowing the outer `backend`/`err` here to prevent subtle bugs

This line used to reassign the existing `backend`/`err`; switching to `:=` creates new inner-scope variables and leaves the outer ones unchanged. Any code after this block will still see the old values, which changes the previous behavior and can introduce hard-to-spot bugs. Unless a new inner scope is intended, please use `backend, err = ...` to match the original semantics.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively refactors duplicated code into two new helper functions, readRequestBody and parseBoolQueryParam, improving code maintainability. The refactoring is well-executed. However, I've identified a potential logic issue in the new parseBoolQueryParam function (which was also present in the original code) regarding the handling of boolean query parameters without explicit values. I've suggested a fix to make the API behavior more intuitive.

Deduplicate the MaxBytesReader+ReadAll error-handling block (repeated 4x
in scheduling/http_handler.go) into a readRequestBody helper, and the
bool query-param parsing block (repeated 2x in models/http_handler.go)
into a parseBoolQueryParam helper.

Signed-off-by: Eric Curtin <eric.curtin@docker.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants