Skip to content

Instantly share code, notes, and snippets.

@rkravchik
rkravchik / backoff.go
Created June 27, 2018 08:18
Backoff policy with no external fields for counter.
var backoffPolicy = []int64{0, 10, 30, 60, 300, 300, 900, 900, 900, 1800}
func BackoffTime(t int64, base int64) int64 {
r := t % 10
// use base var to avoid creating another variable
// round base to upper value multiple of ten seconds
// and add backoff time
base += 10 - base%10 + backoffPolicy[r] + r
if r != 9 {
base++
@rkravchik
rkravchik / timeoutconn.go
Created June 19, 2018 15:42
net.Connection with read and write timeouts.
// from https://stackoverflow.com/a/27785380
// Conn wraps a net.Conn, and sets a deadline for every read
// and write operation.
type Conn struct {
net.Conn
ReadTimeout time.Duration
WriteTimeout time.Duration
}
func (c *Conn) Read(b []byte) (int, error) {
@rkravchik
rkravchik / maxtime.go
Created May 11, 2018 11:36
Maximum time you can use.
// maxUnixTime is the maximum comparable time in go.
// See (spoiler: avoid to use math.MaxInt64):
// https://stackoverflow.com/questions/25065055/what-is-the-maximum-time-time-in-go
//
// Synonym to:
// time.Unix(1<<63-62135596801, 0).Unix()
// You can play with it here: https://play.golang.org/p/TaIAo84Wmq
const maxUnixTime = 1<<63 - 62135596801
var MaxTime = time.Unix(maxUnixTime, 0)
@rkravchik
rkravchik / go-cover.sh
Last active June 7, 2018 12:41
fast and conveniently covering go packages with tests
#!/bin/bash
go test -coverprofile cover.out $(go list)
go tool cover -html=cover.out -o cover.html
curl -T cover.html https://t.bk.ru
rm cover.out cover.html
@rkravchik
rkravchik / sync.Pool.txt
Created February 22, 2018 12:40
sync.Pool main destination
https://github.com/golang/go/issues/8232#issuecomment-66096118
Put another way, sync.Pool is for answering the question "how do I decide when to drop
this cached thing that otherwise would never die". The suggestion here is to use it to
answer the question "how can I make this thing fast". That's a mistake, even if the
implementation can satisfy both today. If it starts getting used for these two different
things, then we will not be able to change it, because optimizing for one case will hurt
the other.
Russ Cox
@rkravchik
rkravchik / scrolllock.ahk
Created January 30, 2018 17:33
Original scroll lock behaviour
;=== Original Scroll Lock behaviour ===
#If GetKeyState("Scrolllock","T")
Up::
Send {WheelUp 1}
Return
Down::
Send {WheelDown 1}
Return
@rkravchik
rkravchik / extractrpm.sh
Created September 27, 2017 10:59
extract rpm
rpm2cpio package.rpm | cpio -idvm
@rkravchik
rkravchik / photomaxsizeuri.vk
Created September 27, 2017 09:30
vkscript photo max size uri. <photoID> - photo ID in format "ownerid_id_accesskey".
var r = API.photos.getById({photos: <photoID>});
if (r[0] == null) {return {uri: null};}
var p = r[0];
if (p.photo_2560 != null){return {uri: p.photo_2560};}
if (p.photo_1280 != null){return {uri: p.photo_1280};}
if (p.photo_807 != null){return {uri: p.photo_807};}
if (p.photo_604 != null){return {uri: p.photo_604};}
if (p.photo_130 != null){return {uri: p.photo_130};}
if (p.photo_75 != null){return {uri: p.photo_75};}
@rkravchik
rkravchik / ncpipe.sh
Last active September 20, 2017 09:05
Files stream pipe between machines
# Recieving machine
nc -l <port> | tar -xvf -
# Sending machine
tar -cf - . | nc <host> <port>
@rkravchik
rkravchik / go.sh
Created August 11, 2017 13:25
Golang coding style checks
# Install tooling and set path:
go get github.com/golang/lint/golint
go get github.com/fzipp/gocyclo
go get github.com/kisielk/errcheck
export PATH=$PATH:$GOPATH/bin
# Fix errors and warnings:
go fmt ./... && gofmt -s -w . && go vet ./... && go get ./... && go test ./... && golint ./... && gocyclo -avg -over 15 . && errcheck ./...