Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition in ref access #1

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ lazy val ibFeed = project
Dependencies.Libraries.log4cats,
Dependencies.Libraries.logback,
Dependencies.Libraries.fs2io,
Dependencies.Libraries.mapref,
Dependencies.Libraries.fs2core,
Dependencies.Libraries.scalatest,
Dependencies.Libraries.ceTestKit,
Expand Down
32 changes: 16 additions & 16 deletions ibFeed/src/main/scala/domain/feed/Limits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import cats.syntax.all._
import cats.effect.std.{Semaphore, UUIDGen}
import cats.effect.{Async, Clock, Ref, Resource, Temporal}
import cats.effect.syntax.all._
import io.chrisdavenport.mapref.implicits._
import domain.feed.Limits._
import model.datastax.ib.feed.ast.{BarSize, RequestType}
import model.datastax.ib.feed.request.{Request, RequestData}
import org.typelevel.log4cats.Logger
import io.chrisdavenport.mapref._

import scala.concurrent.duration._

Expand Down Expand Up @@ -125,7 +123,7 @@ object Limits {

class SameContractSizeLimit[F[_]: Async](
limit: Int,
reqByContSizeLocks: MapRef[F, (Int, BarSize), Option[(Semaphore[F], List[FiniteDuration])]],
reqByContSizeLocks: Ref[F, Map[(Int, BarSize), Ref[F, (Semaphore[F], List[FiniteDuration])]]],
expDuration: FiniteDuration
) extends Limit[F] {

Expand All @@ -139,28 +137,30 @@ object Limits {
_ <- request match {
case r: RequestData if r.requestType == RequestType.Historic =>
val idx = (r.contId, r.size)
reqByContSizeLocks(idx).get.flatMap { {
case Some((lck, _)) =>
lck
for {
lck <- Semaphore[F](1)
ref <- Ref.of((lck, Nil))
ref <- reqByContSizeLocks.modify { tsById =>
tsById.get(idx) match {
case Some(ref2) => (tsById, ref2)
case None => (tsById + (idx -> ref), ref)
}
}
(lck, _) <- ref.get.map(_._1)
_ <- lck
.permit
.use(_ =>
for {
now <- Clock[F].realTime
maybeValue <- reqByContSizeLocks(idx).get
(_, ts) = maybeValue.get
ts <- ref.get.map(_._2)
_ <- if (ts.size >= limit) Async[F].delay(println(s"$idx $now ${expDuration + ts.head - now} $ts")) else Async[F].unit
_ <- if (ts.size >= limit) Temporal[F].sleep(expDuration + ts.head - now) else Async[F].unit
now <- Clock[F].realTime
_ <- Async[F].delay(println(s"2. $idx $id $now"))
_ <- reqByContSizeLocks.updateKeyValueIfSet(idx, ts => (ts._1, ts._2.filter(_ + expDuration > now) :+ now))
ts = ts.filter(_ + expDuration > now) :+ now
_ <- ref.set((lck, ts))
} yield ()
) >> Async[F].delay( println(s"3. $idx $id $now"))
case None =>
Semaphore[F](1).flatMap(lck => reqByContSizeLocks(idx).update{
case None => (lck, List(now)).some
case Some(v) => (v._1, v._2 :+ now).some
}) >> Async[F].delay( println(s"$idx $id $now <<<<"))
}
}
case _ => ().pure[F]
}
Expand All @@ -174,7 +174,7 @@ object Limits {
for {
hist10Min <- Resource.eval(Ref[F].of(List.empty[FiniteDuration]))
hist10MinLock <- Resource.eval(Semaphore[F](lim.hist10MinLimit))
reqByContSizeLocks <- Resource.eval(MapRef.ofSingleImmutableMap[F, (Int, BarSize), (AtomicBoolean.bool, List[FiniteDuration])]())
reqByContSizeLocks <- Resource.eval(Ref[F].of(Map.empty[(Int, BarSize), (Semaphore[F], List[FiniteDuration])]))
hist10MinMainLock <- Resource.eval(Semaphore[F](1))
msgLimitMainLock <- Resource.eval(Semaphore[F](1))
msgLimit <- Resource.eval(Ref[F].of(List.empty[FiniteDuration]))
Expand Down
2 changes: 0 additions & 2 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ object Dependencies {
val test = "3.2.9"
val CEtest = "1.4.0"
val ceTestKit = "3.3.9"
val mapref = ""

val cassandraCore = "4.0.0"
val logback = "1.2.10"
Expand Down Expand Up @@ -41,7 +40,6 @@ object Dependencies {
val catsCore = "org.typelevel" %% "cats-core" % V.cats
val kittens = "org.typelevel" %% "kittens" % V.kittens
val catsEffect = ("org.typelevel" %% "cats-effect" % V.ce).withSources().withJavadoc()
val mapref = "io.chrisdavenport" %% "mapref" % "0.2.1"
val fs2core = "co.fs2" %% "fs2-core" % V.fs2
val fs2io = "co.fs2" %% "fs2-io" % V.fs2
val fs2rx = "co.fs2" %% "fs2-reactive-streams" % V.fs2
Expand Down