Async
33 articles
Common Context Mistakes and Anti-Patterns in Go
Avoid nil contexts, cancelled parents, and missing timeouts to prevent hangs and resource leaks in Go.
Complete Guide to the context Package in Go
The context package manages deadlines, cancellation signals, and request-scoped values across goroutines and API boundaries in Go.
Context Best Practices in Go
Use the GODEBUG environment variable or //go:debug directives to override default Go runtime behaviors for compatibility or testing.
Fix: "context canceled" in Go
Fix 'context canceled' in Go by ensuring parent contexts aren't canceled prematurely and handling the error gracefully.
Fix: "context deadline exceeded" in Go
Fix context deadline exceeded by increasing the timeout duration or optimizing the slow operation to complete faster.
How to Build a Producer-Consumer System in Go
Build a Go producer-consumer system using buffered channels to safely pass data between goroutines.
How to Check If a Context Is Done or Cancelled
Check if a Go context is done or cancelled by selecting on its Done channel or calling Err().
How to Create Timers and Tickers in Go
Create one-off delays with time.NewTimer and repeating intervals with time.NewTicker in Go.
How to Implement a Background Job Worker in Go
Implement a Go background job worker by spawning goroutines that consume tasks from a channel and send results back to the main program.
How to Implement a Job Queue in Go
Implement a Go job queue using a buffered channel for tasks and goroutines for concurrent processing.
How to Implement an Actor Model in Go
Implement the Actor Model in Go by creating a struct with a channel for message passing and a goroutine that processes messages in a loop.
How to Implement Delayed and Retry Logic for Jobs in Go
Implement job retries in Go using a manual loop with exponential backoff and time.Sleep since no standard library exists for this.
How to Implement Graceful Shutdown with Context in Go
Implement graceful shutdown in Go by using context cancellation triggered by OS signals like SIGINT and SIGTERM.
How to Implement Idempotent Job Processing in Go
Prevent duplicate job execution in Go by checking a persistent store for the job ID before running the task logic.
How to Monitor Background Job Health in Go
Monitor background job health in Go by exposing a metrics endpoint that reports job status, error counts, and last execution time via `runtime/metrics` or custom counters. Use a goroutine to track job state and expose it through an HTTP handler for external monitoring tools.
How to Pass Context Through Your Application
Pass context by creating it at the entry point and threading it as the first argument to every function in your call chain.
How to Propagate Cancellation with Context in Go
Use context.Context with cancel functions to signal goroutines to stop, checking ctx.Done() or ctx.Err() to exit tasks immediately.
How to Run Scheduled Tasks (Cron Jobs) in Go
Run scheduled tasks in Go by installing the robfig/cron library and adding a job with a cron expression string.
How to Set a Timeout with time.After and context
Use context.WithTimeout and time.After together to enforce a deadline on Go operations.
How to Use Asynq for Distributed Task Queues in Go
Asynq is a robust Go library for building distributed task queues that relies on Redis as its backend to handle job scheduling, retries, and concurrency.
How to Use context.AfterFunc (Go 1.21+)
Schedule a function to run after a duration or context cancellation using context.AfterFunc in Go 1.21+.
How to Use Context with Database Queries in Go
Use context.WithTimeout and QueryRowContext to prevent database queries from hanging indefinitely.
How to Use Context with Goroutines
Pass a context.Context to goroutines and check ctx.Done() to safely stop them on cancellation or timeout.
How to Use Context with HTTP Requests in Go
Pass a context created with WithTimeout or WithCancel to NewRequestWithContext to control HTTP request deadlines.
How to Use context.WithoutCancel (Go 1.21+)
context.WithoutCancel creates a derived context that ignores parent cancellation while preserving values and deadlines.
How to Use Machinery for Distributed Task Processing in Go
Machinery is a robust asynchronous task queue and job scheduler for Go that allows you to distribute work across multiple workers using a message broker like Redis or RabbitMQ.
How to Use Reactive Streams Patterns in Go
Go implements reactive streams patterns using channels and goroutines to manage asynchronous data flow and backpressure.
How to Use River for Background Jobs with PostgreSQL in Go
River is a robust job queue library for Go that uses PostgreSQL as its storage backend, allowing you to define jobs as Go structs and enqueue them with automatic retry logic, scheduling, and error handling.
How to Use robfig/cron for Scheduling in Go
Use `robfig/cron` by creating a cron instance, adding jobs with standard cron expressions or custom parsers, and then running the scheduler in a separate goroutine.
How to Use time.After and time.Tick in Go
Use time.After for single delays and time.NewTicker for repeated intervals to manage timing in Go programs effectively.
How to Use time.Sleep in Go
Use time.Sleep to pause the current goroutine for a specific duration without blocking other operations.
What Is the Difference Between context.WithTimeout and context.WithDeadline
Use WithTimeout for a duration and WithDeadline for a specific time to cancel Go contexts.
Why Context Should Be the First Parameter in Go Functions
Context must be the first parameter in Go functions to prevent accidental omission and ensure consistent cancellation and timeout propagation.