Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home

Tony Han tony612

🏠
Working from home
View GitHub Profile
View README.md

Erlang honors cgroup CPU quota from Erlang 23. But when less than 1 CPU is used, schedulers_online will be cores number(see below k8s 0-5).

docker

$ docker run --rm -it --cpu-period 100000 --cpu-quota 100000 erlang:23 bash
root@3f793576001a:/# erl
Erlang/OTP 23 [erts-11.0.2] [source] [64-bit] [smp:4:1] [ds:4:1:10] [async-threads:1] [hipe]
@tony612
tony612 / read_until_eof.py
Created Feb 17, 2020
Python read from stdin
View read_until_eof.py
def read_until_eof():
buffer = ''
batch_size = 1024
while True:
ready, _, _ = select.select([sys.stdin], [], [], 0.0)
if sys.stdin not in ready:
continue
# Don't know why, but sys.stdin.read() and sys.stdin.readline()
# will cause EPIPE when writting to stdout
View mmap_test.c
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
int main(void) {
size_t pagesize = getpagesize();
printf("System page size: %zu bytes\n", pagesize);
View binary_parse_slow.exs
defmodule BinaryParseSlow do
def parse(bin) do
parse(bin, 0)
end
def parse(bin, acc) do
case do_parse(bin) do
{:nofin, x, rest} ->
parse(rest, acc + x)
{:fin, x} ->
View binary_parse_fast.exs
# For binary 0000 0001 0100 0000 1000 1000
# The result is 0b1 + 0b100_0000 + 0b1000 = 73
defmodule BinaryParseFast do
def parse(bin) do
parse(bin, 0)
end
# A byte beginning with 0 means the end, but still need to add the last 7 bits
def parse(<<0::1, x::7, _::bits>>, acc), do: acc + x
# A byte beginning with 1 means there is more data needed to be processed.
View generate_all_float.ex
for a1 <- 0..1,
a2 <- 0..1,
a3 <- 0..1,
a4 <- 0..1,
a5 <- 0..1,
a6 <- 0..1,
a7 <- 0..1,
a8 <- 0..1,
a9 <- 0..1,
@tony612
tony612 / binary_parse_fast.exs
Last active Jul 25, 2019
Erlang binary parsing comparing
View binary_parse_fast.exs
# For 0000 0001 0100 0000 1000 1000
# The result is 0b1 + 0b100_0000 + 0b1000 = 73
defmodule BinaryParseFast do
def parse(bin) do
parse(bin, 0)
end
# def parse(<<0::1, x::7, _::bits>>, acc), do: acc + x
# def parse(<<1::1, x::7, rest::bits>>, acc) do
# parse(rest, acc + x)
@tony612
tony612 / bench.exs
Last active Jul 22, 2019
Bench pattern match with functions
View bench.exs
defmodule Foo do
Enum.each(1..100, fn i ->
def field(unquote(:"foo#{i}")) do
"field #{unquote(i)}"
end
def unquote(:"field_foo#{i}")() do
"field #{unquote(i)}"
end
end)
@tony612
tony612 / test.js
Last active Jul 3, 2019
Test node protobuf float
View test.js
var protobuf = require('protobufjs');
protobuf.load("test.proto", function (err, root) {
if (err)
throw err;
var Message = root.lookupType("test.Message");
var payload = { foo1: 123.0, foo2: 123.5 };
var errMsg = Message.verify(payload);
View sentry_fingerprinter.ex
defmodule Sentry.Fingerprinter do
def custom_fingerprints(metadata, _msg) do
file = Keyword.get(metadata, :file)
line = Keyword.get(metadata, :line)
if file && line do
%{
application: Keyword.get(metadata, :application),
module: Keyword.get(metadata, :module),
function: Keyword.get(metadata, :function),
You can’t perform that action at this time.