Skip to content

Commit 142b1d4

Browse files
committed
Revert "refactor a bit"
This reverts commit 20b1dd5.
1 parent ef16af5 commit 142b1d4

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/main/kotlin/app/GhService.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ 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+
2124
private val tokens = Config.getApiTokens()?.split(",") ?: listOf("") // empty token is limited to 60 requests
2225
private val clients = tokens.map { GitHubClient().apply { setOAuth2Token(it) } }
2326
private val repoServices = clients.map { RepositoryService(it) }
@@ -32,9 +35,8 @@ object GhService {
3235

3336
val remainingRequests: Int get() = clients.sumBy { it.remainingRequests }
3437

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

4042
init {
@@ -54,11 +56,13 @@ object GhService {
5456

5557
// update all connected clients with remainingRequests twice per second
5658
scheduleAtFixedRate({
59+
val payload = remainingRequests.toString()
5760
clientSessions.forEachKey(1) {
5861
try {
59-
it.send(remainingRequests.toString())
60-
} catch (e: Exception) {
61-
log.error(e.toString())
62+
if (it.isOpen)
63+
it.send(payload)
64+
} catch (e: IOException) {
65+
log?.error(e.toString())
6266
}
6367
}
6468
}, 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 { session -> GhService.registerClient(session) }
64+
ws.onConnect { GhService.registerClient(it) }
6565
ws.onClose { session, _, _ -> GhService.unregisterClient(session) }
6666
ws.onError { session, _ -> GhService.unregisterClient(session) }
6767
}

0 commit comments

Comments
 (0)