Skip to content

Instantly share code, notes, and snippets.

Avatar

Alexey Soshin AlexeySoshin

View GitHub Profile
View Ktor HTTPS Test Server.kt
import io.ktor.network.tls.certificates.buildKeyStore
import io.ktor.network.tls.certificates.saveToFile
import io.ktor.network.tls.extensions.HashAlgorithm
import io.ktor.network.tls.extensions.SignatureAlgorithm
import io.ktor.routing.routing
import io.ktor.server.engine.applicationEngineEnvironment
import io.ktor.server.engine.embeddedServer
import io.ktor.server.engine.sslConnector
import io.ktor.server.netty.Netty
import io.ktor.server.netty.NettyApplicationEngine
@AlexeySoshin
AlexeySoshin / go_future.go
Last active Aug 15, 2020
Part of "Build your own Future in Go" article
View go_future.go
func (this *future[T]) Get() Result[T] {
if this.completed {
return this.result
} else {
fmt.Println("Need to wait...")
select {
case <-this.wait:
return this.result
case <-this.ctx.Done():
return &result(T){failure: this.ctx.Err()}
View KIO Recovery.kt
sealed class KIO<out A : Any?> {
...
fun <B> recover(f: (Throwable) -> KIO<B>): Recovery<B> {
return Recovery(this, f)
}
}
...
View kio-run-nullable.kt
fun main() {
Runtime.unsafeRunSync(
Effect {
// Generate some random UUID
if (Random().nextBoolean()) {
UUID.randomUUID().toString()
}
else {
null
}
View running-kio.kt
fun main() {
Runtime.unsafeRunSync(
Effect {
// Generate some random UUID
UUID.randomUUID().toString()
}.map {
// Print it
println("UUID: $it")
}
)
View kio-runtime.kt
object Runtime {
fun <A> unsafeRunSync(kio: KIO<A>): A = eval(kio)
fun <A> eval(kio: KIO<A>): A {
return when (kio) {
is Effect<A> -> kio.f()
is FlatMap<*, A> -> {
val res: Any? = eval(kio.input)
val f = kio.f as (Any?) -> KIO<A>
eval(f(res))
View kio-algebra.kt
sealed class KIO<out A> {
fun <B> flatMap(f: (A) -> KIO<B>): KIO<B> {
return FlatMap(this, f)
}
fun <B> map(f: (A) -> B): KIO<B> {
return flatMap { a ->
Effect {
f(a)
}
View Virtual thread with sleep.java
for (var i = 0; i < 1_000_000; i++) {
Thread.startVirtualThread(() -> {
c.incrementAndGet();
try {
Thread.sleep(1_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
View Virtual thread starvation.java
var threads = new ArrayList<Thread>();
var cores = 10;
for (var i = 0; i < cores; i++) {
var t = Thread.startVirtualThread(() -> {
var bestUUID = "";
for (var j = 0; j < 1_000_000; j++) {
var currentUUID = UUID.randomUUID().toString();
if (currentUUID.compareTo(bestUUID) > 0) {
bestUUID = currentUUID;
}
View gRPC with Gradle and Bazel - build.gradle.kts
import com.google.protobuf.gradle.*
...
protobuf {
...
generatedFilesBaseDir = "$buildDir/generated-sources"
...
dependencies {
protobuf(files(project.properties["protoDir"].toString()))
}
generateProtoTasks {
You can’t perform that action at this time.