diff --git a/src/main/scala/io/otoroshi/wasm4s/scaladsl/integration.scala b/src/main/scala/io/otoroshi/wasm4s/scaladsl/integration.scala index 9279ff6..cbb6178 100644 --- a/src/main/scala/io/otoroshi/wasm4s/scaladsl/integration.scala +++ b/src/main/scala/io/otoroshi/wasm4s/scaladsl/integration.scala @@ -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)) } @@ -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) =>