Skip to content

Instantly share code, notes, and snippets.

View samspills's full-sized avatar
💜

Sam Pillsworth samspills

💜
View GitHub Profile
@samspills
samspills / Blerfaphore.scala
Last active July 29, 2024 19:15
a simple semaphore that can support FIFO, LIFO, and maybe eventually priority
//> using scala "2.13.14"
//> using dep "org.typelevel::cats-effect:3.5.4"
//> using dep "org.typelevel::cats-core:2.12.0"
import cats.effect.{IO, IOApp}
import cats.syntax.all._
import cats.effect.kernel.syntax.all._
import cats.effect.kernel.GenConcurrent
import cats.effect.kernel.Deferred
@samspills
samspills / MBean.scala
Last active July 20, 2023 13:21
[wip] how to trigger a CE fiber dump programatically
//> using scala "2.13.11"
//> using lib "org.typelevel::cats-effect:3.5.1"
import java.lang.management.ManagementFactory
import javax.management.ObjectName
import javax.management.Query
import scala.collection.JavaConverters.asScalaSetConverter
import javax.management.MBeanServer
//> using lib "co.fs2::fs2-core:3.5.0"
//> using lib "org.typelevel::cats-effect:3.4.6"
import cats.effect.{IO, IOApp}
import fs2.Stream
import scala.concurrent.duration._
object StreamingChunksIO extends IOApp.Simple {
def mapCheck = Stream
//> using lib "co.fs2::fs2-core:3.5.0"
//> using lib "org.typelevel::cats-effect:3.4.6"
import fs2.Stream
object StreamingChunks {
def mapCheck = Stream
.emits(0 until 100)
.chunkN(10)
@samspills
samspills / BlerfCache.scala
Last active April 21, 2023 18:14
cache that handles its own updates
case class BlerfCache(
blerfs: Ref[IO, Map[Id, Blerf]],
private val getBlerfMap: IO[Map[Id, Blerf]],
) extends Logging {
def updateBlerfs: IO[Unit] = getBlerfMap.flatMap(b => blerfs.set(b))
def refreshCache: IO[Unit] =
Stream
.repeatEval(logger.debug("refreshing blerf...") *> updateBlerfs)
.metered(5.minutes)
@samspills
samspills / JwksApp.scala
Created April 13, 2023 20:26
toy example that runs a mock JWKS server and demonstrates decoding a JWT token using a public jwk
//> using scala "2.13.10"
//> using lib "org.typelevel::cats-effect:3.4.8"
//> using lib "org.http4s::http4s-ember-client:0.23.18"
//> using lib "org.http4s::http4s-ember-server:0.23.18"
//> using lib "org.http4s::http4s-server:0.23.18"
//> using lib "org.http4s::http4s-dsl:0.23.18"
//> using lib "org.http4s::http4s-circe:0.23.18"
//> using lib "io.circe::circe-core:0.14.5"
//> using lib "io.circe::circe-generic:0.14.5"
//> using lib "org.scodec::scodec-bits:1.1.37"
@samspills
samspills / scheduledmetered.scala
Last active March 24, 2023 12:59
how does metered work
//> using lib "co.fs2::fs2-core:3.4.0"
//> using lib "org.typelevel::cats-effect:3.4.4"
/** Toy example with with metered stream. What happens if a stream element takes longer than metering?
*/
import fs2.Stream
import cats.effect.IO
import cats.effect.IOApp
import scala.concurrent.duration._
import cats.effect.kernel.Ref
@samspills
samspills / 3506Example.scala
Created March 21, 2023 20:29
example for #3506
//> using lib "org.typelevel::cats-effect:3.4.8"
import cats.effect.{IO, IOApp}
import cats.effect.std.Dispatcher
import scala.concurrent.duration._
object Example extends IOApp.Simple {
def run: IO[Unit] =
Dispatcher.parallel[IO](await = true).use { dispatcher =>
@samspills
samspills / 3131Example.scala
Created March 7, 2023 02:59
CE 3131 example (maybe)
import cats.effect.std.Dispatcher
import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, Future}
import cats.effect.unsafe.implicits.global
object StackTraceExample {
case class SomeError(message: String) extends Exception
val (dispatcher, dispatcherShutdown) = Dispatcher.parallel[IO].allocated.unsafeRunSync()
@samspills
samspills / OpensearchRepro.scala
Created February 17, 2023 19:48
opensearch reproduction start
//> using lib "org.typelevel::cats-effect:3.4.7"
//> using lib "org.http4s::http4s-core:0.23.18"
//> using lib "org.http4s::http4s-dsl:0.23.18"
//> using lib "org.http4s::http4s-ember-client:0.23.18"
//> using lib "org.http4s::http4s-client:0.23.18"
//> using lib "org.http4s::http4s-circe:0.23.18"
//> using lib "io.circe::circe-core:0.14.4"
//> using lib "io.circe::circe-literal:0.14.4"
import cats.effect.kernel.Resource