@@ -18,6 +18,9 @@ object GhService {
18
18
19
19
private val log = LoggerFactory .getLogger(GhService .javaClass)
20
20
21
+ // Allows for parallel iteration and O(1) put/remove
22
+ private val clientSessions = ConcurrentHashMap <WsSession , Boolean >()
23
+
21
24
private val tokens = Config .getApiTokens()?.split(" ," ) ? : listOf (" " ) // empty token is limited to 60 requests
22
25
private val clients = tokens.map { GitHubClient ().apply { setOAuth2Token(it) } }
23
26
private val repoServices = clients.map { RepositoryService (it) }
@@ -32,9 +35,8 @@ object GhService {
32
35
33
36
val remainingRequests: Int get() = clients.sumBy { it.remainingRequests }
34
37
35
- // Allows for parallel iteration and O(1) put/remove
36
- private val clientSessions = ConcurrentHashMap <WsSession , Boolean >()
37
38
fun registerClient (ws : WsSession ) = clientSessions.put(ws, true ) == true
39
+
38
40
fun unregisterClient (ws : WsSession ) = clientSessions.remove(ws) == true
39
41
40
42
init {
@@ -54,11 +56,13 @@ object GhService {
54
56
55
57
// update all connected clients with remainingRequests twice per second
56
58
scheduleAtFixedRate({
59
+ val payload = remainingRequests.toString()
57
60
clientSessions.forEachKey(1 ) {
58
61
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())
62
66
}
63
67
}
64
68
}, 0 , 500 , TimeUnit .MILLISECONDS )
0 commit comments