Leah Thompson

78 code snips · on codesnips 6 months

Go backend engineer sharing production-grade patterns for APIs, concurrency, and data systems. I focus on reliability: timeouts, idempotency, observability, and clean service...

go
package middleware

import (
  "log"
  "net/http"
  "runtime/debug"

Panic recovery middleware that fails closed and logs context

go http middleware
by Leah Thompson 1 tab
go
package dbutil

import (
  "context"

  "github.com/jackc/pgconn"

Retry Postgres serialization failures with bounded attempts

go postgres transactions
by Leah Thompson 1 tab
go
package cleanup

import "errors"

func CloseAll(closers ...func() error) error {
  var errs []error

errors.Join for cleanup (surface multiple close failures)

go errors reliability
by Leah Thompson 1 tab
go
package middleware

import "net/http"

type StatusRecorder struct {
  http.ResponseWriter

Capture status code and bytes written via ResponseWriter wrapper

go http observability
by Leah Thompson 1 tab
go
package api

import (
  "encoding/json"
  "net/http"
)

WriteJSON helper with consistent headers and status

go http json
by Leah Thompson 1 tab
go
package api

import (
  "encoding/json"
  "errors"
  "io"

Strict JSON decode helper (size limit + unknown fields)

go http json
by Leah Thompson 1 tab
go
package middleware

import (
  "net/http"
  "sync/atomic"
)

Track in-flight requests with atomic counters

go observability concurrency
by Leah Thompson 1 tab
go
package api

import (
  "io"
  "net/http"
)

Content sniffing for uploads (don't trust the header)

go http uploads
by Leah Thompson 1 tab
go
package consumers

import (
  "context"

  "github.com/jackc/pgx/v5/pgxpool"

Idempotent event consumer with processed-events table

go postgres messaging
by Leah Thompson 1 tab
go
package observability

import (
  "expvar"
  "net/http"
)

expvar counters for quick-and-dirty production introspection

go observability metrics
by Leah Thompson 1 tab
go
package main

import (
  "os"
  "runtime/debug"
  "strconv"

Set a soft memory limit with debug.SetMemoryLimit

go performance memory
by Leah Thompson 1 tab
go
package pools

import (
  "bytes"
  "sync"
)

sync.Pool for bytes.Buffer to reduce allocations in hot paths

go performance memory
by Leah Thompson 1 tab