Skip to content

Instantly share code, notes, and snippets.

View stokito's full-sized avatar
Self-hosting become easier

Sergey Ponomarev stokito

Self-hosting become easier
View GitHub Profile
@stokito
stokito / curl_request_time.md
Created September 15, 2021 14:19
Measure request time with curl
time curl -X POST --location "http://127.0.0.1:8080/url" \
  -H "Content-Type: application/json" \
  -d "{\"id\":\"1234567893\"}" \
  -s -o /dev/null -w "%{time_starttransfer}\n"

Output will be like:

127618
@stokito
stokito / addr_is_ip.go
Created October 16, 2021 07:15
Check that string is an IP address
// IsIp checks that the given string is IPv4 or IPv6 address.
func IsIp(addr string) bool {
// IPv6
if addr[0] == '[' {
return true
}
// domain can have digits, but it can't have ONLY digits
// So if all symbols are digits then this is an IPv4 address. We can check only first octet before dot .
@stokito
stokito / json_encode_text.go
Created November 17, 2021 14:48
Fast json text value encode: replace
// EncodeToJsonAsciiText Fast json text value encode: escape " with \" and \ with \\
// Any non printable or Unicode chars will be removed.
// Please note that value is not enclosed into double quotes.
func EncodeToJsonAsciiText(plain string) string {
plainLen := len(plain)
newSize := plainLen
for i := 0; i < plainLen; i++ {
c := plain[i]
if c < 32 || c > 127 { // skip non printable and unicode
newSize--
@stokito
stokito / grpc_error_handling.go
Created November 29, 2021 21:29
GRPC catch error
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"reflect"
"strconv"
)
// GrpcError The interface the same as internal type Error from grpc: internal/status/status.go
@stokito
stokito / ipv4_parse.go
Created December 1, 2021 18:48
Golang parse IPv4 to bytes array without unnecessary allocation
package main
// parseIPv4 fast path clone of net.ParseIP that don't wrap IPv4 into IPv6 array
func parseIPv4(s string) []byte {
var p [4]byte
for i := 0; i < 4; i++ {
octet, pos := dtoi(s)
p[i] = octet
if i < 3 {
s = s[pos+1:]
@stokito
stokito / hostmap.go
Last active December 20, 2021 11:37
GoLang bytes slice as a key map. Just convert the slice to string and Go will optimize it https://stackoverflow.com/questions/20297503/slice-as-a-key-in-map https://github.com/golang/go/wiki/CompilerOptimizations#string-and-byte
package bytesmap
var m = make(map[string]*string)
// this works faster
func GetOrDefaultInline(host []byte) *string {
hc, found := m[string(host)]
if !found {
m[string(host)] = nil
}
package main
// UInt16Slice is a sorted slice
type UInt16Slice []uint16
// BinSearch binary search over the sorted sclice
func (a UInt16Slice) BinSearch(key uint16) int {
low := 0
high := len(a) - 1
for low <= high {
@stokito
stokito / README.md
Last active January 9, 2022 14:02
OpenWrt specific shell (ash) script to decode JWT token and verify it's signature. Based on https://gist.github.com/stokito/f2d7ea0b300f14638a9063559384ec89

You'll need to instll OpenSSL and it will use about 1.3Mb. So check that it's enought of available space:

df -h | grep /overlay

Then install it:

opkg install openssl-util

It also will install libopenssl1.1 and libopenssl-conf.