go
35 lines · 1 tab
Leah Thompson
Jan 2026
1 tab
package deps
import (
"context"
"net/http"
"time"
"github.com/sony/gobreaker"
)
type Client struct {
http *http.Client
cb *gobreaker.CircuitBreaker
}
func NewClient(httpClient *http.Client) *Client {
st := gobreaker.Settings{
Name: "billing-api",
MaxRequests: 5,
Interval: 30 * time.Second,
Timeout: 10 * time.Second,
ReadyToTrip: func(c gobreaker.Counts) bool { return c.ConsecutiveFailures >= 5 },
}
return &Client{http: httpClient, cb: gobreaker.NewCircuitBreaker(st)}
}
func (c *Client) Do(ctx context.Context, req *http.Request) (*http.Response, error) {
v, err := c.cb.Execute(func() (any, error) {
return c.http.Do(req.WithContext(ctx))
})
if err != nil {
return nil, err
}
return v.(*http.Response), nil
}
1 file · go
Explain with highlit
Retries alone can make an outage worse: if a dependency is hard failing, retries just add load. A circuit breaker adds a simple state machine: closed (normal), open (fail fast), and half-open (probe). I like gobreaker because it’s small and predictable. The important detail is what counts as a failure: timeouts and 5xx responses should trip the breaker; validation errors should not. I also emit breaker state changes as logs/metrics so you can see when the system is protecting itself. The result is better tail latency and less cascading failure, especially for high QPS services that call a single upstream. Pair it with request-level context.WithTimeout and you get a clean “fail fast” behavior.
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
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 files
import (
"context"
"time"
Presigned S3 upload URLs (AWS SDK v2)
go
aws
s3
by Leah Thompson
1 tab
go
package deps
import (
"net"
"net/http"
"time"
HTTP client tuned for production: timeouts, transport, and connection reuse
go
http
client
by Leah Thompson
1 tab
go
package api
import (
"io"
"net/http"
"os"
Safe multipart uploads using temp files (bounded memory)
go
http
uploads
by Leah Thompson
1 tab
Share this code
Here's the card — post it anywhere.