Skip to content

Commit

Permalink
decrease time-to-live of channel contents file to 30 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
memo33 committed Mar 31, 2024
1 parent eaeab13 commit f7d3aa6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 4 additions & 0 deletions src/main/scala/sc4pac/Constants.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/sc4pac/Find.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/main/scala/sc4pac/Sc4pac.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 })
Expand All @@ -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)
}
Expand Down

0 comments on commit f7d3aa6

Please sign in to comment.