Skip to content

Instantly share code, notes, and snippets.

View adamw's full-sized avatar

Adam Warski adamw

View GitHub Profile
@adamw
adamw / chat.scala
Last active September 6, 2024 20:40
//> using dep com.softwaremill.sttp.openai::ox:0.2.2
//> using dep com.softwaremill.sttp.tapir::tapir-netty-server-sync:1.11.2
//> using dep com.softwaremill.sttp.client4::ox:4.0.0-M17
//> using dep com.softwaremill.ox::core:0.3.6
//> using dep ch.qos.logback:logback-classic:1.5.7
// Remember to set the OPENAI_KEY env variable!
package examples
# VERSION 1.0
# the base image is a trusted ubuntu build with java 7 (https://index.docker.io/u/dockerfile/java/)
FROM dockerfile/java
# that's me!
MAINTAINER Adam Warski, adam@warski.org
# we need this because the workdir is modified in dockerfile/java
WORKDIR /
import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.spi.LogbackServiceProvider
import org.slf4j.spi.{MDCAdapter, SLF4JServiceProvider}
import org.slf4j.{ILoggerFactory, IMarkerFactory, LoggerFactory, MDC}
import ox.{ForkLocal, pipe, tap}
/** Provides support for MDC which is inheritable across (virtual) threads. Only MDC values set using the [[where]] method will be
* inherited; this method also defines the scope, within which the provided MDC values are available.
*
* The semantics of [[MDC.put]] are unchanged: values set using this method will only be visible in the original thread. That is because
//> using dep com.softwaremill.sttp.tapir::tapir-netty-server-sync:1.10.7
import ox.channels.{Actor, ActorRef, Channel, ChannelClosed, Default, DefaultResult, selectOrClosed}
import ox.{fork, releaseAfterScope, supervised}
import sttp.tapir.*
import sttp.tapir.CodecFormat.*
import sttp.tapir.server.netty.sync.{Id, NettySyncServer, OxStreams}
import java.util.UUID
package brc
import monix.eval.Task
import monix.execution
import monix.reactive.Observable
import java.io.{BufferedReader, FileInputStream, InputStreamReader}
import java.text.DecimalFormat
object UsingMonix:
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.concurrent.Exchanger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
// with changes inspired by Exchanger
public class Rendezvous2 {
private volatile Thread waiting;
private volatile int data = -1; // together with `consumed`, used to transmit data if t1 wins the race (and waits for t2)
import cats.effect.{Deferred, IO, Ref}
import cats.effect.std.Queue
import cats.effect.unsafe.implicits.global
import java.util.concurrent.Executors
import scala.concurrent.ExecutionContext
// max times rendezvous using cats-effect's synchronous queue
def rendezvousUsingCatsEffect(): Unit =
val max = 10_000_000
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
public class Rendezvous {
private final int spinIterations;
private final int yieldIterations;
private final AtomicReference<ThreadAndCell> waiting = new AtomicReference<>();
public Rendezvous(int spinIterations, int yieldIterations) {
this.spinIterations = spinIterations;
import java.util.concurrent.Exchanger;
import java.util.concurrent.SynchronousQueue;
public class RendezvousUsingExchanger {
public static void test() throws Exception {
long startTime = System.currentTimeMillis();
final int max = 10_000_000;
Exchanger<Integer> data = new Exchanger<>();
Thread t1 = Thread.ofVirtual().start(() -> {
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
// -Djdk.virtualThreadScheduler.parallelism=1 -Djdk.virtualThreadScheduler.maxPoolSize=1 -Djdk.virtualThreadScheduler.minRunnable=1
public class Rendezvous {
private final int spinIterations;
private final int yieldIterations;
private final AtomicReference<ThreadAndCell> waiting = new AtomicReference<>();
public Rendezvous(int spinIterations, int yieldIterations) {