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
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue