Skip to content

Commit 726c12e

Browse files
committed
Only throttle YouTube feed loading
1 parent 33b96d2 commit 726c12e

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadManager.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.schabi.newpipe.database.subscription.NotificationMode
1717
import org.schabi.newpipe.database.subscription.SubscriptionEntity
1818
import org.schabi.newpipe.extractor.Info
1919
import org.schabi.newpipe.extractor.NewPipe
20+
import org.schabi.newpipe.extractor.ServiceList
2021
import org.schabi.newpipe.extractor.feed.FeedInfo
2122
import org.schabi.newpipe.extractor.stream.StreamInfoItem
2223
import org.schabi.newpipe.ktx.getStringSafe
@@ -90,9 +91,9 @@ class FeedLoadManager(private val context: Context) {
9091
else -> feedDatabaseManager.outdatedSubscriptionsForGroup(groupId, outdatedThreshold)
9192
}
9293

93-
// like `currentProgress`, but counts the number of extractions that have begun, so they
94-
// can be properly throttled every once in a while (see doOnNext below)
95-
val extractionCount = AtomicInteger()
94+
// like `currentProgress`, but counts the number of YouTube extractions that have begun, so
95+
// they can be properly throttled every once in a while (see doOnNext below)
96+
val youtubeExtractionCount = AtomicInteger()
9697

9798
return outdatedSubscriptions
9899
.take(1)
@@ -109,11 +110,13 @@ class FeedLoadManager(private val context: Context) {
109110
.observeOn(Schedulers.io())
110111
.flatMap { Flowable.fromIterable(it) }
111112
.takeWhile { !cancelSignal.get() }
112-
.doOnNext {
113-
// throttle extractions once every BATCH_SIZE to avoid being throttled
114-
val previousCount = extractionCount.getAndIncrement()
115-
if (previousCount != 0 && previousCount % BATCH_SIZE == 0) {
116-
Thread.sleep(DELAY_BETWEEN_BATCHES_MILLIS.random())
113+
.doOnNext { subscriptionEntity ->
114+
// throttle YouTube extractions once every BATCH_SIZE to avoid being rate limited
115+
if (subscriptionEntity.serviceId == ServiceList.YouTube.serviceId) {
116+
val previousCount = youtubeExtractionCount.getAndIncrement()
117+
if (previousCount != 0 && previousCount % BATCH_SIZE == 0) {
118+
Thread.sleep(DELAY_BETWEEN_BATCHES_MILLIS.random())
119+
}
117120
}
118121
}
119122
.parallel(PARALLEL_EXTRACTIONS, PARALLEL_EXTRACTIONS * 2)
@@ -342,14 +345,14 @@ class FeedLoadManager(private val context: Context) {
342345
private const val PARALLEL_EXTRACTIONS = 3
343346

344347
/**
345-
* How many extractions to perform before waiting [DELAY_BETWEEN_BATCHES_MILLIS] to avoid
346-
* being rate limited
348+
* How many YouTube extractions to perform before waiting [DELAY_BETWEEN_BATCHES_MILLIS]
349+
* to avoid being rate limited
347350
*/
348351
private const val BATCH_SIZE = 50
349352

350353
/**
351-
* Wait a random delay in this range once every [BATCH_SIZE] extractions to avoid being
352-
* rate limited
354+
* Wait a random delay in this range once every [BATCH_SIZE] YouTube extractions to avoid
355+
* being rate limited
353356
*/
354357
private val DELAY_BETWEEN_BATCHES_MILLIS = (6000L..12000L)
355358

0 commit comments

Comments
 (0)