Back to Groups
Development

Go: backend workers and HTTP services

Go 1.20+ worker pools, HTTP concurrency limits, shutdown coordination and bounded request data using the standard library. Merge imports into your package. Review names and integration points before use.

workflowsbackendgogo-developercode
MC
mcarter
0likes12snippets

Try it here

Loading snippet…

Your typing stays in this tab. Nothing is saved or sent.
Snippets (12)
;go-json
Decode one bounded JSON value into a struct
import ( "bytes" "encoding/json" "errors" "io" "math" ) func DecodeOneJSON(r io
;go-read-cap
Read a stream with an explicit byte cap
import ( "errors" "io" "math" ) func ReadBounded(r io.Reader, limit int64) ([]byte, err
;go-httpget
GET with context, status validation and a bounded body
import ( "context" "errors" "io" "math" "net/http" "strconv" ) func GetBoun
;go-csv
Write CSV and check buffered flush errors
import ( "encoding/csv" "io" ) func WriteCSV(destination io.Writer, rows [][]string) error
;go-intsetting
Parse an integer setting within an allowed range
import ( "errors" "strconv" "strings" ) func ParseBoundedInt(raw string, minimum, maxim
;go-delay
Wait without ignoring context cancellation
import ( "context" "errors" "time" ) func WaitOrCancel(ctx context.Context, delay time.
;go-batches
Split work into copied batches
import "errors" func Batches[T any](values []T, size int) ([][]T, error) { if size <= 0 { retur
;go-workers
Process jobs with bounded workers and first-error cancellation
import ( "context" "errors" "sync" ) func RunWorkers[T any](ctx context.Context, count
;go-unique
Deduplicate a slice without reordering it
func UniqueInOrder[T comparable](values []T) []T { seen := make(map[T]struct{}, len(values))
;go-httpcapacity
Reject excess concurrent HTTP work without growing a queue
import ( "errors" "net/http" ) func WithConcurrencyLimit(next http.Handler, limit int) (htt
;go-shutdown
Drain an HTTP server using a fresh shutdown deadline
import ( "context" "errors" "time" ) type HTTPShutdowner interface { Shutdown(conte
;go-periodic
Run a periodic maintenance task without overlapping executions
import ( "context" "errors" "time" ) func RunPeriodic(ctx context.Context, interval tim