Created
July 13, 2024 12:58
-
-
Save keynmol/2ed8b6b0255a006f3f23cd3b201bb060 to your computer and use it in GitHub Desktop.
Scala Native 0.5, Gears, Roach, Postgres, multi-threading
This file contains 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
//> using dep "ch.epfl.lamp::gears::0.2.0" | |
//> using dep "com.indoorvivants.roach::core::0.1.0" | |
//> using nativeVersion 0.5.4 | |
//> using option -Wunused:all | |
//> using platform "scala-native" | |
//> using repository sonatype:snapshots | |
//> using scala 3.5.0-RC4 | |
import gears.async.*, default.given | |
import roach.*, codecs.* | |
import scalanative.unsafe.Zone | |
val connectionString = | |
"postgresql://postgres:mysecretpassword@localhost:5432/postgres" | |
def read(limit: Int)(using Database, Zone): Vector[(String, Boolean)] = | |
// select some data from postgres | |
sql"select typname, typisdefined from pg_type limit $int4" | |
// pass the parameter, and read the results as a (String, Boolean) tuple | |
.all(limit, name ~ bool) | |
@main def hello = | |
val builder = List.newBuilder[(String, Boolean)] | |
val parallelism = 500 | |
val limit = 100 | |
Zone: | |
Async.blocking: | |
// create shared postgres connection | |
Pool.single(connectionString): pool => | |
List | |
.fill(parallelism): | |
Future: | |
// lease a connection | |
pool.withLease: | |
read(limit).foreach: (name, b) => | |
builder.synchronized: | |
builder += name -> b | |
.awaitAll | |
assert(builder.result().size == parallelism * limit) | |
assert(builder.result().distinct.size == limit) | |
println("🔥Scala Native is multi-threaded!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment