Skip to content

Add alternative speed-limit (turtle) APIs - #41

Merged
davidnewhall merged 4 commits into
mainfrom
dn2_speed_limits
Nov 24, 2026
Merged

davidnewhall merged 4 commits into
mainfrom
dn2_speed_limits

Conversation

@davidnewhall

Copy link
Copy Markdown
Contributor

Summary

  • Add SpeedLimitsMode, ToggleSpeedLimitsMode, and SetSpeedLimitsMode (get then toggle; qBit has no set-mode endpoint).
  • Cover the new endpoints with httptest like TestGetXfers / TestSetTorrentCategory.

Needed by Notifiarr #1212 to turn turtle mode on while someone is watching Plex/Jellyfin remotely.

Test plan

  • go test ./...
  • Optional: against a live qBit, SpeedLimitsMode then SetSpeedLimitsMode(true) then SetSpeedLimitsMode(false) restores.

Made with Cursor

Notifiarr needs turtle-mode on/off when someone is watching remotely; qBit only exposes get+toggle.

Co-authored-by: Cursor <cursoragent@cursor.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The set operation can return success with the wrong final state under concurrent calls.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds qBittorrent turtle-mode APIs needed for remote-stream throttling.

Changes:

  • Adds get, toggle, and set-style speed-limit methods.
  • Adds HTTP endpoint tests.
  • Documents turtle-mode support.
File summaries
File Description
README.md Documents speed-limit support.
qbit.go Implements turtle-mode APIs.
qbit_test.go Tests new endpoint behavior.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread qbit.go Resolved
…flip.

qBittorrent only exposes toggle; two SetSpeedLimitsMode calls that both read the same mode would both toggle and leave the opposite state.

Co-authored-by: Cursor <cursoragent@cursor.com>
@davidnewhall

Copy link
Copy Markdown
Contributor Author

@qwen-pr-bot can you check this out?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Both speed-limit context methods can remain blocked on the mutex after cancellation or deadline expiry, requiring context-aware serialization and a cancellation-under-contention test.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread qbit.go Outdated

// ToggleSpeedLimitsModeContext toggles alternative (turtle) speed limits on or off.
func (q *Qbit) ToggleSpeedLimitsModeContext(ctx context.Context) error {
q.speedLimits.Lock()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not adding a context-aware lock. That wait only existed to serialize get-then-toggle, and qBittorrent already has a set endpoint. SetSpeedLimitsModeContext and ToggleSpeedLimitsModeContext are single postReq calls, so a canceled context fails in the HTTP client instead of sitting on a mutex. df12505.

Comment thread qbit.go Outdated
}

// SetSpeedLimitsMode enables or disables alternative (turtle) speed limits.
// qBittorrent has no set-mode endpoint, so this gets the current mode and toggles if needed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This premise is wrong. qBittorrent added POST /api/v2/transfer/setSpeedLimitsMode?mode=... in release-4.5.0 (API 2.8.14, qbittorrent/qBittorrent@7d979057 "WebAPI: Add endpoint to set speed limit mode"), back to back with speedLimitsMode and toggleSpeedLimitsMode. It takes mode, where any non-zero value means alternative, and sets it directly server-side. That makes the setter atomic and lets us drop the whole get-then-toggle plus the mutex. Details in the overall review.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Confirmed TransferController::setSpeedLimitsModeAction in current qBittorrent: POST /api/v2/transfer/setSpeedLimitsMode with mode, non-zero means alternative. SetSpeedLimitsModeContext is that one POST now (qBittorrent 4.5+).

Dropped the mutex, the private toggle helper, and TestSetSpeedLimitsModeConcurrent. GET and toggle stay. df12505.

@qwen-pr-bot qwen-pr-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Requesting changes on one point: the premise for SetSpeedLimitsMode is wrong, and the fix is simpler than the mutex you added.

Reviewed e07152508 against 66698525 (qbit.go, qbit_test.go, README.md; commits 21e1eac and e9ed350). The SpeedLimitsMode GET and ToggleSpeedLimitsMode endpoints match the qBittorrent WebAPI source exactly, the README update is accurate, and the e9ed350 mutex fix is sound — concurrent setters through one client really did double-flip before it, and no longer do. That part of the conversation with Copilot is settled.

The problem: both doc comments claim "qBittorrent has no set-mode endpoint," but it does. The qBittorrent source has setSpeedLimitsModeAction, added in release-4.5.0 (API 2.8.14, qbittorrent/qBittorrent@7d979057 "WebAPI: Add endpoint to set speed limit mode"), in the same window as the get and toggle endpoints:

void TransferController::setSpeedLimitsModeAction()
{
    requireParams({u"mode"_s});
    const std::optional<int> mode = Utils::String::parseInt(params().value(u"mode"_s));
    if (!mode) throw APIError(APIErrorType::BadParams, tr("'mode': invalid argument"));
    BitTorrent::Session::instance()->setAltGlobalSpeedLimitEnabled(mode != 0); // non-zero = turtle
}

So SetSpeedLimitsModeContext should be a single atomic POST:

func (q *Qbit) SetSpeedLimitsModeContext(ctx context.Context, enabled bool) error {
	values := url.Values{}
	if enabled {
		values.Set("mode", "1")
	} else {
		values.Set("mode", "0")
	}

	return q.postReq(ctx, "api/v2/transfer/setSpeedLimitsMode", values, nil)
}

Why it matters beyond tidiness: the mutex in e9ed350 only serializes calls made through the same Qbit instance. Notifiarr's use case is turtle mode while someone is watching remotely, and the people toggling the same qBittorrent box can just as easily be a second app or a person, or the user clicking the WebUI themselves. Get-then-toggle can return nil while leaving the mode opposite to what was requested whenever any of those land between the GET and the POST; a direct set cannot. The mutex, the private toggleSpeedLimitsMode helper, and TestSetSpeedLimitsModeConcurrent all exist to paper over a race that disappears with the real endpoint. Keep the GET and toggle methods, they are genuinely useful, but drop the serialization machinery.

One non-blocking nit: TestSetSpeedLimitsMode reads and writes toggles/mode from the httptest handler goroutine without a lock. That is safe today only because the test issues calls sequentially; the mutex pattern from the concurrent test is the shape to copy if you ever parallelize it.

Executed validation: go test ./... and go test -race ./... pass at e07152508 (0.04s / 1.1s); go test -race -count=5 -run TestSetSpeedLimitsModeConcurrent passes 5/5 at head but fails against 21e1eac, where 20 concurrent setters produced 17 toggles, confirming the pre-fix double-flip Copilot flagged. gofmt -l and go vet ./... are clean. Endpoint semantics verified against qbittorrent/qBittorrent master src/webui/api/transfercontroller.cpp and commit 7d979057, which the compare API places inside release-4.5.0.

Get-then-toggle only serialized one client and could still lose to the WebUI. qBittorrent 4.5 sets the mode in one POST, so the mutex is unnecessary.

Co-authored-by: Cursor <cursoragent@cursor.com>
@davidnewhall
davidnewhall merged commit f2e0117 into main Nov 24, 2026
9 checks passed
@davidnewhall
davidnewhall deleted the dn2_speed_limits branch November 24, 2026 02:24
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