Repository navigation
Add alternative speed-limit (turtle) APIs - #41
Conversation
Notifiarr needs turtle-mode on/off when someone is watching remotely; qBit only exposes get+toggle. Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
🟡 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.
…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>
|
@qwen-pr-bot can you check this out? |
There was a problem hiding this comment.
🟡 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
|
|
||
| // ToggleSpeedLimitsModeContext toggles alternative (turtle) speed limits on or off. | ||
| func (q *Qbit) ToggleSpeedLimitsModeContext(ctx context.Context) error { | ||
| q.speedLimits.Lock() |
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| // SetSpeedLimitsMode enables or disables alternative (turtle) speed limits. | ||
| // qBittorrent has no set-mode endpoint, so this gets the current mode and toggles if needed. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>

Summary
SpeedLimitsMode,ToggleSpeedLimitsMode, andSetSpeedLimitsMode(get then toggle; qBit has no set-mode endpoint).TestGetXfers/TestSetTorrentCategory.Needed by Notifiarr #1212 to turn turtle mode on while someone is watching Plex/Jellyfin remotely.
Test plan
go test ./...SpeedLimitsModethenSetSpeedLimitsMode(true)thenSetSpeedLimitsMode(false)restores.Made with Cursor