Skip to content

Instantly share code, notes, and snippets.

Avatar

Kailash Nadh knadh

View GitHub Profile
View parse-database-url-dsn-bash.sh
#!/usr/bin/env bash
DATABASE_URL="postgres://MyPostgresUser:MyPostgresPassword@192.168.0.1:5432/MyPostgresDB"
# `cut` is used to cut out the separators (:, @, /) that come matched with the groups.
DATABASE_USER=$(echo $DATABASE_URL | grep -oP "postgres://\K(.+?):" | cut -d: -f1)
DATABASE_PASSWORD=$(echo $DATABASE_URL | grep -oP "postgres://.*:\K(.+?)@" | cut -d@ -f1)
DATABASE_HOST=$(echo $DATABASE_URL | grep -oP "postgres://.*@\K(.+?):" | cut -d: -f1)
DATABASE_PORT=$(echo $DATABASE_URL | grep -oP "postgres://.*@.*:\K(\d+)/" | cut -d/ -f1)
@knadh
knadh / flatten_test.go
Last active Jun 9, 2021
Benchmark of flattening nested maps ({ "parent": { "child": 123 }}` -> `{ "parent.child": 123 }): Recursion vs. iteration
View flatten_test.go
package main
import (
"encoding/json"
"strings"
"testing"
)
var nested map[string]interface{}
@knadh
knadh / go-redis-struct-scan.go
Last active Jan 24, 2021
A Redis reply to struct scanner for go-redis/redis
View go-redis-struct-scan.go
package cacheman
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
)
@knadh
knadh / listmonk-upgrade-v0.7.0-alpha.sql
Created Jul 26, 2020
Schema changes to upgrade to listmonk v0.7.0-alpha
View listmonk-upgrade-v0.7.0-alpha.sql
ALTER TABLE media DROP COLUMN width, DROP COLUMN height;
ALTER TABLE media ADD COLUMN provider TEXT NOT NULL;
DROP TABLE IF EXISTS settings CASCADE;
CREATE TABLE settings (
key TEXT NOT NULL UNIQUE,
value JSONB NOT NULL DEFAULT '{}',
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
DROP INDEX IF EXISTS idx_settings_key; CREATE INDEX idx_settings_key ON settings(key);
INSERT INTO settings (key, value) VALUES
@knadh
knadh / go-graceful-reload.go
Last active Jul 14, 2020
Example of gracefully shutting down and re-loading / restarting a Go app on SIGHUP retaining the same PID
View go-graceful-reload.go
// program
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
View kaf-kafka-connect-error.md

Unable to get cluster admin: kafka: controller is not available

If you get this error and don't want to waste hours trying to debug why kaf is unable to communicate with a Kafka cluster, make sure you have an entry in the hosts file for the system's hostname that resolves to self (127.0.0.1), if your cluster is running locally.

@knadh
knadh / nats-cluster-multiple-publisher-failover.md
Created Mar 21, 2019
Running multiple active publishers on a NATS cluster for failover avoding message duplication
View nats-cluster-multiple-publisher-failover.md

Running multiple active publishers on a NATS cluster for failover while avoding message duplication

NATS is an excellent, clustered, full-mesh PubSub messaging system, highly performant and a cakewalk to setup. Full mesh means every node (servers and clients) knows about every other node, which is great, but makes it tricky to have multiple publishers on hot standby, for high availability of publishers (not the NATS network), while avoiding duplicate pubs.

Here --no-advertise comes in handy if we're willing to sacrifice the automatic meshing and discovery mechanism. This may be acceptable in setups where only a fixed set of NATS servers run in a cluster and whose addresses (either IPs or hostnames) are known.

--no-advertise

The gnatsd --no-advertise flag makes a NATS server not advertise itself automatically to the mesh. For other nodes to discover --no-advertise nodes, the --routes have to be explicitly specified. If there are N servers, there should be N routes.

@knadh
knadh / redis-protocol.go
Created Feb 16, 2019
Go function for generation Redis raw protocol for importing with redis-cli --pipe
View redis-protocol.go
package main
import (
"fmt"
"bytes"
"strconv"
)
// toRedisProto converts a Redis command, represented by a slice of
// bytes, where each element of the slice represents a chunk of the
@knadh
knadh / voltdb-direct-download.md
Created Aug 28, 2018
VoltDB Community Edition direct download
View voltdb-direct-download.md
@knadh
knadh / netstat-conn-group.sh
Created Sep 10, 2017
Group and list all TCP connections (local address) by port and number of connections
View netstat-conn-group.sh
netstat -ntu | awk ' $4 ~ /^[0-9]/ {print $4}' | sort | uniq -c | sort -n
# Rehashed from http://www.commandlinefu.com/commands/view/8767/netstat-with-group-by-ip-adress