in nlpcraft/src/main/scala/org/apache/nlpcraft/internal/dialogflow/NCDialogFlowManager.scala [40:91]
private def clearForTimeout(): Long =
require(Thread.holdsLock(flow))
val timeout = cfg.getConversationTimeout
val bound = NCUtils.now() - timeout
var next = Long.MaxValue
val delKeys = mutable.ArrayBuffer.empty[String]
for ((usrId, values) <- flow)
values --= values.filter(_.getRequest.getReceiveTimestamp < bound)
if values.nonEmpty then
val candidate = values.map(_.getRequest.getReceiveTimestamp).min + timeout
if next > candidate then next = candidate
else
delKeys += usrId
if delKeys.nonEmpty then flow --= delKeys
next
/**
*
* @param intentMatch
* @param res
* @param ctx
*/
private def mkItem(intentMatch: NCIntentMatch, res: Option[NCResult], ctx: NCContext): NCDialogFlowItem =
new NCDialogFlowItem:
override val getIntentMatch: NCIntentMatch = intentMatch
override val getRequest: NCRequest = ctx.getRequest
override val getResult: Option[NCResult] = res
/**
*
*/
def start(): Unit =
gc = NCUtils.mkThread("dialog-mgr-gc", cfg.getId) { t =>
while (!t.isInterrupted)
try
flow.synchronized {
val sleepTime = clearForTimeout() - NCUtils.now()
if sleepTime > 0 then
logger.trace(s"${t.getName} waits for $sleepTime ms.")
flow.wait(sleepTime)
}
catch
case _: InterruptedException => // No-op.
case e: Throwable => logger.error(s"Unexpected error for thread: ${t.getName}", e)
}