Skip to content

Instantly share code, notes, and snippets.

@omerkaya1
omerkaya1 / tls_certs.go
Created April 19, 2024 08:43
Create TLS certs (mostly for unit tests)
package some_test
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
@omerkaya1
omerkaya1 / pgx.go
Last active March 27, 2024 09:30
Golang PostgreSQL connection pool example Go ^1.18
package db
import (
"context"
"database/sql"
"net"
"net/url"
"strconv"
"time"
@omerkaya1
omerkaya1 / collector.go
Last active November 21, 2023 13:25
Generic sync.Cond-based data collector (Go ^1.20)
package collector
import (
"sync"
)
// Collector manages feeding workers with Job objects and Result data collection,
// synchronizing both parts through an internal counter.
// Once created, it can and should be re-used.
type Collector[Result, Job any] struct {
@omerkaya1
omerkaya1 / metrics.go
Created September 22, 2023 21:41
pgx conn pool metrics
package db
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
const (
clientNamespace = "db_client"
querySubsystem = "query"
@omerkaya1
omerkaya1 / pool.go
Created September 22, 2023 15:53
A sized pool of slices of generic type objects
// SizedPool is a channel-based pool of slices objects.
// It retains an arbitrary number of slices of objects and ensures their initial capacity.
type SizedPool[T any] struct {
pool chan []T
cap int
}
func NewSizedPool[T any](size int, cap int) *SizedPool[T] {
return &SizedPool[T]{
pool: make(chan []T, size),
@omerkaya1
omerkaya1 / delete_with_cte.sql
Created April 6, 2023 07:20
Delete records from multiple tables in one query with CTE
WITH
first AS (
DELETE FROM table1 WHERE condition1
RETURNING *
),
second AS (
DELETE FROM table2 WHERE condition2
RETURNING *
),
-- add more analogous delete statements
WITH ranked_records AS (
SELECT row_number() OVER (PARTITION BY user_id, user_id ORDER BY created_at DESC) AS rn,
id, some_other_column, created_at
FROM user_status WHERE user_id IN (?)
)
SELECT
id, some_other_column, created_at
FROM ranked_records
WHERE rn=1
ORDER BY user_id;
WITH get AS (
SELECT id FROM table_name WHERE name=$1
), new AS (
INSERT INTO table_name (name) VALUES ($1) ON CONFLICT (name) DO NOTHING RETURNING id
)
SELECT id FROM get
UNION ALL
SELECT id FROM new;
@omerkaya1
omerkaya1 / artillery.yml
Created October 29, 2022 10:48
Artillery config file (load testing WebSocket)
config:
environments:
local:
target: "http://localhost:8000"
phases:
- duration: 10
arrivalRate: 1
arrivalCount: 200
maxVusers: 50
name: "Warm up"
@omerkaya1
omerkaya1 / pprof.sh
Created October 29, 2022 10:45
Simple script to collect Go app's profile
#!/bin/bash
BRANCH=$(git branch --show-current)
COMMIT_HASH=$(git rev-parse --short HEAD)
for i in {1..5} ; do
printf "Phase %s\n" "$i"
if ! curl -s "localhost:8000/debug/pprof/heap" > mem-"$BRANCH"-"$COMMIT_HASH"-"$(date "+%Y-%m-%d-%H-%M-%S")".pprof
then
echo "failed to get mem profile"