Skip to content

Instantly share code, notes, and snippets.

@AndreKR
AndreKR / elasticsearch_notes.md
Last active December 11, 2015 00:36
Elasticsearch notes

Elasticsearch notes

Things named "explain"

Three things can be explained in elasticsearch:

Explain for each hit how its score was computed (Docs: Explain)
GET //_search?explain
@AndreKR
AndreKR / wireshark_remote.bat
Created December 13, 2015 23:49
Wireshark remote
@echo off
set PLINK_PATH=C:\Program Files (x86)\PuTTY\plink.exe
set WIRESHARK_PATH=C:\Program Files\Wireshark\Wireshark.exe
if "%1" == "" goto :usage
"%PLINK_PATH%" -ssh %1 "tcpdump -ni %2 -s 0 -w - not port 22" | "%WIRESHARK_PATH%" -k -i -
goto :end
DROP TABLE IF EXISTS public.with_enum;
DROP TABLE IF EXISTS public.with_text;
DROP TYPE IF EXISTS my_enum;
CREATE TYPE public.my_enum AS ENUM ('axyzxyz', 'bxyzxyz', 'cxyzxyz', 'dxyzxyz');
CREATE TABLE public.with_enum (
id serial NOT NULL PRIMARY KEY,
blah integer,
thecolumn my_enum
@AndreKR
AndreKR / detect_http_abort.go
Last active March 15, 2017 05:33
Detecting client closed connection
func handleRequest(w http.ResponseWriter, r *http.Request) {
cn := w.(http.CloseNotifier)
cc := cn.CloseNotify()
go func() {
<-cc
log.Println("Closed!")
}()
@AndreKR
AndreKR / result_and_error_channel.go
Last active March 19, 2017 18:44
Returning result and error through a channel
// (I prefer "Two channels wrapped" and "Anonymous struct" over the other two)
// Two channels
resultCh := make(chan Result)
errorCh := make(chan Result)
go func(resultCh chan Result, errorCh chan errpr) {
errorCh <- errors.New("Does not compute")}
resultCh <- result
}(resultCh, errorCh)
package main
// Trigger ensures a worker runs once after it has been triggered, no matter how many trigger events happened during
// its last run.
//
// Usage:
// trigger := NewTrigger()
// go func() {
// for _ = range trigger {
// do_work()
@AndreKR
AndreKR / http_context.go
Created June 11, 2017 15:25
Go http + context
// While the Go documentation promotes the context package for keeping track of request context and cancelation, to
// actually use it for canceling work in http requests some plumbing is necessary:
// getCancelableContextFromResponseWriter gets a context.Context that is canceled when the client (browser) closes the connection
func getCancelableContextFromResponseWriter(w http.ResponseWriter) context.Context {
// Create a context that can be handed down to the workers to relay cancellation
ctx, cancelContext := context.WithCancel(context.Background())
// Get a channel that receives a value when the request is canceled
var exeDir = getExeDir()
func getExeDir() string {
if os.Getenv("EXEDIR") != "" {
return os.Getenv("EXEDIR")
} else {
return filepath.Dir(os.Args[0])
}
}
@AndreKR
AndreKR / latest_gitlab_artifacts.txt
Last active September 13, 2017 19:06
Download the latest artifacts from GitLab
# ZIP:
wget --header="PRIVATE-TOKEN: $GITLAB_TOKEN" "https://<gitlab>/<group>/<project>/builds/artifacts/master/download?job=<jobname>"
or
wget --header="PRIVATE-TOKEN: $GITLAB_TOKEN" "https://<gitlab>/<group>/<project>/-/jobs/artifacts/master/download?job=<jobname>"
# Single file:
wget -O /path/to/file --header="PRIVATE-TOKEN: $GITLAB_TOKEN" "https://<gitlab>/<group>/<project>/builds/artifacts/master/raw/<filename>?job=<jobname>"
@AndreKR
AndreKR / draw_text.go
Created March 14, 2017 09:03
Three ways of drawing text in go
// Initialize an empty image
dest := image.NewRGBA(image.Rect(0, 0, 800, 300))
// Draw a white background
draw.Draw(dest, dest.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
// Read the font
fontBytes, err := ioutil.ReadFile(exeDir + "/DejaVuSans.ttf")
if err != nil {