Skip to content

Commit

Permalink
Fix: Staled cache response used when system time adjusted backward (#…
Browse files Browse the repository at this point in the history
…8077)

* Fix: Staled cache response used when system time adjusted backward。
OkHttp would incorrectly use a cached response for a second request if the server's first response did not contain caching headers, if the system time was adjusted backward between the two requests。

* clamp each of the durations to be non-negative instead of doing that to the overall result

---------

Co-authored-by: luxinwei <[email protected]>
  • Loading branch information
luuillu and luxinwei authored Dec 3, 2023
1 parent 60d2a54 commit 6c617e8
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ class CacheStrategy internal constructor(
apparentReceivedAge
}

val responseDuration = receivedResponseMillis - sentRequestMillis
val residentDuration = nowMillis - receivedResponseMillis
val responseDuration = maxOf(0, receivedResponseMillis - sentRequestMillis)
val residentDuration = maxOf(0, nowMillis - receivedResponseMillis)
return receivedAge + responseDuration + residentDuration
}

Expand Down

0 comments on commit 6c617e8

Please sign in to comment.