Skip to content

Commit

Permalink
Added a new job to fix the never cleaning cache issue (fix #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuancelin committed Feb 15, 2024
1 parent 34becb3 commit 68e80f4
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/scala/io/otoroshi/wasm4s/scaladsl/integration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class WasmIntegration(ic: WasmIntegrationContext) {
def start(cleanerJobConfig: JsObject): Unit = {
schedRef.set(scheduler.scheduleWithFixedDelay(() => {
runVmLoaderJob()
runCacheCleanerJob()
runVmCleanerJob(cleanerJobConfig)
}, 1000, context.wasmCacheTtl, TimeUnit.MILLISECONDS))
}
Expand Down Expand Up @@ -160,6 +161,25 @@ class WasmIntegration(ic: WasmIntegrationContext) {
}
}

def runCacheCleanerJob(): Future[Unit] = {
for {
inlineSources <- ic.inlineWasmSources()
pluginSources <- ic.wasmConfigs().map(_.map(_.source))
} yield {
val sources = (pluginSources ++ inlineSources).distinct.map(s => (s.cacheKey, s)).toMap
val now = System.currentTimeMillis()
ic.wasmScriptCache.toSeq.foreach {
case (key, CacheableWasmScript.CachedWasmScript(_, createAt)) if (createAt + (ic.wasmCacheTtl * 2)) < now => { // 2 times should be enough
sources.get(key) match {
case Some(_) => ()
case None => ic.wasmScriptCache.remove(key)
}
}
case _ => ()
}
}
}

def runVmCleanerJob(config: JsObject): Future[Unit] = {
val globalNotUsedDuration = config.select("not-used-duration").asOpt[Long].map(v => v.millis).getOrElse(5.minutes)
io.otoroshi.wasm4s.impl.WasmVmPoolImpl.allInstances().foreach { case (key, pool) =>
Expand Down

0 comments on commit 68e80f4

Please sign in to comment.