feat(download): one grab per Soulseek peer, several peers at once
slskd was capped at one transfer per daemon, on the grounds that Soulseek peers punish clients that ask for too much. That politeness is per peer: two different users do not compete for anyone's upload slot. So one slow peer serialised every other Soulseek download behind it. The manager now takes a per-(provider, peer) lock before any slot, so a grab waiting on a busy peer does not hold a provider slot another peer could use, and the slskd default rises to 3, which now counts peers. The help text says so. Running grabs at once exposed the folder collision: slskd names a download's directory after the remote leaf folder, so two peers' "Greatest Hits" (or any two rips' "CD1") share one directory, and collect finds files by name there. Grabs whose local folders overlap now take a package-level lock per folder, in sorted order, keyed on the full path because two clients can share one daemon. Closes #272 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017HJiuc3ZZhxsPXz3ozTirT
This commit is contained in:
1 parent
5e3ac8fb1b
commit
9710c11476
6 files changed
+420
-24
No files matched your search
@@ -59,13 +59,15 @@ const concurrencyKey = "maxConcurrent"
|
||||
// A single global cap is the wrong shape here: usenet and torrent
|
||||
// clients are built to run many transfers at once and are throttled by
|
||||
// bandwidth, while Soulseek transfers come from one person's home
|
||||
// upload slot. Hitting the same peer with parallel requests gets you
|
||||
// queued behind everyone else at best and banned at worst, so slskd is
|
||||
// capped at one — the polite number, and the one that actually
|
||||
// completes fastest, because a Soulseek peer serves one file at a time
|
||||
// regardless of how many you ask for.
|
||||
// upload slot. Politeness there is per *peer* — asking one user for two
|
||||
// folders at once gets you queued behind everyone else at best and
|
||||
// banned at worst — and the manager holds that line separately, one
|
||||
// grab per peer (peerLocks). Two different users do not compete for
|
||||
// anyone's slot, so the daemon-wide number only bounds how many peers
|
||||
// are asked at once, and one slow peer no longer serialises every other
|
||||
// Soulseek download behind it.
|
||||
var kindConcurrency = map[Kind]int{
|
||||
KindSlskd: 1,
|
||||
KindSlskd: 3,
|
||||
KindYtDlp: 2,
|
||||
KindQBittorrent: 4,
|
||||
KindSABnzbd: 4,
|
||||
@@ -155,6 +157,11 @@ type Manager struct {
|
||||
semMu sync.Mutex
|
||||
provSem map[int64]chan struct{}
|
||||
|
||||
// peerLocks holds one grab per Soulseek peer, taken before any
|
||||
// slot: a grab waiting for a busy peer must not sit on a provider
|
||||
// slot another peer could be using.
|
||||
peerLocks keyedLock[peerKey]
|
||||
|
||||
// delegatePoll is how often delegating managers are asked for
|
||||
// status. A field rather than the constant so tests can drive the
|
||||
// full delegate flow without sleeping through it.
|
||||
@@ -790,6 +797,23 @@ func (m *Manager) grab(
|
||||
// whole list for six hours.
|
||||
const maxGrabAttempts = 3
|
||||
|
||||
// peerKey names one Soulseek user on one daemon. The same username on
|
||||
// two daemons is two logins and two queues.
|
||||
type peerKey struct {
|
||||
provider int64
|
||||
peer string
|
||||
}
|
||||
|
||||
// peerKeyFor returns the peer a candidate is fetched from, when the
|
||||
// source is one where asking a peer for two things at once is rude.
|
||||
func peerKeyFor(c Candidate) (peerKey, bool) {
|
||||
if c.Kind != KindSlskd || c.Origin == "" {
|
||||
return peerKey{}, false
|
||||
}
|
||||
|
||||
return peerKey{provider: c.ProviderID, peer: c.Origin}, true
|
||||
}
|
||||
|
||||
// grabOutcome is how one candidate's attempt ended.
|
||||
type grabOutcome struct {
|
||||
item DownloadItem
|
||||
@@ -825,6 +849,15 @@ func (m *Manager) attemptGrab(
|
||||
}
|
||||
|
||||
if !plan.delegated() {
|
||||
if key, ok := peerKeyFor(c); ok {
|
||||
release, err := m.peerLocks.acquire(ctx, key)
|
||||
if err != nil {
|
||||
return grabOutcome{err: err}
|
||||
}
|
||||
|
||||
defer release()
|
||||
}
|
||||
|
||||
provSem := m.semaphoreFor(plan.transportID)
|
||||
|
||||
select {
|
||||
|
||||
Reference in new issue
Block a user