forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into ply-alert
- Loading branch information
Showing
69 changed files
with
760 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package lila.relay | ||
|
||
import java.nio.charset.{ Charset, StandardCharsets } | ||
import io.mola.galimatias.URL | ||
import play.api.libs.ws.* | ||
import play.shaded.ahc.org.asynchttpclient.util.HttpUtils.extractContentTypeCharsetAttribute | ||
|
||
import lila.core.lilaism.LilaException | ||
|
||
/* Extra generic features for play WS client, | ||
* without any knowledge of broadcast specifics. | ||
* This could be moved for reuse later on. | ||
* - Proxies | ||
* - Etag cache | ||
*/ | ||
private final class HttpClient( | ||
ws: StandaloneWSClient, | ||
cacheApi: lila.memo.CacheApi, | ||
proxySelector: ProxySelector | ||
)(using Executor): | ||
|
||
import HttpClient.* | ||
|
||
val etagCache = cacheApi.notLoadingSync[URL, (Body, Etag)](256, "relay.fetch.etagCache"): | ||
_.expireAfterWrite(10 minutes).build() | ||
|
||
def get(url: URL)(using CanProxy): Fu[Body] = | ||
etagCache | ||
.getIfPresent(url) | ||
.match | ||
case None => | ||
fetchBodyAndEtag(url, none) | ||
case Some((prevBody, prevEtag)) => | ||
fetchBodyAndEtag(url, prevEtag.some).map: (newBody, newEtag) => | ||
val body = if newBody.isEmpty && newEtag.has(prevEtag) then prevBody.some else newBody | ||
(body, newEtag) | ||
.map: (body, etag) => | ||
(body, etag).mapN((b, e) => etagCache.put(url, b -> e)) | ||
~body | ||
|
||
private def fetchBodyAndEtag(url: URL, etag: Option[Etag])(using | ||
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 = res.header("Etag") | ||
if res.status == 304 | ||
then none -> newEtag.orElse(etag) | ||
else decodeResponseBody(res).some -> newEtag | ||
|
||
private def fetchResponse(req: StandaloneWSRequest): Fu[StandaloneWSResponse] = | ||
Future | ||
.fromTry(lila.common.url.parse(req.url)) | ||
.flatMap: url => | ||
req | ||
.get() | ||
.monValue: res => | ||
_.relay.httpGet( | ||
res.status, | ||
url.host.toString, | ||
etag = monitorEtagHit(req, res), | ||
req.proxyServer.map(_.host) | ||
) | ||
.flatMap: res => | ||
if res.status == 200 || res.status == 304 then fuccess(res) | ||
else fufail(Status(res.status, url)) | ||
|
||
private def decodeResponseBody(res: StandaloneWSResponse): Body = | ||
val charset = Option(extractContentTypeCharsetAttribute(res.contentType)) | ||
.orElse(res.contentType.startsWith("text/").option(StandardCharsets.ISO_8859_1)) | ||
charset match | ||
case None => lila.common.String.charset.guessAndDecode(res.bodyAsBytes) | ||
case Some(known) => res.bodyAsBytes.decodeString(known) | ||
|
||
private def toRequest(url: URL)(using CanProxy): StandaloneWSRequest = | ||
val req = ws | ||
.url(url.toString) | ||
.withRequestTimeout(5.seconds) | ||
.withFollowRedirects(false) | ||
proxySelector(url).foldLeft(req)(_ withProxyServer _) | ||
|
||
private def monitorEtagHit(req: StandaloneWSRequest, res: StandaloneWSResponse): String = | ||
(req.header("If-None-Match"), res.header("Etag")) match | ||
case (None, None) => "none" // endpoint doesn't support Etag | ||
case (None, Some(_)) => "first" // local cache is cold | ||
case (Some(_), _) if res.status == 304 => "hit" // cache hit | ||
case (Some(_), Some(_)) => "miss" // new data from the endpoint | ||
case (Some(_), None) => "fail" // we sent an etag but the endpoint doesn't support it? | ||
|
||
private object HttpClient: | ||
type Etag = String | ||
type Body = String | ||
case class Status(code: Int, url: URL) extends LilaException: | ||
override val message = s"$code: $url" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.