Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: fix reporting "no response error" after laptop in sleep mode #5716

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference

import scala.concurrent.duration.Duration

Expand Down Expand Up @@ -62,52 +61,38 @@ class ServerLivenessMonitor(
pingInterval: Duration,
bspStatus: BspStatus,
) {
private val state: AtomicReference[ServerLivenessMonitor.State] =
new AtomicReference(ServerLivenessMonitor.Idle)
@volatile private var lastPing: Long = 0
val scheduler: ScheduledExecutorService = Executors.newScheduledThreadPool(1)

scribe.debug("starting server liveness monitor")

def runnable(): Runnable = new Runnable {
def run(): Unit = {
def now = System.currentTimeMillis()
def lastIncoming =
requestMonitor.lastIncoming
.map(now - _)
.getOrElse(pingInterval.toMillis)
def notResponding = lastIncoming > (pingInterval.toMillis * 2)
def metalsIsIdle =
requestMonitor.lastOutgoing
.map(lastOutgoing =>
(now - lastOutgoing) > metalsIdleInterval.toMillis
)
.getOrElse(true)
if (!metalsIsIdle) {
val currState = state.getAndUpdate {
case ServerLivenessMonitor.Idle => ServerLivenessMonitor.FirstPing
case _ => ServerLivenessMonitor.Running
}
currState match {
case ServerLivenessMonitor.Idle =>
scribe.debug("setting server liveness monitor state to FirstPing")
case ServerLivenessMonitor.FirstPing =>
scribe.debug("setting server liveness monitor state to Running")
case _ =>
}
if (currState == ServerLivenessMonitor.Running) {
if (notResponding) {
bspStatus.noResponse()
}
if (lastPingOk && notResponding) {
bspStatus.noResponse()
}
scribe.debug("server liveness monitor: pinging build server...")
lastPing = now
ping()
} else {
if (state.get() != ServerLivenessMonitor.Idle)
scribe.debug("setting server liveness monitor state to Idle")
state.set(ServerLivenessMonitor.Idle)
}
}
}

private def now = System.currentTimeMillis()

def metalsIsIdle: Boolean =
requestMonitor.lastOutgoing
.map(lastOutgoing => (now - lastOutgoing) > metalsIdleInterval.toMillis)
.getOrElse(true)

def lastPingOk: Boolean = now - lastPing < (pingInterval.toMillis * 2)

val scheduled: ScheduledFuture[_ <: Object] = scheduler.scheduleAtFixedRate(
runnable(),
pingInterval.toMillis,
Expand All @@ -124,7 +109,6 @@ class ServerLivenessMonitor(
bspStatus.disconnected()
}

def getState: ServerLivenessMonitor.State = state.get()
}

object ServerLivenessMonitor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@ class ServerLivenessMonitorSuite extends BaseSuite {
)
bspStatus.connected()
Thread.sleep(pingInterval.toMillis * 3 / 2)
assertEquals(livenessMonitor.getState, ServerLivenessMonitor.Idle)
assert(livenessMonitor.metalsIsIdle)
server.sendRequest(false)
Thread.sleep(pingInterval.toMillis * 2)
assertNotEquals(livenessMonitor.getState, ServerLivenessMonitor.Idle)
assert(!livenessMonitor.metalsIsIdle)
Thread.sleep(pingInterval.toMillis * 5)
assertEquals(livenessMonitor.getState, ServerLivenessMonitor.Idle)
assert(livenessMonitor.metalsIsIdle)
server.sendRequest(false)
Thread.sleep(pingInterval.toMillis)
server.sendRequest(false)
server.sendRequest(false)
Thread.sleep(pingInterval.toMillis * 2)
server.sendRequest(false)
assertEquals(livenessMonitor.getState, ServerLivenessMonitor.Running)
assert(!livenessMonitor.metalsIsIdle)
assert(livenessMonitor.lastPingOk)
assert(client.showMessageRequests == 0)
livenessMonitor.shutdown()
}
Expand Down
Loading