Skip to content

Instantly share code, notes, and snippets.

View ysbaddaden's full-sized avatar

Julien Portalier ysbaddaden

View GitHub Profile
@ysbaddaden
ysbaddaden / bus.cr
Last active September 21, 2023 22:24
A GenServer in Crystal
class Bus(A, M)
include GenServer
def initialize
@listeners = [] of A
end
# public interface
def subscribe(listener : A) : Nil
@ysbaddaden
ysbaddaden / stop-the-world.c
Last active June 21, 2022 14:12
Stop the World in C
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdatomic.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ucontext.h>
@ysbaddaden
ysbaddaden / rebar2_rewrite_github_git_protocol_to_https.patch
Created April 5, 2022 16:58
Fix for Rebar2 that transparently rewrites git://github.com into https://github.com (useful to bootstrap old legacy Erlang projects)
commit 921120d96c175d997d815805bdeced4b4fbb0cfa (HEAD -> fix/transparently-rewrite-unauthenticated-github-url-to-https)
Author: Julien Portalier <julien@portalier.com>
Date: Tue Apr 5 18:37:34 2022 +0200
Fix: rewrite GitHub unauthenticated URL
Rewrites the URL on-the-fly and transparently from git://github.com
to https://github.com because GitHub no longer supports the
unauthenticated git protocol anymore.
@ysbaddaden
ysbaddaden / earl_scheduler.rb
Last active January 8, 2021 09:34
A basic Scheduler for Ruby 3, leveraging the NIO and timers gems
# frozen_string_literal: true
require "fiber"
require "nio"
require "timers"
module Earl
class Scheduler
def initialize
@selector = NIO::Selector.new
@ysbaddaden
ysbaddaden / jwt
Created June 19, 2019 14:42
WINDEV: create JWT with HS256 signature
// Crée un JSON Web Token pour l'extranet, signé via HMAC SHA 256, permettant
// d'identifier le CaC sur l'extranet comme utilisateur ayant accès à toutes les
// données.
PROCÉDURE CréerTokenExtranet()
// header du token (algorithm, type) :
taHeader est un tableau associatif de chaînes
taHeader["alg"] = "HS256"
taHeader["typ"] = "JWT"
@ysbaddaden
ysbaddaden / shards_bootstrap.rb
Last active September 12, 2019 12:31
Crystal Shards bootstrap (Ruby)
#! /usr/bin/env ruby
#
# A bootstrap to install locked Crystal Shards dependencies. Written in Ruby so
# it doesn't need Crystal. This coul be useful to extract stdlib as distinct
# shards from the main crystal-lang/crystal repository, while still allowing to
# install and distribute them to build crystal, avoiding the crystal <-> shards
# requirement loop.
require "yaml"
# helpers:
@ysbaddaden
ysbaddaden / kube-rails.sh
Last active February 2, 2019 11:24
Run a bin/rails command in a Kubernetes Job
#! /usr/bin/env sh
ARGS=$*
TASK=$1
NAME=rails-task-$(echo $TASK | sed 's/:/-/g')
[ -z $TMP ] && TMP=tmp
[ -z $DOCKER_IMAGE ] && DOCKER_IMAGE=eu.gcr.io/project/app:latest
cleanup() {
# delete the temporary yaml definition and delete the Kubernetes job:
@ysbaddaden
ysbaddaden / MT.md
Last active October 30, 2018 15:31

Crystal MTs (Multi Threaded schedulers)

Goals:

  • developers must only care about fibers and channels;
  • crystal runtime should start 1 upto N threads;
  • each thread can take & resume any fiber in the loop;
  • channels as user-code sync primitives;
  • also propose fiber-aware mutex/monitors;
  • additional sync primitives (e.g. IO events, ...)
@ysbaddaden
ysbaddaden / sample.cr
Created August 17, 2018 22:57
Syslog Logger for Crystal (pure)
require "./syslog"
syslog = Syslog.new
syslog.info "bla bla bla"
# $ tail -n1 /var/log/syslog
# Aug 18 00:55:05 xps crystal-run-syslog.tmp[27911]: bla bla bla
@ysbaddaden
ysbaddaden / hasher_randomize_distributions_test.cr
Created October 21, 2017 12:49
Search for collisions and display distribution (libsdl2) of hashes for a list of patterns (e.g. words, uuids, numbers, ...) from STDIN
# Requires https://github.com/ysbaddaden/sdl.cr
require "sdl"
# Depends on the Crystal::Hasher implementation.
# Adjust to match the type of `"".hash`
alias HashType = UInt64
word_count = 0
maximum = 0
collisions = 0