go.temporal.io/server/common/circuitbreaker/circuitbreaker.go

87 LOC · 28 covered · 59 uncovered · 6 ranges · 5 concepts · 4 introducers · 3 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

1 package circuitbreaker
2
3 import (
4 "sync"
5 "sync/atomic"
6
7 "github.com/sony/gobreaker"
8 "go.temporal.io/server/common/dynamicconfig"
9 )
10
11 type (
12 TwoStepCircuitBreaker interface {
13 Name() string
14 State() gobreaker.State
15 Counts() gobreaker.Counts
16 Allow() (done func(success bool), err error)
17 }
18
19 // TwoStepCircuitBreakerWithDynamicSettings is a wrapper of gobreaker.TwoStepCircuitBreaker
20 // that calls the settingsFn everytime the Allow function is called and replaces the circuit
21 // breaker if there is a change in the settings object. Note that in this case, the previous
22 // state of the circuit breaker is lost.
23 TwoStepCircuitBreakerWithDynamicSettings struct {
24 name string
25 readyToTrip func(counts gobreaker.Counts) bool
26 onStateChange func(name string, from gobreaker.State, to gobreaker.State)
27
28 cb atomic.Pointer[gobreaker.TwoStepCircuitBreaker]
29 cbLock sync.Mutex
30 settings dynamicconfig.CircuitBreakerSettings
31 }
32
33 Settings struct {
34 // For the following options, check gobreaker docs for details.
35 Name string
36 ReadyToTrip func(counts gobreaker.Counts) bool
37 OnStateChange func(name string, from gobreaker.State, to gobreaker.State)
38 }
39 )
40
41 var _ TwoStepCircuitBreaker = (*TwoStepCircuitBreakerWithDynamicSettings)(nil)
42
43 // Caller must call UpdateSettings once before using this object.
44 func NewTwoStepCircuitBreakerWithDynamicSettings(
45 settings Settings,
46 > ) *TwoStepCircuitBreakerWithDynamicSettings { circuitbreaker.go ×3
47 > return &TwoStepCircuitBreakerWithDynamicSettings{
48 > name: settings.Name,
49 > readyToTrip: settings.ReadyToTrip,
50 > onStateChange: settings.OnStateChange,
51 > }
52 > }
53
54 func (c *TwoStepCircuitBreakerWithDynamicSettings) UpdateSettings(
55 ds dynamicconfig.CircuitBreakerSettings,
57 > c.cbLock.Lock()
58 > defer c.cbLock.Unlock()
59 > if c.cb.Load() != nil && ds == c.settings {
60 > return // no change circuitbreaker.go ×1
61 > }
62 > c.settings = ds circuitbreaker.go ×3
63 > c.cb.Store(gobreaker.NewTwoStepCircuitBreaker(gobreaker.Settings{
64 > Name: c.name,
65 > MaxRequests: uint32(ds.MaxRequests),
66 > Interval: ds.Interval,
67 > Timeout: ds.Timeout,
68 > ReadyToTrip: c.readyToTrip,
69 > OnStateChange: c.onStateChange,
70 > }))
71 }
72
73 > func (c *TwoStepCircuitBreakerWithDynamicSettings) Name() string { circuitbreaker.go ×1
74 > return c.cb.Load().Name()
75 > }
76
77 func (c *TwoStepCircuitBreakerWithDynamicSettings) State() gobreaker.State {
78 return c.cb.Load().State()
79 }
80
81 func (c *TwoStepCircuitBreakerWithDynamicSettings) Counts() gobreaker.Counts {
82 return c.cb.Load().Counts()
83 }
84
85 > func (c *TwoStepCircuitBreakerWithDynamicSettings) Allow() (done func(success bool), err error) { circuitbreaker.go ×1
86 > return c.cb.Load().Allow()
87 > }