From 8e3af696e06a5e1f74f5ba951a9aee14e9e94f8d Mon Sep 17 00:00:00 2001 From: Hannes Braun Date: Fri, 15 Oct 2021 20:07:58 +0200 Subject: [PATCH] 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). --- .../hsoparser/TimetableParser.kt | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt b/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt index 39e386c..9d691d5 100644 --- a/src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/TimetableParser.kt +++ b/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() + } } }