Skip to content

Instantly share code, notes, and snippets.

View Szer's full-sized avatar

Ayrat Hudaygulov Szer

  • Thrive Global
  • Dublin, Ireland
  • X @omgszer
View GitHub Profile
@Szer
Szer / MagicLinkTokenVerifier.kt
Created January 19, 2022 15:20
Example of verifying and signing of MagicLink token
// org.apache.tuweni:tuweni-crypto:2.0.0
import org.apache.tuweni.bytes.Bytes
import org.apache.tuweni.crypto.Hash
import org.apache.tuweni.crypto.SECP256K1
import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.slf4j.LoggerFactory
import java.security.Security
import java.time.Clock
import java.time.Duration
@Szer
Szer / PoolWithEviction.kt
Last active September 29, 2021 18:17
thread-safe ObjectPool with growth, shrinking and limits
package com.thriveglobal.reset
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import org.slf4j.LoggerFactory
import java.io.Closeable
import java.util.concurrent.LinkedBlockingDeque
import kotlin.time.Duration
@Szer
Szer / early-return.fs
Created September 15, 2021 22:55
Early return vs non-early return CE
type NoEarlyReturn() =
member _.Return x = [x]
member _.Combine (x, y) = x @ y
member _.Delay f = f()
let wtf = NoEarlyReturn()
wtf {
return 1
return 2
@Szer
Szer / struct-builder.fs
Last active September 2, 2021 21:45
Mutable Dictionary DSL
open System.Collections.Generic
[<Struct>]
type DictBuilder<'a, 'b when 'a: equality> =
val d: Dictionary<'a,'b>
new(()) = { d = Dictionary<'a, 'b>() }
member this.Yield (key, value) =
this.d.Add(key, value)
member this.Zero() = this.d
member _.Combine(_, _) = ()
#r "nuget: Hopac"
open System
open System.Threading
open Hopac
// ------- IMPLEMENTATION -------
type IQueue<'a> =
abstract member Dequeue: Alt<'a>
@Szer
Szer / custom_jackson.kt
Created July 20, 2021 18:32
custom_jackson
object CustomDeserToTheRescue :
ApplicationFeature<ApplicationCallPipeline, Unit, CustomDeserToTheRescue> {
override val key: AttributeKey<CustomDeserToTheRescue> = AttributeKey("CustomDeserToTheRescue")
override fun install(
pipeline: ApplicationCallPipeline,
configure: Unit.() -> Unit
): CustomDeserToTheRescue {
pipeline.receivePipeline.intercept(ApplicationReceivePipeline.Transform) { receive ->
// skip if already transformed
@Szer
Szer / UUID.parse.kt
Last active July 6, 2021 17:02
Parsing of UUID
import java.util.*
import java.util.UUID
// java.util.UUID.fromString doesn't enforce string representation of UUID https://www.ietf.org/rfc/rfc4122.txt
// It requires 4 '-' to present with any hex numbers in between. So it treats 1-1-1-1-1 as a valid UUID
// I don't think it's valid string representation of UUID. RFC4122 also doesn't think so
// Open JDK bug: https://bugs.openjdk.java.net/browse/JDK-8216407
// That's why we have our own UUID parser here
// Taken from here https://github.com/openjdk/jdk/blob/f485171ce8c7e9c9d7d2c24e1807efaa6ff137e8/src/java.base/share/classes/java/util/UUID.java#L212-L259
// But without "wrong" part
@Szer
Szer / mapUnordered.cs
Created July 3, 2021 09:59
Tasks pipelining
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
public static class TaskExt
{
@Szer
Szer / Program.fs
Created May 20, 2021 07:53
That's the minimum giraffe sample?
open Microsoft.AspNetCore
open Microsoft.AspNetCore.Hosting
open Giraffe
WebHost
.CreateDefaultBuilder()
.UseKestrel()
.Configure(fun a -> a.UseGiraffe(route "/" >=> POST >=> bindJson json))
.ConfigureServices(fun s -> s.AddGiraffe() |> ignore)
.Build()
@Szer
Szer / encoding_bench.cs
Created January 4, 2021 17:00
Custom Base64 encoding benches
using System;
using System.Buffers;
using System.Globalization;
using System.Text;
using BenchmarkDotNet;
using BenchmarkDotNet.Attributes;
namespace BenchmarkSuite1
{