Skip to content

Instantly share code, notes, and snippets.

@tony612
tony612 / gencert.go
Last active June 14, 2023 04:48
generate x509 cert and key
package main
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
package main
// Run this in root of https://github.com/vscode-kubernetes-tools/vscode-kubernetes-tools
// `snippets` dir is used.
// Somehow vscode can't show snippets by default.
import (
"encoding/json"
"fmt"
"io/fs"
@tony612
tony612 / client.go
Created February 24, 2022 09:43 — forked from xjdrew/client.go
golang tls client and server, require and verify certificate in double direction
package main
import (
"crypto/tls"
"crypto/x509"
"flag"
"io"
"io/ioutil"
"log"
"os"
export USERNAME=myadmin
export GROUPNAME=myadmin
export CERTIFICATE_NAME=myadmin
openssl genrsa -out ${USERNAME}.key 2048
openssl req -new -key ${USERNAME}.key -out ${USERNAME}.csr -subj "/CN=${USERNAME}/O=${GROUPNAME}"
cat <<EOF | kubectl apply -f -
apiVersion: certificates.k8s.io/v1beta1
kind: CertificateSigningRequest

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 February 17, 2020 09:33
Python read from stdin
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
#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);
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} ->
# 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.
@tony612
tony612 / generate_all_float.ex
Created July 30, 2019 03:22
Generate all float
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,