Skip to content

Commit a0882f5

Browse files
only usePermit for download jobs
1 parent c0fd989 commit a0882f5

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/main/kotlin/de/lobbenmeier/stefan/downloadlist/business/DownloadItem.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class DownloadItem(
3636
var videoMetadata: VideoMetadata? = null
3737
try {
3838
ytDlp.runAsync(
39+
false,
3940
// Print the whole object again so we get the filename
4041
"--print",
4142
"$VIDEO_METADATA_JSON_PREFIX%()j",
@@ -90,7 +91,7 @@ class DownloadItem(
9091

9192
fun gatherMetadata() {
9293
CoroutineScope(Dispatchers.IO).launch {
93-
ytDlp.runAsync("-J", "--flat-playlist", url) { metadataJson ->
94+
ytDlp.runAsync(true, "-J", "--flat-playlist", url) { metadataJson ->
9495
val videoMetadata = YtDlpJson.decodeFromString<VideoMetadata>(metadataJson)
9596
metadata.value = videoMetadata
9697

src/main/kotlin/de/lobbenmeier/stefan/downloadlist/business/YtDlp.kt

+14-9
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,19 @@ import de.lobbenmeier.stefan.settings.business.Settings
66
import de.lobbenmeier.stefan.updater.business.getPlatform
77
import de.lobbenmeier.stefan.updater.model.Binaries
88
import kotlin.io.path.pathString
9-
import kotlinx.coroutines.CoroutineScope
10-
import kotlinx.coroutines.Dispatchers
11-
import kotlinx.coroutines.launch
129
import kotlinx.coroutines.sync.Semaphore
1310
import kotlinx.coroutines.sync.withPermit
1411

1512
class YtDlp(private val binaries: Binaries, private val settings: Settings) {
1613

17-
private val semaphore = Semaphore(settings.rateLimit?.toInt() ?: 100)
14+
private val semaphore = Semaphore(settings.maxConcurrentJobs?.toInt() ?: 100)
1815

1916
fun createDownloadItem(url: String): DownloadItem {
2017
return DownloadItem(this, url).also { it.gatherMetadata() }
2118
}
2219

23-
fun run(vararg options: String) {
24-
CoroutineScope(Dispatchers.IO).launch { runAsync(*options) }
25-
}
26-
2720
suspend fun runAsync(
21+
isDownloadJob: Boolean,
2822
vararg options: String,
2923
consumer: suspend (String) -> Unit = { line -> println("process $line") }
3024
) {
@@ -46,7 +40,7 @@ class YtDlp(private val binaries: Binaries, private val settings: Settings) {
4640
val command = arrayOf(ytDlpBinary, *fullOptions).joinToString(separator = " ") { "\"$it\"" }
4741

4842
val res =
49-
semaphore.withPermit {
43+
withPermit(isDownloadJob) {
5044
println("Start process: $command")
5145

5246
process(
@@ -67,4 +61,15 @@ class YtDlp(private val binaries: Binaries, private val settings: Settings) {
6761
throw Exception("yt-dlp indicated error in its response")
6862
}
6963
}
64+
65+
private suspend fun <T> withPermit(
66+
usePermit: Boolean,
67+
action: suspend () -> T,
68+
): T {
69+
return if (usePermit) {
70+
semaphore.withPermit { action() }
71+
} else {
72+
action()
73+
}
74+
}
7075
}

src/main/kotlin/de/lobbenmeier/stefan/ui/App.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fun App() {
3838
@Composable
3939
private fun MainView(settings: Settings, updateSettings: (Settings) -> Unit, binaries: Binaries) {
4040
val ytDlp = remember(settings) { YtDlp(binaries, settings) }
41-
val downloadQueue = remember { DownloadQueue(ytDlp) }
41+
val downloadQueue = remember(ytDlp) { DownloadQueue(ytDlp) }
4242

4343
var settingsOpen by remember { mutableStateOf(false) }
4444

0 commit comments

Comments
 (0)