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:
parent
fb6291792d
commit
8e3af696e0
|
@ -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 {
|
||||
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…
Reference in New Issue