Skip to content

Commit

Permalink
Wait until SiteManager is initialized before trying to access siteRes…
Browse files Browse the repository at this point in the history
…olver from CloudFlareHandlerInterceptor.
  • Loading branch information
K1rakishou committed Mar 26, 2024
1 parent ebb8edb commit aced639
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class CloudFlareHandlerInterceptor(
synchronized(this) { sitesThatRequireCloudFlareCache.add(host) }

if (canShowCloudFlareBypassScreen(retrying, request)) {
siteResolver.waitUntilInitialized()

val site = siteResolver.findSiteForUrl(request.url.toString())
if (site != null) {
val siteDescriptor = site.siteDescriptor()
Expand Down Expand Up @@ -176,6 +178,8 @@ class CloudFlareHandlerInterceptor(
return true
}

siteResolver.waitUntilInitialized()

val url = request.url
val site = siteResolver.findSiteForUrl(url.toString())

Expand Down Expand Up @@ -204,6 +208,8 @@ class CloudFlareHandlerInterceptor(

private fun addCloudFlareCookie(prevRequest: Request): Request? {
val url = prevRequest.url

siteResolver.waitUntilInitialized()
val site = siteResolver.findSiteForUrl(url.toString())

val domainOrHost = prevRequest.url.domain()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import java.io.File
import java.net.InetSocketAddress
import java.net.Proxy
import java.net.URI
import java.util.concurrent.CountDownLatch
import java.util.concurrent.atomic.AtomicBoolean

@Suppress("BlockingMethodInNonBlockingContext")
Expand Down Expand Up @@ -238,10 +237,7 @@ class ProxyStorage(
return
}

val countDownLatch = CountDownLatch(1)
siteResolver.runWhenInitialized { countDownLatch.countDown() }
countDownLatch.await()

siteResolver.waitUntilInitialized()
dependenciesInitialized.set(true)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
/*
* KurobaEx - *chan browser https://github.com/K1rakishou/Kuroba-Experimental/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.github.k1rakishou.chan.core.site

import com.github.k1rakishou.chan.core.manager.SiteManager
import com.github.k1rakishou.model.data.descriptor.ChanDescriptor
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import java.util.concurrent.CountDownLatch
import javax.inject.Inject

open class SiteResolver @Inject constructor(
private val siteManager: SiteManager
) {

fun runWhenInitialized(func: (Throwable?) -> Unit) {
siteManager.runWhenInitialized(func)
fun waitUntilInitialized() {
if (siteManager.isReady()) {
return
}

val countDownLatch = CountDownLatch(1)

siteManager.runWhenInitialized {
countDownLatch.countDown()
}

countDownLatch.await()
}

fun isInitialized(): Boolean = siteManager.isReady()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.withContext
import java.util.concurrent.atomic.AtomicReference
import kotlin.time.ExperimentalTime

/**
* A super useful class for cases when you want to initialize something in a class (that may take
Expand Down Expand Up @@ -61,7 +60,6 @@ class SuspendableInitializer<T> @JvmOverloads constructor(
}
}

@OptIn(ExperimentalTime::class)
suspend fun awaitUntilInitialized() {
if (value.isCompleted) {
val throwable = error.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.github.k1rakishou.model.data.site.ChanSiteData
import com.github.k1rakishou.model.source.local.SiteLocalSource
import com.github.k1rakishou.model.util.ensureBackgroundThread
import kotlinx.coroutines.CoroutineScope
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime
import kotlin.time.measureTimedValue

Expand Down Expand Up @@ -42,7 +41,6 @@ class SiteRepository(
}
}

@OptIn(ExperimentalTime::class)
suspend fun persist(chanSiteDataList: Collection<ChanSiteData>): ModularResult<Unit> {
check(allSitesLoadedInitializer.isInitialized()) { "SiteRepository is not initialized" }
Logger.d(TAG, "persist(chanSiteDataListCount=${chanSiteDataList.size})")
Expand Down

0 comments on commit aced639

Please sign in to comment.