go
12 lines · 1 tab
Leah Thompson
Jan 2026
1 tab
package api
import "net/http"
func ETag(w http.ResponseWriter, r *http.Request, etag string) bool {
w.Header().Set("ETag", etag)
if inm := r.Header.Get("If-None-Match"); inm != "" && inm == etag {
w.WriteHeader(http.StatusNotModified)
return true
}
return false
}
1 file · go
Explain with highlit
ETags are a low-effort way to cut bandwidth and CPU when clients poll for resources that rarely change. The server computes an ETag for the representation (often a version, content hash, or updated_at value) and compares it to If-None-Match. If they match, we return 304 Not Modified with no body. The detail that matters is consistency: your ETag must change whenever the response body would change, or caches lie. I like using stable version strings (like a row version or sha256) rather than timestamps that can drift. I also set Cache-Control to a short max-age for CDN friendliness. In practice this is a huge win for endpoints like profile pages, settings payloads, and feature-flag documents.
Related snips
go
package api
import (
"net/http"
"runtime/debug"
)
Expose build metadata for debugging deploys
go
observability
build
by Leah Thompson
1 tab
typescript
export interface RetryOptions {
retries: number;
baseMs: number;
maxMs: number;
signal?: AbortSignal;
onRetry?: (attempt: number, delay: number, err: unknown) => void;
Exponential backoff with jitter for retries
typescript
reliability
retry
by codesnips
2 tabs
ruby
module Api
module V1
class UsersController < BaseController
def show
user = User.includes(:profile).find(params[:id])
ETags for conditional requests and caching
rails
caching
http-caching
by Alex Kumar
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
ruby
require "csv"
class PeopleCsvStream
include Enumerable
HEADERS = %w[id full_name email signed_up_at plan].freeze
Resilient CSV Export as a Streamed Response
rails
performance
streaming
by codesnips
3 tabs
ruby
Rails.application.configure do
config.after_initialize do
Bullet.enable = true
Bullet.alert = false
Bullet.bullet_logger = true
Bullet.console = true
N+1 query detection with Bullet gem
rails
performance
activerecord
by Alex Kumar
2 tabs
Share this code
Here's the card — post it anywhere.