Skip to content
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 @@ -36,8 +36,10 @@ internal class BridgeHeartbeat(

/**
* Replaces any running heartbeat. [ping] sends one frame on the socket and
* returns false when that socket is no longer the current one, which ends the
* heartbeat quietly. [onSilent] reports the drop; it also runs when [ping] throws.
* returns false when that socket is no longer the current one or cannot take
* the frame, which ends the heartbeat quietly. A socket that cannot take a
* frame is closing, and its own close reports the drop with the real close
* code. [onSilent] runs only for silence past the timeout.
*/
fun start(intervalMs: Long, timeoutMs: Long, ping: () -> Boolean, onSilent: () -> Unit) {
synchronized(lock) {
Expand All @@ -61,11 +63,13 @@ internal class BridgeHeartbeat(
onSilent()
return
}
// A ping that cannot be sent is not a drop: reporting one here would
// replace the close code the socket is about to report, and turn a
// policy close into a reconnect.
val sent = try {
ping()
} catch (_: Throwable) {
onSilent()
return
false
}
if (!sent) return
// The next check falls at the timeout when no frame arrives before it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,8 @@ class PyricBridgeClient(
if (isCurrentSocket) transport else null
} ?: return false
val frame = BridgeProtocol.createPingFrame("ping-${pingCounter.incrementAndGet()}")
val enqueued = socket.send(JsonCodec.encodeToString(frame))
if (!enqueued) {
throw FirebaseFirestoreException(
"Failed to enqueue ping to bridge transport.",
FirebaseFirestoreException.Code.UNAVAILABLE
)
}
return true
// False when the socket is closing; its close callback reports the drop.
return socket.send(JsonCodec.encodeToString(frame))
}

private fun setState(next: BridgeConnectionState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ class FakeReconnectSocket(
open = false
listener.onClosed(code, "dropped")
}

/**
* The peer's close is under way but not yet reported: [send] returns false,
* as OkHttp's does on a closing socket, until [finishClosing] reports it.
*/
fun beginClosing() {
open = false
}

fun finishClosing(code: Int) {
listener.onClosed(code, "closed by peer")
}
}

/** Hands out a new socket per connection and records every frame across all of them. */
Expand Down Expand Up @@ -602,6 +614,27 @@ class BridgeReconnectTest {
assertEquals(1, bridge.ofType("attach").size)
}

@Test
fun `a ping refused by a closing socket leaves the drop to its close, so a policy close is not retried`() = runBlocking {
val client = client(heartbeatIntervalMs = 20L)
client.connect()
val socket = bridge.current
until { socket.pings() >= 1 }

socket.beginClosing()
val pingsAtClosing = socket.pings()
until { socket.pings() > pingsAtClosing }
delay(100)
assertEquals(BridgeConnectionState.ATTACHED, client.connectionState, "a refused ping is not a drop")
assertEquals(1, bridge.connectCalls.get(), "a refused ping must not start a reconnect")

socket.finishClosing(1008)
until { client.connectionState == BridgeConnectionState.CLOSED }
delay(50)
assertEquals(1, bridge.ofType("attach").size)
assertEquals(1, bridge.connectCalls.get())
}

@Test
fun `a replaced host runs the Auth restore before any listen is re-sent`() = runBlocking {
val client = client()
Expand Down
Loading