<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Pool on Chen Shungen</title><link>https://chenshungen.cn/tags/pool/</link><description>Recent content in Pool on Chen Shungen</description><generator>Hugo</generator><language>zh-cn</language><lastBuildDate>Mon, 20 Apr 2026 12:30:00 +0800</lastBuildDate><atom:link href="https://chenshungen.cn/tags/pool/index.xml" rel="self" type="application/rss+xml"/><item><title>Go 并发原语 - Pool</title><link>https://chenshungen.cn/blog/golang-concurrency/golang-pool/</link><pubDate>Mon, 20 Apr 2026 12:30:00 +0800</pubDate><guid>https://chenshungen.cn/blog/golang-concurrency/golang-pool/</guid><description>&lt;p>Go 是自动垃圾回收的语言，创建对象没有回收的心理负担。但如果你要开发高性能应用，就必须关注 GC 的影响——大量创建堆上的对象，会增加 GC 标记的时间和 STW（stop-the-world）的开销。&lt;strong>对象池&lt;/strong> 是一种经典的优化手段：把不用的对象回收起来复用，减少堆分配和 GC 压力。Go 标准库提供了 &lt;code>sync.Pool&lt;/code> 来实现这个目的。&lt;/p>
&lt;p>这篇文章我们先讲 sync.Pool 的用法和原理，再扩展到连接池和 Worker Pool。&lt;/p>
&lt;h2 id="一syncpool-基本用法">一、sync.Pool 基本用法&lt;/h2>
&lt;p>sync.Pool 的 API 非常简洁：&lt;/p>
&lt;div class="highlight-wrapper">
 &lt;button class="copy-code-btn" type="button" aria-label="Copy code to clipboard">
 &lt;svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
 &lt;rect x="9" y="9" width="13" height="13" rx="2" ry="2">&lt;/rect>
 &lt;path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1">&lt;/path>
 &lt;/svg>
 &lt;span class="copy-text">Copy&lt;/span>
 &lt;/button>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="cl">┌─────────────────────────────────────────────────────────────┐
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">│ sync.Pool │
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">├─────────────────────────────────────────────────────────────┤
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">│ New func() any 创建新对象的工厂函数（Pool 为空时调用）│
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">│ Get() any 从池中取出一个对象 │
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">│ Put(x any) 把对象放回池中 │
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">└─────────────────────────────────────────────────────────────┘&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/div>
&lt;p>一个典型的用法——复用 &lt;code>bytes.Buffer&lt;/code>：&lt;/p></description></item></channel></rss>