Skip to content

Instantly share code, notes, and snippets.

@tony612
tony612 / binary_parse_fast.exs
Last active July 25, 2019 07:54
Erlang binary parsing comparing
# 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 July 22, 2019 06:52
Bench pattern match with functions
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 July 3, 2019 02:16
Test node protobuf float
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);
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),
@tony612
tony612 / linux_config.sh
Last active May 6, 2019 06:39
Linux config
export PS1="\\u@\\h:\\w:\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\$ "
#set-option -g mouse on
setw -g mode-keys vi
bind-key -Tcopy-mode-vi 'v' send -X begin-selection
bind-key -Tcopy-mode-vi 'y' send -X copy-selection
# set -g terminal-overrides 'xterm*:smcup@:rmcup@'
## Debian apt
@tony612
tony612 / test_binary_parse.exs
Last active February 19, 2019 12:21
Elixir binary parsing test
defmodule Foo do
def foo1(bin, arg \\ 0) do
case do_foo1(bin) do
{:end, bin} = r ->
{r, arg}
{a, rest} ->
foo1(rest, arg + 1)
end
end
@tony612
tony612 / multi.go
Last active June 30, 2022 12:38
grpc one connection vs multiple connections (one: 13.8s, multi: 17s)
package main
import (
"log"
"os"
"sync"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
@tony612
tony612 / to_pb.rb
Created August 10, 2018 06:23
ruby hash to pb
require 'google/protobuf/timestamp_pb'
# Use official Message.new() instead
class Pb
class << self
def message_to_pb(message, msg_klass)
unless msg_klass.ancestors.include?(Google::Protobuf::MessageExts)
raise ArgumentError, "message class should be a pb message"
end
if msg_klass == Google::Protobuf::Timestamp && message.is_a?(::Time)
@tony612
tony612 / _service.md
Created July 2, 2018 13:27 — forked from naholyr/_service.md
Sample /etc/init.d script

Sample service script for debianoids

Look at LSB init scripts for more information.

Usage

Copy to /etc/init.d:

# replace "$YOUR_SERVICE_NAME" with your service's name (whenever it's not enough obvious)
@tony612
tony612 / subnet_calculator.rb
Last active June 26, 2018 11:00
subnet calculator
# https://github.com/ipaddress-gem/ipaddress
require 'ipaddress'
class Calculator
def self.run(top, parts: nil, prefixes: nil)
top = IPAddress(top)
subnets = by_parts(top, parts) if parts
subnets = by_prefixes(top, prefixes) if prefixes
subnets
end