Skip to content

Commit 90da3c6

Browse files
committed
Revert "Revert "refactor a bit""
This reverts commit 142b1d4.
1 parent 67ac0ed commit 90da3c6

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

src/main/kotlin/app/GhService.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ object GhService {
1818

1919
private val log = LoggerFactory.getLogger(GhService.javaClass)
2020

21-
// Allows for parallel iteration and O(1) put/remove
22-
private val clientSessions = ConcurrentHashMap<WsSession, Boolean>()
23-
2421
private val tokens = Config.getApiTokens()?.split(",") ?: listOf("") // empty token is limited to 60 requests
2522
private val clients = tokens.map { GitHubClient().apply { setOAuth2Token(it) } }
2623
private val repoServices = clients.map { RepositoryService(it) }
@@ -35,8 +32,9 @@ object GhService {
3532

3633
val remainingRequests: Int get() = clients.sumBy { it.remainingRequests }
3734

35+
// Allows for parallel iteration and O(1) put/remove
36+
private val clientSessions = ConcurrentHashMap<WsSession, Boolean>()
3837
fun registerClient(ws: WsSession) = clientSessions.put(ws, true) == true
39-
4038
fun unregisterClient(ws: WsSession) = clientSessions.remove(ws) == true
4139

4240
init {
@@ -56,13 +54,11 @@ object GhService {
5654

5755
// update all connected clients with remainingRequests twice per second
5856
scheduleAtFixedRate({
59-
val payload = remainingRequests.toString()
6057
clientSessions.forEachKey(1) {
6158
try {
62-
if (it.isOpen)
63-
it.send(payload)
64-
} catch (e: IOException) {
65-
log?.error(e.toString())
59+
it.send(remainingRequests.toString())
60+
} catch (e: Exception) {
61+
log.error(e.toString())
6662
}
6763
}
6864
}, 0, 500, TimeUnit.MILLISECONDS)

src/main/kotlin/app/Main.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fun main(args: Array<String>) {
6161
}
6262

6363
ws("/rate-limit-status") { ws ->
64-
ws.onConnect { GhService.registerClient(it) }
64+
ws.onConnect { session -> GhService.registerClient(session) }
6565
ws.onClose { session, _, _ -> GhService.unregisterClient(session) }
6666
ws.onError { session, _ -> GhService.unregisterClient(session) }
6767
}

0 commit comments

Comments
 (0)