Created
February 11, 2020 21:00
-
-
Save vhutov/628cabb46ffde9a793463d3f7ed6364b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Concurrent extends IOApp { | |
override def run(args: List[String]): IO[ExitCode] = { | |
(0 to 10).toList | |
.parTraverse(run) | |
.as(ExitCode.Success) | |
} | |
private def run(id: Int): IO[String] = { | |
val idx = id % types.length | |
val tpe = types(idx) | |
IO.shift *> | |
op(id.toString, tpe).withLogContext(_.id(id.toString)) <* IO.shift | |
} | |
private def op(id: String, tpe: String): IO[String] = { | |
for { | |
_ <- log.info(s"op start [static id = $id]") | |
_ <- IO.shift | |
result <- opInner(id, tpe).withLogContext(_.tpe(tpe)) | |
_ <- IO.shift | |
_ <- log.info(s"op end [static id = $id]") | |
} yield result | |
} | |
private def opInner(id: String, tpe: String): IO[String] = | |
for { | |
_ <- log.info(s"op_inner start [static id = $id, static type = $tpe]") | |
_ <- IO.shift | |
result <- IO("user") | |
_ <- IO.shift | |
_ <- log.info(s"op_inner end [static id = $id, static type = $tpe]") | |
} yield result | |
private val log: Logger[IO] = Slf4jLogger.getLogger[IO].mdc | |
private val types = Vector("type1", "type2", "type3") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment