All skills
cxuu avatar

/go-concurrency

@91f0c2e
by Charles Xucxuu/golang-skills165 stars
19

Use when writing concurrent Go code — goroutines, channels, mutexes, or thread-safety guarantees. Also use when parallelizing work, fixing data races, or protecting shared state, even if the user doesn't explicitly mention concurrency primitives. Does not cover context.Context patterns (see go-context).

Use this Skill: https://skilld.dev/gh/cxuu/golang-skills/go-concurrency

This session only. Nothing lands on disk.

referencesBUFFER-POOLING.md

≈582 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Buffer Pooling with Channels

Use a buffered channel as a free list to reuse allocated buffers, avoiding repeated allocations. This "leaky buffer" pattern uses select with default for non-blocking operations.

Source: Effective Go

var freeList = make(chan *Buffer, 100) // Buffered channel as free list

// Client: Get buffer from free list or allocate new one
func getBuffer() *Buffer {
    select {
    case b := <-freeList:
        return b // Reuse existing buffer
    default:
        return new(Buffer) // Free list empty; allocate new buffer
    }
}

// Server: Return buffer to free list if room, otherwise drop it
func putBuffer(b *Buffer) {
    b.Reset() // Prepare for reuse
    select {
    case freeList <- b:
        // Buffer returned to free list
    default:
        // Free list full; drop buffer (GC will reclaim)
    }
}

How It Works

  1. Non-blocking receive: Client tries to grab a buffer from freeList. If empty, default runs and allocates a new buffer.
  2. Non-blocking send: Server tries to return the buffer. If freeList is full, default runs and the buffer is dropped for garbage collection.
  3. Bounded memory: The channel capacity (100) limits pooled buffers, preventing unbounded growth.

This pattern is useful when allocation is expensive and buffer reuse is beneficial, but you don't want blocking behavior when the pool is empty or full.

When to Use

  • High-frequency allocations of similar-sized objects
  • Performance-critical code paths where allocation overhead matters
  • Scenarios where you want bounded memory usage

Production Alternative

For production code, consider sync.Pool which provides similar functionality with better integration into the garbage collector:

var bufferPool = sync.Pool{
    New: func() any {
        return new(Buffer)
    },
}

func getBuffer() *Buffer {
    return bufferPool.Get().(*Buffer)
}

func putBuffer(b *Buffer) {
    b.Reset()
    bufferPool.Put(b)
}

sync.Pool advantages:

  • Automatic cleanup during garbage collection
  • No need to manage pool size
  • Thread-safe by design
  • Better performance under high concurrency

The channel-based approach is still valuable for understanding Go's concurrency primitives and for cases where you need more control over pool behavior.

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    This skill provides comprehensive guidelines and best practices for writing concurrent Go code, focusing on goroutine lifetimes, channel patterns, and synchronization primitives. It includes references to trusted community resources and tools. No security issues were detected.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    2 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 91f0c2e. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 3 months ago

README badge

README badge for cxuu/golang-skills/go-concurrency