From f7d3aa6228dfb7ed26150c4c4f47119df6c2c7ef Mon Sep 17 00:00:00 2001 From: memo Date: Sun, 31 Mar 2024 08:37:01 +0200 Subject: [PATCH] decrease time-to-live of channel contents file to 30 minutes --- CHANGELOG.md | 2 ++ build.sbt | 2 +- src/main/scala/sc4pac/Constants.scala | 4 ++++ src/main/scala/sc4pac/Find.scala | 2 +- src/main/scala/sc4pac/Sc4pac.scala | 16 ++++++++-------- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42a3fea..d98f444 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## [Unreleased] +### Changed +- decreased caching period of channel table-of-contents file from 24 hours to 30 minutes to receive package updates sooner ## [0.4.1] - 2024-03-25 diff --git a/build.sbt b/build.sbt index 6438296..19b82a4 100644 --- a/build.sbt +++ b/build.sbt @@ -2,7 +2,7 @@ name := "sc4pac" ThisBuild / organization := "io.github.memo33" -ThisBuild / version := "0.4.1" +ThisBuild / version := "0.4.2-SNAPSHOT" // ThisBuild / versionScheme := Some("early-semver") diff --git a/src/main/scala/sc4pac/Constants.scala b/src/main/scala/sc4pac/Constants.scala index ace3b98..dcdd7c5 100644 --- a/src/main/scala/sc4pac/Constants.scala +++ b/src/main/scala/sc4pac/Constants.scala @@ -3,6 +3,7 @@ package sc4pac import coursier.core.{Configuration, Organization, Type, Module} import java.util.regex.Pattern +import scala.concurrent.duration.DurationInt object Constants { val compile = Configuration.compile // includes only metadata as dependencies @@ -27,6 +28,9 @@ object Constants { val sslRetryCount = 3 // Coursier legacy val resumeIncompleteDownloadAttemps = 4 val fuzzySearchThreshold = 50 // 0..100 + val cacheTtl = 12.hours + val channelContentsTtl = 30.minutes + val channelContentsTtlShort = 60.seconds val interactivePromptTimeout = java.time.Duration.ofSeconds(240) val urlConnectTimeout = java.time.Duration.ofSeconds(60) val urlReadTimeout = java.time.Duration.ofSeconds(60) // timeout in case of internet outage while downloading a file diff --git a/src/main/scala/sc4pac/Find.scala b/src/main/scala/sc4pac/Find.scala index 80af837..e99f35f 100644 --- a/src/main/scala/sc4pac/Find.scala +++ b/src/main/scala/sc4pac/Find.scala @@ -48,7 +48,7 @@ object Find { val repoUris = context.repositories.map(_.baseUri) context.logger.log(s"Could not find metadata of ${module}. Trying to update channel contents.") for { - repos <- Sc4pac.initializeRepositories(repoUris, context.cache, channelContentsTtl = Some(60.seconds)) + repos <- Sc4pac.initializeRepositories(repoUris, context.cache, Constants.channelContentsTtlShort) // 60 seconds .provideLayer(zio.ZLayer.succeed(ScopeRoot(context.scopeRoot))) result <- tryAllRepos(repos, context) // 2nd try } yield result diff --git a/src/main/scala/sc4pac/Sc4pac.scala b/src/main/scala/sc4pac/Sc4pac.scala index 0ee2969..1a111c1 100644 --- a/src/main/scala/sc4pac/Sc4pac.scala +++ b/src/main/scala/sc4pac/Sc4pac.scala @@ -470,13 +470,13 @@ object Sc4pac { case class StageResult(tempPluginsRoot: os.Path, files: Seq[(DepModule, Seq[os.SubPath])], stagingRoot: os.Path) - private def fetchChannelData(repoUri: java.net.URI, cache: FileCache, channelContentsTtl: Option[scala.concurrent.duration.Duration]): ZIO[ScopeRoot, ErrStr, MetadataRepository] = { + private def fetchChannelData(repoUri: java.net.URI, cache: FileCache, channelContentsTtl: scala.concurrent.duration.Duration): ZIO[ScopeRoot, ErrStr, MetadataRepository] = { import CoursierZio.* // implicit coursier-zio interop val contentsUrl = MetadataRepository.channelContentsUrl(repoUri).toString val artifact = Artifact(contentsUrl).withChanging(true) // changing as the remote file is updated whenever any remote package is added or updated for { channelContentsFile <- cache - .withTtl(channelContentsTtl.orElse(cache.ttl)) + .withTtl(Some(channelContentsTtl)) .file(artifact) // requires initialized logger .run.absolve .mapError { case e @ (_: coursier.cache.ArtifactError | scala.util.control.NonFatal(_)) => e.getMessage } @@ -493,7 +493,7 @@ object Sc4pac { } yield result } - private[sc4pac] def initializeRepositories(repoUris: Seq[java.net.URI], cache: FileCache, channelContentsTtl: Option[scala.concurrent.duration.Duration]): RIO[ScopeRoot, Seq[MetadataRepository]] = { + private[sc4pac] def initializeRepositories(repoUris: Seq[java.net.URI], cache: FileCache, channelContentsTtl: scala.concurrent.duration.Duration): RIO[ScopeRoot, Seq[MetadataRepository]] = { val task: RIO[ScopeRoot, Seq[MetadataRepository]] = ZIO.collectPar(repoUris) { url => fetchChannelData(url, cache, channelContentsTtl) .mapError((err: ErrStr) => { System.err.println(s"Failed to read channel data: $err"); None }) @@ -510,12 +510,12 @@ object Sc4pac { val coursierPool = coursier.cache.internal.ThreadUtil.fixedThreadPool(size = 2) // limit parallel downloads to 2 (ST rejects too many connections) for { cacheRoot <- config.cacheRootAbs - logger <- ZIO.service[Logger] - cache = FileCache(location = (cacheRoot / "coursier").toIO, logger = logger, pool = coursierPool) + logger <- ZIO.service[Logger] + cache = FileCache(location = (cacheRoot / "coursier").toIO, logger = logger, pool = coursierPool) + .withTtl(Some(Constants.cacheTtl)) // 12 hours // .withCachePolicies(Seq(coursier.cache.CachePolicy.ForceDownload)) // TODO cache policy - // .withTtl(1.hour) // TODO time-to-live - repos <- initializeRepositories(config.channels, cache, channelContentsTtl = None) - tempRoot <- config.tempRootAbs + repos <- initializeRepositories(config.channels, cache, Constants.channelContentsTtl) // 30 minutes + tempRoot <- config.tempRootAbs scopeRoot <- ZIO.service[ScopeRoot] } yield Sc4pac(repos, cache, tempRoot, logger, scopeRoot.path) }