Limit sending timetable requests in parallel to 3

Otherwise, the requests may fail (from my experience). Also we don't want to look suspicious (flooding their server with tons of requests at the same time).
This commit is contained in:
Hannes Braun 2021-10-15 20:07:58 +02:00
parent fb6291792d
commit 8e3af696e0
Signed by: hannesbraun
GPG Key ID: 7B6557E1DFD685BE
1 changed files with 17 additions and 5 deletions

View File

@ -22,6 +22,8 @@
package org.mosad.thecitadelofricks.hsoparser package org.mosad.thecitadelofricks.hsoparser
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Semaphore
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.jsoup.nodes.Document import org.jsoup.nodes.Document
import org.mosad.thecitadelofricks.Lesson 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 var logger: org.slf4j.Logger = LoggerFactory.getLogger(TimetableParser::class.java)
private val days = arrayOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") private val days = arrayOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
companion object {
val semaphore = Semaphore(3, 0)
}
private val htmlDoc: Document? = private val htmlDoc: Document? =
htmlDoc htmlDoc
?: if (timetableURL == null) { ?: if (timetableURL == null) {
null null
} else { } else {
try { runBlocking {
Jsoup.connect(timetableURL).get() try {
} catch (gex: Exception) { // Only allow sending a limited amount of requests at the same time
logger.error("general TimetableParser error", gex) semaphore.acquire()
null Jsoup.connect(timetableURL).get()
} catch (gex: Exception) {
logger.error("general TimetableParser error", gex)
null
} finally {
semaphore.release()
}
} }
} }