{"id":469,"date":"2026-06-01T09:00:00","date_gmt":"2026-06-01T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/"},"modified":"2026-06-01T09:00:00","modified_gmt":"2026-06-01T03:30:00","slug":"go-circuit-breaker-pattern","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/","title":{"rendered":"Go Circuit Breaker Pattern Explained with Code"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A Go circuit breaker stops your program from calling a service that is already failing. In this post, you will learn the three states of a breaker and why it protects both sides. Then we will build one from scratch with a mutex and a replaceable clock.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This lesson builds on <a href=\"https:\/\/smitabharat.com\/blog\/go-retry-backoff-jitter\/\">Go retry with backoff and jitter<\/a>. We also use the <a href=\"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/\">Go sync package<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-error-handling-guide\/\">Go error handling<\/a>. So it helps to read those first.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is a Go Circuit Breaker?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In a house, a circuit breaker cuts the power when too much current flows. It protects the wires from a fire, because less current can flow. Then, after you fix the problem, you flip the switch back on. A Go circuit breaker works in a very similar way for function calls, so the idea is easy to remember.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Your program wraps a risky call, such as a payment API, inside the breaker. While the calls work, the breaker stays out of the way. When too many calls fail in a row, the breaker &#8220;opens&#8221;. From then on, new calls fail at once, and the payment API is not touched.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After a cooldown, the breaker lets one trial call through. When that call works, the breaker closes again. Otherwise, it opens for another cooldown. Because of this loop, a Go circuit breaker heals itself without any human help.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why a Go Circuit Breaker Matters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Also, retries help with short problems. However, a service that is down for ten minutes needs a different answer. That answer is a Go circuit breaker. When every request waits for a timeout, your own service slows down and runs out of resources. Here is what a Go circuit breaker gives you.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Fast failures<\/strong>: callers get an answer in microseconds, not after a long timeout.<\/li>\n\n\n\n<li><strong>Protected resources<\/strong>: goroutines and connections are not stuck on a dead service.<\/li>\n\n\n\n<li><strong>Time to recover<\/strong>: the failing service gets quiet, so it can restart.<\/li>\n\n\n\n<li><strong>Clear signals<\/strong>: you can log and graph every time the breaker opens.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Without a breaker, one slow dependency can bring down a whole chain of services. This effect is called a cascading failure, and it is hard to stop later. As a result, many teams treat a breaker as basic equipment for any call across the network.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How a Go Circuit Breaker Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A breaker is a small state machine with three states. Then it moves between them based on the results of calls.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-1-1024x640.png\" alt=\"Go Circuit Breaker diagram showing the closed, open and half-open states and how a call moves between them\" class=\"wp-image-436\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">The breaker starts closed. After enough failures it opens. After a cooldown it becomes half-open, and one trial call decides whether it closes or opens again.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">The Closed State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">While the breaker is closed, every call goes through. Meanwhile, the breaker counts failures in a row. Then a success resets the count to zero. When the count reaches the limit, the breaker moves to open.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Open State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In the open state, calls fail at once with a special error, and the real function is not called. The breaker also remembers the time when it opened, so it knows when the cooldown is over.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Half-Open State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After the cooldown, the next call becomes a trial. When it works, the breaker closes. When it fails, the breaker opens again, and then the cooldown restarts. So the half-open state is a careful test, not a full return to normal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go Circuit Breaker vs Retry<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">People often mix up these two tools, but they solve different problems. Still, they work well together. The table below compares them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Retry with backoff<\/th><th>Circuit breaker<\/th><\/tr><\/thead><tbody><tr><td>Goal<\/td><td>Get a success after a short glitch<\/td><td>Stop calling a service that is down<\/td><\/tr><tr><td>Acts on<\/td><td>One call<\/td><td>Many calls over time<\/td><\/tr><tr><td>Helps when<\/td><td>Failure is brief<\/td><td>Failure lasts a while<\/td><\/tr><tr><td>Risk if missing<\/td><td>Visible errors from small glitches<\/td><td>Slow, stuck callers<\/td><\/tr><tr><td>Memory<\/td><td>None between calls<\/td><td>Counts failures and remembers state<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-2-1024x640.png\" alt=\"Go Circuit Breaker diagram comparing a retry with a circuit breaker\" class=\"wp-image-437\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">A retry loop repeats one call with growing waits, while a breaker watches many calls and blocks them when the failure count is too high.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In practice, you put the retry inside the breaker or the breaker around the retry, depending on your needs. A common choice is to let the breaker see the final result of the retry helper. That way, one bad call counts as a single failure, so the count stays fair.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go Circuit Breaker Hands-On: Build a Breaker<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s write the code. We keep it small, so every line is easy to follow, and we use only the standard library.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: The State and the Struct<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, we define the three states with <code>iota<\/code>, which numbers them for us. Next, the <code>Breaker<\/code> struct holds a mutex, the current state, the failure count and a clock function. The clock is replaceable, so a test can move time forward without sleeping.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var ErrOpen = errors.New(\"circuit open\")\n\ntype State int\n\nconst (\n\tClosed State = iota\n\tOpen\n\tHalfOpen\n)\n\nfunc (s State) String() string { return [...]string{\"closed\", \"open\", \"half-open\"}[s] }\n\ntype Breaker struct {\n\tmu          sync.Mutex\n\tstate       State\n\tfailures    int\n\tmaxFailures int\n\tcooldown    time.Duration\n\topenedAt    time.Time\n\tnow         func() time.Time\n}\n\nfunc NewBreaker(maxFailures int, cooldown time.Duration) *Breaker {\n\treturn &amp;Breaker{maxFailures: maxFailures, cooldown: cooldown, now: time.Now}\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: The Do Method<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Then comes the main method. It checks the state under the lock and releases it while the real function runs. Then it takes the lock again to record the result. This order matters, because holding a lock during a slow call would block every other caller.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (b *Breaker) Do(fn func() error) error {\n\tb.mu.Lock()\n\tif b.state == Open {\n\t\tif b.now().Sub(b.openedAt) &lt; b.cooldown {\n\t\t\tb.mu.Unlock()\n\t\t\treturn ErrOpen\n\t\t}\n\t\tb.state = HalfOpen\n\t}\n\tb.mu.Unlock()\n\n\terr := fn()\n\n\tb.mu.Lock()\n\tdefer b.mu.Unlock()\n\tif err != nil {\n\t\tb.failures++\n\t\tif b.state == HalfOpen || b.failures &gt;= b.maxFailures {\n\t\t\tb.state = Open\n\t\t\tb.openedAt = b.now()\n\t\t}\n\t\treturn err\n\t}\n\tb.failures = 0\n\tb.state = Closed\n\treturn nil\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s look at the key lines. When the state is open and the cooldown has not passed, we return <code>ErrOpen<\/code> and never call <code>fn<\/code>. After the cooldown, we switch to half-open and let the call run. On a failure in half-open, the breaker opens again at once. Also, on a success, it resets the count and closes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Try It with a Fake Clock<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, we replace the clock with a variable, because a real wait would slow the demo. We call a failing function five times, because the limit is three. Then we move the clock forward by 11 seconds and call a working function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>clock := time.Date(2026, 5, 31, 9, 0, 0, 0, time.UTC)\nbr := NewBreaker(3, 10*time.Second)\nbr.now = func() time.Time { return clock }\n\nfor i := 1; i &lt;= 5; i++ {\n\terr := br.Do(func() error { return errors.New(\"payment API down\") })\n\tfmt.Printf(\"call %d: err=%v state=%s\\n\", i, err, br.State())\n}\n\nclock = clock.Add(11 * time.Second)\nerr := br.Do(func() error { return nil })\nfmt.Printf(\"after cooldown: err=%v state=%s\\n\", err, br.State())<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the real output from our run.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>call 1: err=payment API down state=closed\ncall 2: err=payment API down state=closed\ncall 3: err=payment API down state=open\ncall 4: err=circuit open state=open\ncall 5: err=circuit open state=open\nafter cooldown: err=&lt;nil&gt; state=closed<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, the first two failures keep the breaker closed, because the limit is three. Then the third failure opens it. Calls 4 and 5 fail at once with <code>circuit open<\/code>. Meanwhile, the payment function is never run. Finally, after the cooldown, one working call closes the breaker. This is the full life cycle of a Go circuit breaker.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go Circuit Breaker Settings<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A breaker has only a few settings, yet each one changes how it behaves. The table below explains them and gives a starting value.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Setting<\/th><th>What it controls<\/th><th>Starting value<\/th><\/tr><\/thead><tbody><tr><td>Failure limit<\/td><td>Failures in a row before it opens<\/td><td>5<\/td><\/tr><tr><td>Cooldown<\/td><td>How long it stays open<\/td><td>10 to 30 seconds<\/td><\/tr><tr><td>Trial calls<\/td><td>Calls allowed in half-open<\/td><td>1<\/td><\/tr><tr><td>Counted errors<\/td><td>Which errors count as failures<\/td><td>Timeouts and 5xx only<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Start with these values, then adjust them from real data. A short cooldown makes the breaker test the service often, while a long cooldown keeps users waiting for recovery. Likewise, a low failure limit reacts fast but may open on a short glitch. So watch your metrics for a week and tune from there.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What Callers Should Do When the Breaker Is Open<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Returning <code>ErrOpen<\/code> is only half of the job, because users never read error codes. The caller must also decide what the user sees. For example, a product page can show cached prices with a small note. A checkout page, on the other hand, may show a clear message and ask the user to try again soon. In both cases, the page loads fast, because no one waits for a dead service.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with a Go Circuit Breaker<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Holding the lock during the call<\/strong>: every caller then waits in line. So release the mutex before you run <code>fn<\/code>.<\/li>\n\n\n\n<li><strong>Counting all errors<\/strong>: a 404 is not a sign of a sick service. Therefore, count only errors that mean the service is unhealthy.<\/li>\n\n\n\n<li><strong>No cooldown<\/strong>: a breaker that reopens at once never lets the service rest. Instead, wait for a real pause.<\/li>\n\n\n\n<li><strong>One breaker for everything<\/strong>: a failure in one service should not block another. So make one breaker for each dependency.<\/li>\n\n\n\n<li><strong>Hiding the open state<\/strong>: users need a clear message. For that reason, return a distinct error such as <code>ErrOpen<\/code> and handle it.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for a Go Circuit Breaker<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Use one breaker for each dependency<\/strong>, because each service fails on its own schedule.<\/li>\n\n\n\n<li><strong>Pick a small failure limit<\/strong>, usually between three and ten.<\/li>\n\n\n\n<li><strong>Allow only one trial call in half-open<\/strong>, since many trials can overload a service that is just waking up.<\/li>\n\n\n\n<li><strong>Log every state change<\/strong>, which makes outages easy to trace.<\/li>\n\n\n\n<li><strong>Offer a fallback<\/strong>, such as a cached value or a friendly message.<\/li>\n\n\n\n<li><strong>Test with a fake clock<\/strong>, because sleeping in tests is slow.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To practise the Go circuit breaker, add a <code>successThreshold<\/code> field. The breaker should need two good calls in half-open before it closes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hint: add a counter for successes in the half-open state. Then reset it when the breaker opens again. After that, run the demo and check the state after each call.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These pages describe the packages and ideas used in this lesson.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pkg.go.dev\/sync\">sync package documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/time\">time package documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/go.dev\/tour\/methods\/17\">Go Tour: Stringers<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A Go circuit breaker blocks calls to a service that keeps failing.<\/li>\n\n\n\n<li>It has three states: closed, open and half-open.<\/li>\n\n\n\n<li>Open means calls fail at once, and half-open allows one trial.<\/li>\n\n\n\n<li>A mutex protects the state, but the lock is released during the real call.<\/li>\n\n\n\n<li>Use one breaker for each dependency, and count only real service errors.<\/li>\n\n\n\n<li>A fake clock makes the breaker easy to test.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By now, you can protect your service from a failing dependency. However, APIs also need to return long lists without sending everything at once. So next, we will cover pagination for REST APIs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next up: <a href=\"https:\/\/smitabharat.com\/blog\/go-pagination-rest-api\/\">Go pagination for REST APIs<\/a><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>A Go circuit breaker stops your program from calling a service that is already failing. In this post, you will learn the three states&hellip;<\/p>\n","protected":false},"author":1,"featured_media":438,"comment_status":"","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[15],"tags":[80,88,11],"swapz_series":[],"class_list":["post-469","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-lang","tag-advanced","tag-go-circuit-breaker","tag-go-lang"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Circuit Breaker Pattern Explained with Code<\/title>\n<meta name=\"description\" content=\"Learn how a Go circuit breaker works with closed, open and half-open states. Build one with a mutex and test it using a fake clock.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Circuit Breaker Pattern Explained with Code\" \/>\n<meta property=\"og:description\" content=\"Learn how a Go circuit breaker works with closed, open and half-open states. Build one with a mutex and test it using a fake clock.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-06-01T03:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-circuit-breaker-pattern\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-circuit-breaker-pattern\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Circuit Breaker Pattern Explained with Code\",\"datePublished\":\"2026-06-01T03:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-circuit-breaker-pattern\\\/\"},\"wordCount\":1597,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-circuit-breaker-pattern\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-circuit-breaker-pattern-featured.png\",\"keywords\":[\"Advanced\",\"Go Circuit Breaker\",\"Go Lang\"],\"articleSection\":[\"Go Lang\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-circuit-breaker-pattern\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-circuit-breaker-pattern\\\/\",\"name\":\"Go Circuit Breaker Pattern Explained with Code\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-circuit-breaker-pattern\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-circuit-breaker-pattern\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-circuit-breaker-pattern-featured.png\",\"datePublished\":\"2026-06-01T03:30:00+00:00\",\"description\":\"Learn how a Go circuit breaker works with closed, open and half-open states. Build one with a mutex and test it using a fake clock.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-circuit-breaker-pattern\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-circuit-breaker-pattern\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-circuit-breaker-pattern\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-circuit-breaker-pattern-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-circuit-breaker-pattern-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Circuit Breaker featured image: closed, open and half-open state cards\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-circuit-breaker-pattern\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Circuit Breaker Pattern Explained with Code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Circuit Breaker Pattern Explained with Code","description":"Learn how a Go circuit breaker works with closed, open and half-open states. Build one with a mutex and test it using a fake clock.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/","og_locale":"en_US","og_type":"article","og_title":"Go Circuit Breaker Pattern Explained with Code","og_description":"Learn how a Go circuit breaker works with closed, open and half-open states. Build one with a mutex and test it using a fake clock.","og_url":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-06-01T03:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Circuit Breaker Pattern Explained with Code","datePublished":"2026-06-01T03:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/"},"wordCount":1597,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-featured.png","keywords":["Advanced","Go Circuit Breaker","Go Lang"],"articleSection":["Go Lang"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/","url":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/","name":"Go Circuit Breaker Pattern Explained with Code","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-featured.png","datePublished":"2026-06-01T03:30:00+00:00","description":"Learn how a Go circuit breaker works with closed, open and half-open states. Build one with a mutex and test it using a fake clock.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-circuit-breaker-pattern-featured.png","width":2400,"height":1260,"caption":"Go Circuit Breaker featured image: closed, open and half-open state cards"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-circuit-breaker-pattern\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Circuit Breaker Pattern Explained with Code"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=469"}],"version-history":[{"count":0,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/469\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/438"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=469"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}