├── .gitignore ├── .scala.fmt ├── LICENSE.txt ├── README.md ├── build.sbt ├── project └── build.properties └── src └── main └── scala └── catseffecttutorial ├── copyfile ├── CopyFile.scala └── CopyFilePolymorphic.scala └── producerconsumer ├── InefficientProducerConsumer.scala ├── ProducerConsumer.scala ├── ProducerConsumerBounded.scala ├── ProducerConsumerBoundedCancelable.scala └── exerciseconcurrentqueue ├── Main.scala └── Queue.scala /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.log 3 | *.swp 4 | 5 | # sbt specific 6 | .cache 7 | .history 8 | .lib/ 9 | dist/* 10 | target/ 11 | lib_managed/ 12 | src_managed/ 13 | project/target/ 14 | project/project/ 15 | project/boot/ 16 | project/plugins/project/ 17 | 18 | # Scala-IDE specific 19 | .scala_dependencies 20 | .worksheet 21 | 22 | # Idea specific 23 | .idea/ 24 | 25 | # Ensime specific 26 | .ensime 27 | .ensime_cache/ 28 | 29 | # Metals specific 30 | .metals/ 31 | project/metals.sbt 32 | .bloop/ 33 | .bsp/ 34 | 35 | -------------------------------------------------------------------------------- /.scala.fmt: -------------------------------------------------------------------------------- 1 | version = 2.4.2 2 | align = none 3 | align.openParenCallSite = true 4 | align.openParenDefnSite = true 5 | maxColumn = 120 6 | continuationIndent.defnSite = 2 7 | assumeStandardLibraryStripMargin = true 8 | danglingParentheses = true 9 | rewrite.rules = [AvoidInfix, SortImports, RedundantBraces, RedundantParens, SortModifiers] 10 | docstrings = JavaDoc 11 | lineEndings=preserve 12 | newlines.afterCurlyLambda = preserve 13 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | cats-effect-tutorial 2 | ==================== 3 | 4 | Source code of the examples/exercises of [cats-effect 5 | tutorial](https://typelevel.org/cats-effect/docs/tutorial). 6 | 7 | All contents released under the [Apache v2 license](https://www.apache.org/licenses/LICENSE-2.0). 8 | 9 | Compile and run the examples 10 | ---------------------------- 11 | Code can be compiled using `sbt`: 12 | ```bash 13 | $ sbt 14 | > compile 15 | ``` 16 | 17 | Any of the files can be executed also using `sbt`. So for example to run 18 | `catseffecttutorial.copyfile.CopyFile` to copy an `origin.txt` file to another 19 | `destination.txt` file we will run: 20 | ```bash 21 | $ sbt 22 | > runMain catseffecttutorial.copyfile.CopyFile origin.txt destination.txt 23 | ``` 24 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | name := "cats-effect-tutorial" 2 | 3 | version := "3.5.2" 4 | 5 | scalaVersion := "2.13.11" 6 | 7 | libraryDependencies += "org.typelevel" %% "cats-effect" % "3.5.2" withSources() withJavadoc() 8 | 9 | scalacOptions ++= Seq( 10 | "-feature", 11 | "-deprecation", 12 | "-unchecked", 13 | "-language:postfixOps" 14 | ) 15 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=1.9.7 2 | -------------------------------------------------------------------------------- /src/main/scala/catseffecttutorial/copyfile/CopyFile.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Luis Rodero-Merino 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at. 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package catseffecttutorial.copyfile 17 | 18 | import cats.effect._ 19 | 20 | import java.io._ 21 | 22 | /** 23 | * Simple IO-based program to copy files. First part of cats-effect tutorial 24 | * at https://typelevel.org/cats-effect/tutorial/tutorial.html 25 | */ 26 | object CopyFile extends IOApp { 27 | 28 | def transfer(origin: InputStream, destination: OutputStream, buffer: Array[Byte], acc: Long): IO[Long] = 29 | for { 30 | amount <- IO.blocking(origin.read(buffer, 0, buffer.length)) 31 | count <- if (amount > -1) IO.blocking(destination.write(buffer, 0, amount)) >> transfer(origin, destination, buffer, acc + amount) 32 | else IO.pure(acc) // End of read stream reached (by java.io.InputStream contract), nothing to write 33 | } yield count // Returns the actual amount of bytes transferred 34 | 35 | def inputStream(f: File): Resource[IO, FileInputStream] = 36 | Resource.make { 37 | IO.blocking(new FileInputStream(f)) 38 | } { inStream => 39 | IO.blocking(inStream.close()).handleErrorWith(_ => IO.unit) 40 | } 41 | 42 | def outputStream(f: File): Resource[IO, FileOutputStream] = 43 | Resource.make { 44 | IO.blocking(new FileOutputStream(f)) 45 | } { outStream => 46 | IO.blocking(outStream.close()).handleErrorWith(_ => IO.unit) 47 | } 48 | 49 | def inputOutputStreams(in: File, out: File): Resource[IO, (InputStream, OutputStream)] = 50 | for { 51 | inStream <- inputStream(in) 52 | outStream <- outputStream(out) 53 | } yield (inStream, outStream) 54 | 55 | def copy(origin: File, destination: File): IO[Long] = 56 | inputOutputStreams(origin, destination).use { case (in, out) => 57 | transfer(in, out, new Array[Byte](1024 * 10), 0L) 58 | } 59 | 60 | override def run(args: List[String]): IO[ExitCode] = 61 | for { 62 | _ <- IO.raiseWhen(args.length < 2)(new IllegalArgumentException("Need origin and destination files")) 63 | orig = new File(args.head) 64 | dest = new File(args.tail.head) 65 | count <- copy(orig, dest) 66 | _ <- IO.println(s"$count bytes copied from ${orig.getPath} to ${dest.getPath}") 67 | } yield ExitCode.Success 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/scala/catseffecttutorial/copyfile/CopyFilePolymorphic.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Luis Rodero-Merino 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at. 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package catseffecttutorial.copyfile 17 | 18 | import cats.effect._ 19 | import cats.syntax.all._ 20 | 21 | import java.io._ 22 | 23 | /** 24 | * Simple IO-based program to copy files. First part of cats-effect tutorial 25 | * at https://typelevel.org/cats-effect/tutorial/tutorial.html. This code is 26 | * a polymorphic version of [[CopyFile]] program. 27 | */ 28 | object CopyFilePolymorphic extends IOApp { 29 | 30 | def transfer[F[_]: Sync](origin: InputStream, destination: OutputStream, buffer: Array[Byte], acc: Long): F[Long] = 31 | for { 32 | amount <- Sync[F].blocking(origin.read(buffer, 0, buffer.length)) 33 | count <- if(amount > -1) Sync[F].blocking(destination.write(buffer, 0, amount)) >> transfer(origin, destination, buffer, acc + amount) 34 | else Sync[F].pure(acc) // End of read stream reached (by java.io.InputStream contract), nothing to write 35 | } yield count // Returns the actual amount of bytes transferred 36 | 37 | def inputStream[F[_]: Sync](f: File): Resource[F, FileInputStream] = 38 | Resource.make { 39 | Sync[F].blocking(new FileInputStream(f)) 40 | } { inStream => 41 | Sync[F].blocking(inStream.close()).handleErrorWith(_ => Sync[F].unit) 42 | } 43 | 44 | def outputStream[F[_]: Sync](f: File): Resource[F, FileOutputStream] = 45 | Resource.make { 46 | Sync[F].blocking(new FileOutputStream(f)) 47 | } { outStream => 48 | Sync[F].blocking(outStream.close()).handleErrorWith(_ => Sync[F].unit) 49 | } 50 | 51 | def inputOutputStreams[F[_]: Sync](in: File, out: File): Resource[F, (InputStream, OutputStream)] = 52 | for { 53 | inStream <- inputStream(in) 54 | outStream <- outputStream(out) 55 | } yield (inStream, outStream) 56 | 57 | def copy[F[_]: Sync](origin: File, destination: File): F[Long] = 58 | inputOutputStreams(origin, destination).use { case (in, out) => 59 | transfer(in, out, new Array[Byte](1024 * 10), 0L) 60 | } 61 | 62 | override def run(args: List[String]): IO[ExitCode] = 63 | for { 64 | _ <- IO.raiseWhen(args.length < 2)(new IllegalArgumentException("Need origin and destination files")) 65 | orig = new File(args.head) 66 | dest = new File(args.tail.head) 67 | count <- copy[IO](orig, dest) 68 | _ <- IO.println(s"$count bytes copied from ${orig.getPath} to ${dest.getPath}") 69 | } yield ExitCode.Success 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/scala/catseffecttutorial/producerconsumer/InefficientProducerConsumer.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Luis Rodero-Merino 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at. 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package catseffecttutorial.producerconsumer 17 | 18 | import cats.effect.{Async, ExitCode, IO, IOApp, Ref, Sync} 19 | import cats.effect.std.Console 20 | import cats.syntax.all._ 21 | 22 | import scala.collection.immutable.Queue 23 | import scala.concurrent.duration.DurationInt 24 | 25 | /** 26 | * Single producer - single consumer system using an unbounded concurrent queue. 27 | * 28 | * Second part of cats-effect tutorial at https://typelevel.org/cats-effect/tutorial/tutorial.html 29 | */ 30 | object InefficientProducerConsumer extends IOApp { 31 | 32 | def producer[F[_]: Async: Console](queueR: Ref[F, Queue[Int]], counter: Int): F[Unit] = 33 | for { 34 | _ <- Async[F].whenA(counter % 10000 == 0)(Console[F].println(s"Produced $counter items")) 35 | _ <- Async[F].sleep(1.microsecond) // To prevent overwhelming consumers 36 | _ <- queueR.getAndUpdate(_.enqueue(counter + 1)) 37 | _ <- producer(queueR, counter + 1) 38 | } yield () 39 | 40 | def consumer[F[_] : Sync: Console](queueR: Ref[F, Queue[Int]]): F[Unit] = 41 | for { 42 | iO <- queueR.modify{ queue => 43 | queue.dequeueOption.fold((queue, Option.empty[Int])){case (i,queue) => (queue, Option(i))} 44 | } 45 | _ <- Sync[F].whenA(iO.exists(_ % 10000 == 0))(Console[F].println(s"Consumed ${iO.get} items")) 46 | _ <- consumer(queueR) 47 | } yield () 48 | 49 | override def run(args: List[String]): IO[ExitCode] = 50 | for { 51 | queueR <- Ref.of[IO, Queue[Int]](Queue.empty[Int]) 52 | res <- (consumer(queueR), producer(queueR, 0)) 53 | .parMapN((_, _) => ExitCode.Success) // Run producer and consumer in parallel until done (likely by user cancelling with CTRL-C) 54 | .handleErrorWith { t => 55 | Console[IO].errorln(s"Error caught: ${t.getMessage}").as(ExitCode.Error) 56 | } 57 | } yield res 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/scala/catseffecttutorial/producerconsumer/ProducerConsumer.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Luis Rodero-Merino 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at. 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package catseffecttutorial.producerconsumer 17 | 18 | import cats.effect._ 19 | import cats.effect.std.Console 20 | import cats.instances.list._ 21 | import cats.syntax.all._ 22 | 23 | import scala.collection.immutable.Queue 24 | import scala.concurrent.duration.DurationInt 25 | 26 | /** 27 | * Multiple producer - multiple consumer system using an unbounded concurrent queue. 28 | * 29 | * Second part of cats-effect tutorial at https://typelevel.org/cats-effect/tutorial/tutorial.html 30 | * 31 | * Code to _offer_ and _take_ elements to/from queue is taken from CE3's Queue implementation. 32 | */ 33 | object ProducerConsumer extends IOApp { 34 | 35 | case class State[F[_], A](queue: Queue[A], takers: Queue[Deferred[F,A]]) 36 | 37 | object State { 38 | def empty[F[_], A]: State[F, A] = State(Queue.empty, Queue.empty) 39 | } 40 | 41 | def producer[F[_]: Async: Console](id: Int, counterR: Ref[F, Int], stateR: Ref[F, State[F,Int]]): F[Unit] = { 42 | 43 | def offer(i: Int): F[Unit] = 44 | stateR.modify { 45 | case State(queue, takers) if takers.nonEmpty => 46 | val (taker, rest) = takers.dequeue 47 | State(queue, rest) -> taker.complete(i).void 48 | case State(queue, takers) => 49 | State(queue.enqueue(i), takers) -> Sync[F].unit 50 | }.flatten 51 | 52 | for { 53 | i <- counterR.getAndUpdate(_ + 1) 54 | _ <- offer(i) 55 | _ <- Async[F].whenA(i % 100000 == 0)(Console[F].println(s"Producer $id has reached $i items")) 56 | _ <- Async[F].sleep(1.microsecond) // To prevent overwhelming consumers 57 | _ <- producer(id, counterR, stateR) 58 | } yield () 59 | } 60 | 61 | def consumer[F[_]: Async: Console](id: Int, stateR: Ref[F, State[F, Int]]): F[Unit] = { 62 | 63 | val take: F[Int] = 64 | Deferred[F, Int].flatMap { taker => 65 | stateR.modify { 66 | case State(queue, takers) if queue.nonEmpty => 67 | val (i, rest) = queue.dequeue 68 | State(rest, takers) -> Async[F].pure(i) 69 | case State(queue, takers) => 70 | State(queue, takers.enqueue(taker)) -> taker.get 71 | }.flatten 72 | } 73 | 74 | for { 75 | i <- take 76 | _ <- Async[F].whenA(i % 10000 == 0)(Console[F].println(s"Consumer $id has reached $i items")) 77 | _ <- consumer(id, stateR) 78 | } yield () 79 | } 80 | 81 | override def run(args: List[String]): IO[ExitCode] = 82 | for { 83 | stateR <- Ref.of[IO, State[IO,Int]](State.empty[IO, Int]) 84 | counterR <- Ref.of[IO, Int](1) 85 | producers = List.range(1, 11).map(producer(_, counterR, stateR)) // 10 producers 86 | consumers = List.range(1, 11).map(consumer(_, stateR)) // 10 consumers 87 | res <- (producers ++ consumers) 88 | .parSequence.as(ExitCode.Success) // Run producers and consumers in parallel until done (likely by user cancelling with CTRL-C) 89 | .handleErrorWith { t => 90 | Console[IO].errorln(s"Error caught: ${t.getMessage}").as(ExitCode.Error) 91 | } 92 | } yield res 93 | } 94 | -------------------------------------------------------------------------------- /src/main/scala/catseffecttutorial/producerconsumer/ProducerConsumerBounded.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Luis Rodero-Merino 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at. 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package catseffecttutorial.producerconsumer 17 | 18 | import cats.effect.{Async, Deferred, ExitCode, IO, IOApp, Ref} 19 | import cats.effect.std.Console 20 | import cats.instances.list._ 21 | import cats.syntax.all._ 22 | 23 | import scala.collection.immutable.Queue 24 | 25 | /** 26 | * Multiple producer - multiple consumer system using a bounded concurrent queue. 27 | * 28 | * Second part of cats-effect tutorial at https://typelevel.org/cats-effect/tutorial/tutorial.html 29 | * 30 | * Code to _offer_ and _take_ elements to/from queue is taken from CE3's Queue implementation. 31 | */ 32 | object ProducerConsumerBounded extends IOApp { 33 | 34 | case class State[F[_], A](queue: Queue[A], capacity: Int, takers: Queue[Deferred[F,A]], offerers: Queue[(A, Deferred[F,Unit])]) 35 | 36 | object State { 37 | def empty[F[_], A](capacity: Int): State[F, A] = State(Queue.empty, capacity, Queue.empty, Queue.empty) 38 | } 39 | 40 | def producer[F[_]: Async: Console](id: Int, counterR: Ref[F, Int], stateR: Ref[F, State[F,Int]]): F[Unit] = { 41 | 42 | def offer(i: Int): F[Unit] = 43 | Deferred[F, Unit].flatMap[Unit]{ offerer => 44 | stateR.modify { 45 | case State(queue, capacity, takers, offerers) if takers.nonEmpty => 46 | val (taker, rest) = takers.dequeue 47 | State(queue, capacity, rest, offerers) -> taker.complete(i).void 48 | case State(queue, capacity, takers, offerers) if queue.size < capacity => 49 | State(queue.enqueue(i), capacity, takers, offerers) -> Async[F].unit 50 | case State(queue, capacity, takers, offerers) => 51 | State(queue, capacity, takers, offerers.enqueue(i -> offerer)) -> offerer.get 52 | }.flatten 53 | } 54 | 55 | for { 56 | i <- counterR.getAndUpdate(_ + 1) 57 | _ <- offer(i) 58 | _ <- Async[F].whenA(i % 100000 == 0)(Console[F].println(s"Producer $id has reached $i items")) 59 | _ <- producer(id, counterR, stateR) 60 | } yield () 61 | } 62 | 63 | def consumer[F[_]: Async: Console](id: Int, stateR: Ref[F, State[F, Int]]): F[Unit] = { 64 | 65 | val take: F[Int] = 66 | Deferred[F, Int].flatMap { taker => 67 | stateR.modify { 68 | case State(queue, capacity, takers, offerers) if queue.nonEmpty && offerers.isEmpty => 69 | val (i, rest) = queue.dequeue 70 | State(rest, capacity, takers, offerers) -> Async[F].pure(i) 71 | case State(queue, capacity, takers, offerers) if queue.nonEmpty => 72 | val (i, rest) = queue.dequeue 73 | val ((move, release), tail) = offerers.dequeue 74 | State(rest.enqueue(move), capacity, takers, tail) -> release.complete(()).as(i) 75 | case State(queue, capacity, takers, offerers) if offerers.nonEmpty => 76 | val ((i, release), rest) = offerers.dequeue 77 | State(queue, capacity, takers, rest) -> release.complete(()).as(i) 78 | case State(queue, capacity, takers, offerers) => 79 | State(queue, capacity, takers.enqueue(taker), offerers) -> taker.get 80 | }.flatten 81 | } 82 | 83 | for { 84 | i <- take 85 | _ <- Async[F].whenA(i % 100000 == 0)(Console[F].println(s"Consumer $id has reached $i items")) 86 | _ <- consumer(id, stateR) 87 | } yield () 88 | } 89 | 90 | override def run(args: List[String]): IO[ExitCode] = 91 | for { 92 | stateR <- Ref.of[IO, State[IO, Int]](State.empty[IO, Int](capacity = 100)) 93 | counterR <- Ref.of[IO, Int](1) 94 | producers = List.range(1, 11).map(producer(_, counterR, stateR)) // 10 producers 95 | consumers = List.range(1, 11).map(consumer(_, stateR)) // 10 consumers 96 | res <- (producers ++ consumers) 97 | .parSequence.as(ExitCode.Success) // Run producers and consumers in parallel until done (likely by user cancelling with CTRL-C) 98 | .handleErrorWith { t => 99 | Console[IO].errorln(s"Error caught: ${t.getMessage}").as(ExitCode.Error) 100 | } 101 | } yield res 102 | } 103 | -------------------------------------------------------------------------------- /src/main/scala/catseffecttutorial/producerconsumer/ProducerConsumerBoundedCancelable.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Luis Rodero-Merino 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at. 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package catseffecttutorial.producerconsumer 17 | 18 | import cats.effect._ 19 | import cats.effect.std.Console 20 | import cats.instances.list._ 21 | import cats.syntax.all._ 22 | import cats.effect.syntax.all._ 23 | 24 | import scala.collection.immutable.Queue 25 | 26 | /** 27 | * Multiple producer - multiple consumer system using a bounded concurrent queue able to 28 | * handle cancellation. 29 | * 30 | * Second part of cats-effect tutorial at https://typelevel.org/cats-effect/tutorial/tutorial.html 31 | * 32 | * Code to _offer_ and _take_ elements to/from queue is taken from CE3's Queue implementation. 33 | */ 34 | object ProducerConsumerBoundedCancelable extends IOApp { 35 | 36 | case class State[F[_], A](queue: Queue[A], capacity: Int, takers: Queue[Deferred[F,A]], offerers: Queue[(A, Deferred[F,Unit])]) 37 | 38 | object State { 39 | def empty[F[_], A](capacity: Int): State[F, A] = State(Queue.empty, capacity, Queue.empty, Queue.empty) 40 | } 41 | 42 | def producer[F[_]: Async: Console](id: Int, counterR: Ref[F, Int], stateR: Ref[F, State[F,Int]]): F[Unit] = { 43 | 44 | def offer(i: Int): F[Unit] = 45 | Deferred[F, Unit].flatMap[Unit]{ offerer => 46 | Async[F].uncancelable { poll => // `poll` used to embed cancelable code, i.e. the call to `offerer.get` 47 | stateR.modify { 48 | case State(queue, capacity, takers, offerers) if takers.nonEmpty => 49 | val (taker, rest) = takers.dequeue 50 | State(queue, capacity, rest, offerers) -> taker.complete(i).void 51 | case State(queue, capacity, takers, offerers) if queue.size < capacity => 52 | State(queue.enqueue(i), capacity, takers, offerers) -> Async[F].unit 53 | case State(queue, capacity, takers, offerers) => 54 | val cleanup = stateR.update { s => s.copy(offerers = s.offerers.filter(_._2 ne offerer)) } 55 | State(queue, capacity, takers, offerers.enqueue(i -> offerer)) -> poll(offerer.get).onCancel(cleanup) 56 | }.flatten 57 | } 58 | } 59 | 60 | for { 61 | i <- counterR.getAndUpdate(_ + 1) 62 | _ <- offer(i) 63 | _ <- Async[F].whenA(i % 100000 == 0)(Console[F].println(s"Producer $id has reached $i items")) 64 | _ <- producer(id, counterR, stateR) 65 | } yield () 66 | } 67 | 68 | def consumer[F[_]: Async: Console](id: Int, stateR: Ref[F, State[F, Int]]): F[Unit] = { 69 | 70 | val take: F[Int] = 71 | Deferred[F, Int].flatMap { taker => 72 | Async[F].uncancelable { poll => 73 | stateR.modify { 74 | case State(queue, capacity, takers, offerers) if queue.nonEmpty && offerers.isEmpty => 75 | val (i, rest) = queue.dequeue 76 | State(rest, capacity, takers, offerers) -> Async[F].pure(i) 77 | case State(queue, capacity, takers, offerers) if queue.nonEmpty => 78 | val (i, rest) = queue.dequeue 79 | val ((move, release), tail) = offerers.dequeue 80 | State(rest.enqueue(move), capacity, takers, tail) -> release.complete(()).as(i) 81 | case State(queue, capacity, takers, offerers) if offerers.nonEmpty => 82 | val ((i, release), rest) = offerers.dequeue 83 | State(queue, capacity, takers, rest) -> release.complete(()).as(i) 84 | case State(queue, capacity, takers, offerers) => 85 | val cleanup = stateR.update { s => s.copy(takers = s.takers.filter(_ ne taker)) } 86 | State(queue, capacity, takers.enqueue(taker), offerers) -> poll(taker.get).onCancel(cleanup) 87 | }.flatten 88 | } 89 | } 90 | 91 | for { 92 | i <- take 93 | _ <- Async[F].whenA(i % 100000 == 0)(Console[F].println(s"Consumer $id has reached $i items")) 94 | _ <- consumer(id, stateR) 95 | } yield () 96 | } 97 | 98 | override def run(args: List[String]): IO[ExitCode] = 99 | for { 100 | stateR <- Ref.of[IO, State[IO, Int]](State.empty[IO, Int](capacity = 100)) 101 | counterR <- Ref.of[IO, Int](1) 102 | producers = List.range(1, 11).map(producer(_, counterR, stateR)) // 10 producers 103 | consumers = List.range(1, 11).map(consumer(_, stateR)) // 10 consumers 104 | res <- (producers ++ consumers) 105 | .parSequence.as(ExitCode.Success) // Run producers and consumers in parallel until done (likely by user cancelling with CTRL-C) 106 | .handleErrorWith { t => 107 | Console[IO].errorln(s"Error caught: ${t.getMessage}").as(ExitCode.Error) 108 | } 109 | } yield res 110 | } 111 | -------------------------------------------------------------------------------- /src/main/scala/catseffecttutorial/producerconsumer/exerciseconcurrentqueue/Main.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Luis Rodero-Merino 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at. 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package catseffecttutorial.producerconsumer.exerciseconcurrentqueue 17 | 18 | import cats.effect.{ExitCode, IO, IOApp, Ref} 19 | import cats.syntax.all._ 20 | 21 | 22 | /** 23 | * Multiple producer - multiple consumer system using a bounded concurrent queue able to 24 | * handle cancellation. 25 | * 26 | * Second part of cats-effect tutorial at https://typelevel.org/cats-effect/tutorial/tutorial.html 27 | */ 28 | object Main extends IOApp { 29 | 30 | private val console = IO.consoleForIO 31 | 32 | def producer(id: Int, counterR: Ref[IO, Int], queue: Queue[IO, Int]): IO[Unit] = 33 | for { 34 | i <- counterR.getAndUpdate(_ + 1) 35 | _ <- queue.offer(i) 36 | _ <- IO.whenA(i % 10000 == 0)(console.println(s"Producer $id has reached $i items")) 37 | _ <- producer(id, counterR, queue) 38 | } yield () 39 | 40 | def consumer(id: Int, queue: Queue[IO, Int]): IO[Unit] = 41 | for { 42 | i <- queue.take 43 | _ <- IO.whenA(i % 10000 == 0)(console.println(s"Consumer $id has reached $i items")) 44 | _ <- consumer(id, queue) 45 | } yield () 46 | 47 | override def run(args: List[String]): IO[ExitCode] = 48 | for { 49 | queue <- Queue[IO, Int](100) 50 | counterR <- Ref.of[IO, Int](1) 51 | producers = List.range(1, 11).map(producer(_, counterR, queue)) // 10 producers 52 | consumers = List.range(1, 11).map(consumer(_, queue)) // 10 consumers 53 | res <- (producers ++ consumers) 54 | .parSequence.as(ExitCode.Success) // Run producers and consumers in parallel until done (likely by user cancelling with CTRL-C) 55 | .handleErrorWith { t => 56 | console.errorln(s"Error caught: ${t.getMessage}").as(ExitCode.Error) 57 | } 58 | } yield res 59 | } 60 | -------------------------------------------------------------------------------- /src/main/scala/catseffecttutorial/producerconsumer/exerciseconcurrentqueue/Queue.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Luis Rodero-Merino 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at. 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * Implementation of a simple concurrent queue for cats-effect 3. This code is 17 | * just a stripped down version of the Queue implementation already available 18 | * in cats.effect.std package: 19 | * https://github.com/typelevel/cats-effect/blob/series/3.x/std/shared/src/main/scala/cats/effect/std/Queue.scala 20 | */ 21 | package catseffecttutorial.producerconsumer.exerciseconcurrentqueue 22 | 23 | import cats.effect.{Async, Deferred, Ref} 24 | import cats.syntax.all._ 25 | import cats.effect.syntax.all._ 26 | 27 | import scala.collection.immutable.{Queue => ScQueue} 28 | 29 | private final case class State[F[_], A](queue: ScQueue[A], capacity: Int, takers: ScQueue[Deferred[F,A]], offerers: ScQueue[(A, Deferred[F,Unit])]) 30 | 31 | private object State { 32 | def empty[F[_], A](capacity: Int): State[F, A] = State(ScQueue.empty[A], capacity, ScQueue.empty[Deferred[F,A]], ScQueue.empty[(A, Deferred[F, Unit])]) 33 | } 34 | 35 | class Queue[F[_]: Async, A](stateR: Ref[F, State[F, A]]) { 36 | 37 | val take: F[A] = 38 | Deferred[F, A].flatMap { taker => 39 | Async[F].uncancelable { poll => 40 | stateR.modify { 41 | case State(queue, capacity, takers, offerers) if queue.nonEmpty && offerers.isEmpty => 42 | val (i, rest) = queue.dequeue 43 | State(rest, capacity, takers, offerers) -> Async[F].pure(i) 44 | case State(queue, capacity, takers, offerers) if queue.nonEmpty => 45 | val (i, rest) = queue.dequeue 46 | val ((move, release), tail) = offerers.dequeue 47 | State(rest.enqueue(move), capacity, takers, tail) -> release.complete(()).as(i) 48 | case State(queue, capacity, takers, offerers) if offerers.nonEmpty => 49 | val ((i, release), rest) = offerers.dequeue 50 | State(queue, capacity, takers, rest) -> release.complete(()).as(i) 51 | case State(queue, capacity, takers, offerers) => 52 | val cleanup = stateR.update { s => s.copy(takers = s.takers.filter(_ ne taker)) } 53 | State(queue, capacity, takers.enqueue(taker), offerers) -> poll(taker.get).onCancel(cleanup) 54 | }.flatten 55 | } 56 | } 57 | 58 | def offer(a: A): F[Unit] = 59 | Deferred[F, Unit].flatMap[Unit]{ offerer => 60 | Async[F].uncancelable { poll => 61 | stateR.modify { 62 | case State(queue, capacity, takers, offerers) if takers.nonEmpty => 63 | val (taker, rest) = takers.dequeue 64 | State(queue, capacity, rest, offerers) -> taker.complete(a).void 65 | case State(queue, capacity, takers, offerers) if queue.size < capacity => 66 | State(queue.enqueue(a), capacity, takers, offerers) -> Async[F].unit 67 | case State(queue, capacity, takers, offerers) => 68 | val cleanup = stateR.update { s => s.copy(offerers = s.offerers.filter(_._2 ne offerer)) } 69 | State(queue, capacity, takers, offerers.enqueue(a -> offerer)) -> poll(offerer.get).onCancel(cleanup) 70 | }.flatten 71 | } 72 | } 73 | 74 | } 75 | 76 | object Queue { 77 | def apply[F[_]: Async, A](capacity: Int): F[Queue[F, A]] = 78 | Ref.of[F, State[F, A]](State.empty[F, A](capacity)).map(st => new Queue(st)) 79 | } 80 | 81 | --------------------------------------------------------------------------------