Browse Source

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).
pull/17/head
Hannes Braun 9 months ago
parent
commit
8e3af696e0
Signed by: hannesbraun
GPG Key ID: 7B6557E1DFD685BE
  1. 22
      src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt

22
src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt

@ -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? =
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()
}
}
}

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close