mirror of
https://github.com/wshobson/agents.git
synced 2026-03-18 09:37:15 +00:00
style: format all files with prettier
This commit is contained in:
@@ -20,14 +20,14 @@ Production patterns for Go concurrency including goroutines, channels, synchroni
|
||||
|
||||
### 1. Go Concurrency Primitives
|
||||
|
||||
| Primitive | Purpose |
|
||||
|-----------|---------|
|
||||
| `goroutine` | Lightweight concurrent execution |
|
||||
| `channel` | Communication between goroutines |
|
||||
| `select` | Multiplex channel operations |
|
||||
| `sync.Mutex` | Mutual exclusion |
|
||||
| `sync.WaitGroup` | Wait for goroutines to complete |
|
||||
| `context.Context` | Cancellation and deadlines |
|
||||
| Primitive | Purpose |
|
||||
| ----------------- | -------------------------------- |
|
||||
| `goroutine` | Lightweight concurrent execution |
|
||||
| `channel` | Communication between goroutines |
|
||||
| `select` | Multiplex channel operations |
|
||||
| `sync.Mutex` | Mutual exclusion |
|
||||
| `sync.WaitGroup` | Wait for goroutines to complete |
|
||||
| `context.Context` | Cancellation and deadlines |
|
||||
|
||||
### 2. Go Concurrency Mantra
|
||||
|
||||
@@ -635,6 +635,7 @@ go run -race main.go
|
||||
## Best Practices
|
||||
|
||||
### Do's
|
||||
|
||||
- **Use context** - For cancellation and deadlines
|
||||
- **Close channels** - From sender side only
|
||||
- **Use errgroup** - For concurrent operations with errors
|
||||
@@ -642,6 +643,7 @@ go run -race main.go
|
||||
- **Prefer channels** - Over mutexes when possible
|
||||
|
||||
### Don'ts
|
||||
|
||||
- **Don't leak goroutines** - Always have exit path
|
||||
- **Don't close from receiver** - Causes panic
|
||||
- **Don't use shared memory** - Unless necessary
|
||||
|
||||
@@ -20,14 +20,14 @@ Cross-language patterns for memory-safe programming including RAII, ownership, s
|
||||
|
||||
### 1. Memory Bug Categories
|
||||
|
||||
| Bug Type | Description | Prevention |
|
||||
|----------|-------------|------------|
|
||||
| **Use-after-free** | Access freed memory | Ownership, RAII |
|
||||
| **Double-free** | Free same memory twice | Smart pointers |
|
||||
| **Memory leak** | Never free memory | RAII, GC |
|
||||
| **Buffer overflow** | Write past buffer end | Bounds checking |
|
||||
| **Dangling pointer** | Pointer to freed memory | Lifetime tracking |
|
||||
| **Data race** | Concurrent unsynchronized access | Ownership, Sync |
|
||||
| Bug Type | Description | Prevention |
|
||||
| -------------------- | -------------------------------- | ----------------- |
|
||||
| **Use-after-free** | Access freed memory | Ownership, RAII |
|
||||
| **Double-free** | Free same memory twice | Smart pointers |
|
||||
| **Memory leak** | Never free memory | RAII, GC |
|
||||
| **Buffer overflow** | Write past buffer end | Bounds checking |
|
||||
| **Dangling pointer** | Pointer to freed memory | Lifetime tracking |
|
||||
| **Data race** | Concurrent unsynchronized access | Ownership, Sync |
|
||||
|
||||
### 2. Safety Spectrum
|
||||
|
||||
@@ -568,6 +568,7 @@ fn rwlock_example() {
|
||||
## Best Practices
|
||||
|
||||
### Do's
|
||||
|
||||
- **Prefer RAII** - Tie resource lifetime to scope
|
||||
- **Use smart pointers** - Avoid raw pointers in C++
|
||||
- **Understand ownership** - Know who owns what
|
||||
@@ -575,6 +576,7 @@ fn rwlock_example() {
|
||||
- **Use tools** - AddressSanitizer, Valgrind, Miri
|
||||
|
||||
### Don'ts
|
||||
|
||||
- **Don't use raw pointers** - Unless interfacing with C
|
||||
- **Don't return local references** - Dangling pointer
|
||||
- **Don't ignore compiler warnings** - They catch bugs
|
||||
|
||||
@@ -28,13 +28,13 @@ Future (lazy) → poll() → Ready(value) | Pending
|
||||
|
||||
### 2. Key Abstractions
|
||||
|
||||
| Concept | Purpose |
|
||||
|---------|---------|
|
||||
| `Future` | Lazy computation that may complete later |
|
||||
| `async fn` | Function returning impl Future |
|
||||
| `await` | Suspend until future completes |
|
||||
| `Task` | Spawned future running concurrently |
|
||||
| `Runtime` | Executor that polls futures |
|
||||
| Concept | Purpose |
|
||||
| ---------- | ---------------------------------------- |
|
||||
| `Future` | Lazy computation that may complete later |
|
||||
| `async fn` | Function returning impl Future |
|
||||
| `await` | Suspend until future completes |
|
||||
| `Task` | Spawned future running concurrently |
|
||||
| `Runtime` | Executor that polls futures |
|
||||
|
||||
## Quick Start
|
||||
|
||||
@@ -497,6 +497,7 @@ tokio::spawn(async move {
|
||||
## Best Practices
|
||||
|
||||
### Do's
|
||||
|
||||
- **Use `tokio::select!`** - For racing futures
|
||||
- **Prefer channels** - Over shared state when possible
|
||||
- **Use `JoinSet`** - For managing multiple tasks
|
||||
@@ -504,6 +505,7 @@ tokio::spawn(async move {
|
||||
- **Handle cancellation** - Check `CancellationToken`
|
||||
|
||||
### Don'ts
|
||||
|
||||
- **Don't block** - Never use `std::thread::sleep` in async
|
||||
- **Don't hold locks across awaits** - Causes deadlocks
|
||||
- **Don't spawn unboundedly** - Use semaphores for limits
|
||||
|
||||
Reference in New Issue
Block a user