Skip to content

Instantly share code, notes, and snippets.

@tony612
tony612 / json_parse_compare.go
Created January 10, 2025 02:36
json_parse_compare
// 嵌套 JSON 解析耗时: 491.875µs
// 打平 JSON 解析耗时: 67.083µs
package main
import (
"encoding/json"
"fmt"
"time"
)
@tony612
tony612 / go_string_panic.go
Last active December 6, 2024 08:14
go_string_panic
// panic: s should not be WHAT
// goroutine 1 [running]:
// main.main()
// main.go:44 +0x19c
// exit status 2
// panic: runtime error: invalid memory address or nil pointer dereference
// [signal SIGSEGV: segmentation violation code=0x2 addr=0x7 pc=0x102fa19a4]
@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} ->