Skip to content

Instantly share code, notes, and snippets.

@temoto
temoto / limitmap.go
Created November 22, 2012 10:03
Map of semaphores to limit concurrency against some string keys in Go (golang)
// Package limitmap provides map of semaphores to limit concurrency against some string keys.
//
// Usage:
// limits := NewLimitMap()
// func process(url *url.URL, rch chan *http.Response) {
// // At most 2 concurrent requests to each host.
// limits.Acquire(url.Host, 2)
// defer limits.Release(url.Host)
// r, err := http.Get(url.String())
// rch <- r
@temoto
temoto / aes-cfb-example.go
Created February 27, 2013 22:37
Example of AES (Rijndael) CFB encryption in Go. IMHO, http://golang.org/pkg/crypto/cipher/ could benefit a lot from similar snippet.
package main
import (
"crypto/aes"
"crypto/cipher"
"fmt"
)
func EncryptAESCFB(dst, src, key, iv []byte) error {
aesBlockEncrypter, err := aes.NewCipher([]byte(key))
@temoto
temoto / Dockerfile
Last active June 20, 2023 08:34
File layout template for project with multiple docker containers in single repo
# I will eat you family and your dog if you use FROM ubuntu for container that runs a service in production
# Reading: http://phusion.github.io/baseimage-docker/
FROM phusion/baseimage:0.9.15
# inspired by https://github.com/progrium/buildstep
RUN mkdir /build
ADD ./files-build/ /build/
RUN chmod --recursive go-rwx /build
RUN LC_ALL=C DEBIAN_FRONTEND=noninteractive /bin/bash /build/prepare
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
@temoto
temoto / output.dir-lvm.fio.txt
Created August 14, 2022 00:07
Benchmark fio 4k random write+fsync load in LXD VM (qemu) with different storage backends.
dir-lvm: (groupid=0, jobs=1): err= 0: pid=776: Sat Aug 13 18:55:00 2022
read: IOPS=108, BW=436KiB/s (446kB/s)(25.5MiB/60001msec)
slat (usec): min=36, max=547, avg=80.78, stdev=18.61
clat (usec): min=66, max=1767, avg=548.89, stdev=58.97
lat (usec): min=361, max=1835, avg=630.83, stdev=59.84
clat percentiles (usec):
| 1.00th=[ 404], 5.00th=[ 441], 10.00th=[ 469], 20.00th=[ 506],
| 30.00th=[ 523], 40.00th=[ 553], 50.00th=[ 570], 60.00th=[ 578],
| 70.00th=[ 586], 80.00th=[ 594], 90.00th=[ 603], 95.00th=[ 611],
| 99.00th=[ 635], 99.50th=[ 668], 99.90th=[ 791], 99.95th=[ 930],
@temoto
temoto / install-shiv.sh
Created March 23, 2022 19:00
(python management) install shiv, poetry, pdm into /usr/local/bin ; no need to create virtualenvs for tools anymore
python3 -m venv /usr/local/shiv-venv && /usr/local/shiv-venv/bin/pip install shiv && /usr/local/shiv-venv/bin/shiv -c shiv -o /usr/local/bin/shiv shiv
/usr/local/bin/shiv -c poetry -o /usr/local/bin/poetry poetry
/usr/local/bin/shiv -c pdm -o /usr/local/bin/pdm pdm
@temoto
temoto / helpers_data.py
Last active March 22, 2022 05:19
Part of py-helpers. Gzip compression shortcuts. Encoding. Database helpers. Retry decorator.
def namedlist(typename, field_names):
"""Returns a new subclass of list with named fields.
>>> Point = namedlist('Point', ('x', 'y'))
>>> Point.__doc__ # docstring for the new class
'Point(x, y)'
>>> p = Point(11, y=22) # instantiate with positional args or keywords
>>> p[0] + p[1] # indexable like a plain list
33
>>> x, y = p # unpack like a regular list
@temoto
temoto / bench.py
Created September 28, 2021 01:44
Code benchmarking approach with correctness and usefulness ambitions
"""Describes "a better way" to benchmark code and present results.
Key points:
- repeat benchmark 5-7 times, take only best time, translate to single operation speed/duration
- calculate relative delta (new/old-1) of single op speed/duration
- translate delta crossing of boundary values to human friendly labels
Take it as is or add to your awesome benchmarking library.
"""
import timeit
@temoto
temoto / guide.md
Created August 2, 2019 14:39
Atom + Go with modules config for 2019-08

How to setup Atom for Go development with 1.11+ modules support

For projects outside GOPATH.

  • go get github.com/saibing/bingo why: ckaznocha/ide-go#42 Run which bingo and have full path in clipboard.
  • (Atom) install package go-plus for format/build/test-on-save It's great, but go-plus autocomplete doesn't work with modules. Settings, packages, go-plus, Autocomplete, set Scope blacklist *
  • (Atom) install package ide-go
@temoto
temoto / pre-commit
Created August 8, 2013 09:39
my git pre-commit hook: standard checks + autopep8 (Python)
#!/bin/bash
set -e
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
@temoto
temoto / bitsquat.py
Created November 20, 2012 18:03
DNS domain bitsquat checker
#!/usr/bin/env python
from __future__ import unicode_literals
# Original idea: http://dinaburg.org/bitsquatting.html
import gevent.monkey
gevent.monkey.patch_all()
import dns.name, dns.resolver
import gevent, gevent.pool
import sys