From 6576cab56355ee3f78330b18a7189612c2ea6162 Mon Sep 17 00:00:00 2001 From: hongwei Date: Thu, 5 Feb 2026 10:20:19 +0100 Subject: [PATCH] refactor/(http4s): make Http4sLiftSession thread-safe with ConcurrentHashMap - Replace mutable.Map with ConcurrentHashMap for thread-safe attribute storage - Add @volatile annotation to maxInactive field for safe concurrent access - Update setAttribute to use put() method for consistency with ConcurrentHashMap API - Update attribute getter to use get() method instead of getOrElse() - Improve thread safety of session attribute operations in multi-threaded environment --- .../scala/code/api/util/http4s/Http4sLiftWebBridge.scala | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/obp-api/src/main/scala/code/api/util/http4s/Http4sLiftWebBridge.scala b/obp-api/src/main/scala/code/api/util/http4s/Http4sLiftWebBridge.scala index b88b5d4b7..58e8407a3 100644 --- a/obp-api/src/main/scala/code/api/util/http4s/Http4sLiftWebBridge.scala +++ b/obp-api/src/main/scala/code/api/util/http4s/Http4sLiftWebBridge.scala @@ -276,16 +276,17 @@ object Http4sLiftWebBridge extends MdcLoggable { } private final class Http4sLiftSession(val sessionId: String) extends HTTPSession { - private val attributesStore = scala.collection.mutable.Map.empty[String, Any] - private var maxInactive: Long = 0L + // Thread-safe attribute store using ConcurrentHashMap + private val attributesStore = new ConcurrentHashMap[String, Any]() + @volatile private var maxInactive: Long = 0L private val createdAt: Long = System.currentTimeMillis() def link(liftSession: LiftSession): Unit = () def unlink(liftSession: LiftSession): Unit = () def maxInactiveInterval: Long = maxInactive def setMaxInactiveInterval(interval: Long): Unit = { maxInactive = interval } def lastAccessedTime: Long = createdAt - def setAttribute(name: String, value: Any): Unit = attributesStore.update(name, value) - def attribute(name: String): Any = attributesStore.getOrElse(name, null) + def setAttribute(name: String, value: Any): Unit = attributesStore.put(name, value) + def attribute(name: String): Any = attributesStore.get(name) def removeAttribute(name: String): Unit = attributesStore.remove(name) def terminate: Unit = () }