Skip to content

Commit

Permalink
retry broadcast fetch without etag
Browse files Browse the repository at this point in the history
something in the http implementation of http://liveskak.dk
is very broken. Requests containing an etag can start producing
responses with error code 400, seemingly randomly, even if the
exact same request has succeeded seconds before.

when it happens, we try again without etag.
  • Loading branch information
ornicar committed Feb 12, 2025
1 parent 43b6d6d commit cfa2266
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions modules/relay/src/main/HttpClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ private final class HttpClient(
CanProxy
): Fu[(Option[Body], Option[Etag])] =
val req = etag.foldLeft(toRequest(url))((req, etag) => req.addHttpHeaders("If-None-Match" -> etag))
fetchResponse(req).map: res =>
val newEtag = extractEtagValue(res)
if res.status == 304
then none -> newEtag.orElse(etag)
else decodeResponseBody(res).some -> newEtag
fetchResponse(req)
.map: res =>
val newEtag = extractEtagValue(res)
if res.status == 304
then none -> newEtag.orElse(etag)
else decodeResponseBody(res).some -> newEtag
.recoverWith:
case Status(400, _) if etag.isDefined =>
val prevEtag = etag.get // terrible, I wish it could be extracted from the match above
fetchBodyAndEtag(url, none)
.addEffects: res =>
val success = if res.isSuccess then "succeeded" else "failed"
logger.info(s"Retrying $url without etag $prevEtag -> $success")
.map: (body, etag) =>
(body, etag.filter(_ != prevEtag))

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag#w
private def extractEtagValue(res: StandaloneWSResponse): Option[Etag] =
Expand Down

0 comments on commit cfa2266

Please sign in to comment.