Various improvements #17

Merged
Seil0 merged 11 commits from :fix/timeout-resilience into master 2021-10-24 14:38:13 +02:00
1 changed files with 17 additions and 5 deletions
Showing only changes of commit 8e3af696e0 - Show all commits

View File

@ -22,6 +22,8 @@
package org.mosad.thecitadelofricks.hsoparser
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Semaphore
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.mosad.thecitadelofricks.Lesson
@ -36,16 +38,26 @@ class TimetableParser(timetableURL: String? = null, htmlDoc: Document? = null) {
private var logger: org.slf4j.Logger = LoggerFactory.getLogger(TimetableParser::class.java)
private val days = arrayOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
companion object {
val semaphore = Semaphore(3, 0)
}
private val htmlDoc: Document? =
hannesbraun marked this conversation as resolved Outdated
Outdated
Review

please us ?.let { ... } for null safety. Something like this:

private val htmlDoc: Document? = htmlDoc ?: timetableURL?.let {
    runBlocking {
        ...
    }
}
please us `?.let { ... }` for null safety. Something like this: ```kotlin private val htmlDoc: Document? = htmlDoc ?: timetableURL?.let { runBlocking { ... } } ```

This should be fixed with ca14a5d2f6

This should be fixed with ca14a5d2f6
htmlDoc
?: if (timetableURL == null) {
null
} else {
try {
Jsoup.connect(timetableURL).get()
} catch (gex: Exception) {
logger.error("general TimetableParser error", gex)
null
runBlocking {
try {
// Only allow sending a limited amount of requests at the same time
semaphore.acquire()
Jsoup.connect(timetableURL).get()
} catch (gex: Exception) {
logger.error("general TimetableParser error", gex)
null
} finally {
semaphore.release()
}
}
}