Skip to content

Commit 9383602

Browse files
committed
Revert "Improve concurrency (#62)"
This reverts commit 72ff88c.
1 parent 142b1d4 commit 9383602

File tree

2 files changed

+19
-33
lines changed

2 files changed

+19
-33
lines changed

src/main/kotlin/app/GhService.kt

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import org.eclipse.egit.github.core.service.RepositoryService
77
import org.eclipse.egit.github.core.service.UserService
88
import org.eclipse.egit.github.core.service.WatcherService
99
import org.slf4j.LoggerFactory
10-
import java.io.IOException
11-
import java.util.concurrent.ConcurrentHashMap
12-
import java.util.concurrent.Executors
10+
import java.util.*
1311
import java.util.concurrent.TimeUnit
1412

1513
object GhService {
@@ -18,11 +16,8 @@ object GhService {
1816

1917
private val log = LoggerFactory.getLogger(GhService.javaClass)
2018

21-
// Allows for parallel iteration and O(1) put/remove
22-
private val clientSessions = ConcurrentHashMap<WsSession, Boolean>()
23-
2419
private val tokens = Config.getApiTokens()?.split(",") ?: listOf("") // empty token is limited to 60 requests
25-
private val clients = tokens.map { GitHubClient().apply { setOAuth2Token(it) } }
20+
private val clients = tokens.map { token -> GitHubClient().apply { setOAuth2Token(token) } }
2621
private val repoServices = clients.map { RepositoryService(it) }
2722
private val commitServices = clients.map { CommitService(it) }
2823
private val userServices = clients.map { UserService(it) }
@@ -35,15 +30,9 @@ object GhService {
3530

3631
val remainingRequests: Int get() = clients.sumBy { it.remainingRequests }
3732

38-
fun registerClient(ws: WsSession) = clientSessions.put(ws, true) == true
39-
40-
fun unregisterClient(ws: WsSession) = clientSessions.remove(ws) == true
41-
42-
init {
43-
Executors.newScheduledThreadPool(2).apply {
44-
45-
// ping clients every other minute to make sure remainingRequests is correct
46-
scheduleAtFixedRate({
33+
init { // create timer to ping clients every other minute to make sure remainingRequests is correct
34+
Timer().scheduleAtFixedRate(object : TimerTask() {
35+
override fun run() {
4736
repoServices.forEach {
4837
try {
4938
it.getRepository("tipsy", "github-profile-summary")
@@ -52,22 +41,18 @@ object GhService {
5241
log.info("Pinged client ${clients.indexOf(it.client)} - was rate-limited")
5342
}
5443
}
55-
}, 0, 2, TimeUnit.MINUTES)
56-
57-
// update all connected clients with remainingRequests twice per second
58-
scheduleAtFixedRate({
59-
val payload = remainingRequests.toString()
60-
clientSessions.forEachKey(1) {
61-
try {
62-
if (it.isOpen)
63-
it.send(payload)
64-
} catch (e: IOException) {
65-
log?.error(e.toString())
66-
}
67-
}
68-
}, 0, 500, TimeUnit.MILLISECONDS)
44+
}
45+
}, 0, TimeUnit.MILLISECONDS.convert(2, TimeUnit.MINUTES))
46+
}
6947

48+
fun broadcastRemainingRequests(session: WsSession) = object : TimerTask() {
49+
override fun run() {
50+
if (session.isOpen) {
51+
return session.send(GhService.remainingRequests.toString())
52+
}
53+
this.cancel()
7054
}
7155
}
7256

7357
}
58+

src/main/kotlin/app/Main.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.eclipse.jetty.server.Server
1212
import org.eclipse.jetty.server.ServerConnector
1313
import org.eclipse.jetty.util.thread.QueuedThreadPool
1414
import org.slf4j.LoggerFactory
15+
import java.util.*
1516

1617
fun main(args: Array<String>) {
1718

@@ -61,9 +62,9 @@ fun main(args: Array<String>) {
6162
}
6263

6364
ws("/rate-limit-status") { ws ->
64-
ws.onConnect { GhService.registerClient(it) }
65-
ws.onClose { session, _, _ -> GhService.unregisterClient(session) }
66-
ws.onError { session, _ -> GhService.unregisterClient(session) }
65+
ws.onConnect { session ->
66+
Timer().scheduleAtFixedRate(GhService.broadcastRemainingRequests(session), 0, 1000)
67+
}
6768
}
6869

6970
}

0 commit comments

Comments
 (0)