Skip to content

Instantly share code, notes, and snippets.

View ChristopherDavenport's full-sized avatar

Christopher Davenport ChristopherDavenport

View GitHub Profile
@ChristopherDavenport
ChristopherDavenport / Ref.go
Created January 24, 2024 15:02
Basic Typed Concurrency
package concurrent
import (
"sync/atomic"
)
type Ref[A any] interface {
Get() A
Set(a A)
UpdateAndGet(f func (A) (A)) A
GetAndSet(a A) A
@ChristopherDavenport
ChristopherDavenport / SingleRoutine.go
Last active March 31, 2024 14:26
SingleRoutine Failed Idea
package single_routine
import (
"log"
"sync"
"sync/atomic"
)
type deferred[B any] struct {
b B
@ChristopherDavenport
ChristopherDavenport / SegmentAllowList.scala
Created July 10, 2023 14:43
Segment Allow List Implementation
import org.http4s.RequestPrelude
import org.typelevel.ci.CIString
object SegmentAllowList {
def fromAllowList(set: Set[CIString])(req: RequestPrelude): String = {
classifier(req)((s: String) => if (set.contains(CIString(s))) SegmentFilter.LeaveAsIs else SegmentFilter.star)
}
@ChristopherDavenport
ChristopherDavenport / App.scala
Last active June 16, 2023 01:11
Random Benchmarks around Ember and
import cats.syntax.all._
import cats.effect._
import fs2._
import fs2.io.net._
import com.comcast.ip4s._
object CheatApp extends IOApp {
val parallelism = Int.MaxValue
@ChristopherDavenport
ChristopherDavenport / SoftCancel.scala
Last active June 29, 2022 18:43
Soft Cancellation, moving cancellation into a Supervisor so you can cancel in flow, but not cancel what is happening.
package io.chrisdavenport.softcancel
import cats.effect._
import cats.effect.std._
import cats.syntax.all._
object SoftCancel {
def makeSoftCancelable[F[_]: Concurrent, A](fa: F[A], supervisor: Supervisor[F]): F[A] = {
supervisor.supervise(fa)
@ChristopherDavenport
ChristopherDavenport / UnsafeRef.scala
Created January 22, 2022 04:22
Unsafe Ref - For when you need it
import cats.effect._
import cats.syntax.all._
import java.util.concurrent.atomic.AtomicReference
import scala.annotation.tailrec
trait UnsafeRef[A]{
def get: A
def set(a: A): Unit
def getAndSet(a: A): A
@ChristopherDavenport
ChristopherDavenport / UnsafeDeferred.scala
Created January 22, 2022 03:21
UnsafeDeferred - Unsafe Set, Safe Get Deferred.
import scala.annotation.tailrec
import scala.collection.immutable.LongMap
import java.util.concurrent.atomic.AtomicReference
import cats.effect._
import cats.syntax.all._
trait UnsafeDeferred[F[_], A] extends cats.effect.kernel.DeferredSource[F, A]{
def complete(a: A): Boolean
}
@ChristopherDavenport
ChristopherDavenport / FunctionalTermSize.scala
Last active January 20, 2022 15:36
Functional Term Size
object Winch {
// https://nodejs.org/api/tty.html
case class Size(cols: Int, lines: Int)
def build[F[_]: Async]: Resource[F, fs2.concurrent.Signal[F, Size]] =
Resource.eval(getCurrentSizeExternal).flatMap{
Stream.repeatEval(singleEvent >> getCurrentSizeExternal)
.holdResource(_)
}
import cats._
import cats.effect.{Sync, Async}
import cats.effect.kernel.MonadCancel
import cats.~>
// Like Deferred except we don't try to do anything for multiple readers/writers.
// The expectation is that this will ONLY be used to write once
// While one fiber will wait for a callback eventually.
// Anything else results in failure.
final class RWDeferred[F[_]: Async, A] private (
@ChristopherDavenport
ChristopherDavenport / NCSECommonLog.scala
Created December 11, 2021 19:27
NCSE Common Log - Access Log
package io.chrisdavenport.http4s.logging
import cats._
import cats.syntax.all._
import cats.data._
import cats.effect._
import cats.effect.syntax.all._
import org.http4s._
import com.comcast.ip4s._
import java.time.format.DateTimeFormatter