Codebase list golang-gopkg-eapache-go-resiliency.v1 / ed2c12b
DRY up breaker.Go and breaker.Run Evan Huus 9 years ago
1 changed file(s) with 31 addition(s) and 46 deletion(s). Raw diff Collapse all Expand all
5454 return ErrBreakerOpen
5555 }
5656
57 var panicValue interface{}
58
59 result := func() error {
60 defer func() {
61 panicValue = recover()
62 }()
63 return work()
64 }()
65
66 if result == nil && panicValue == nil && state == closed {
67 // short-circuit the normal, success path without contending
68 // on the lock
69 return nil
70 }
71
72 b.processResult(result, panicValue)
73
74 if panicValue != nil {
75 // as close as Go lets us come to a "rethrow" although unfortunately
76 // we lose the original panicing location
77 panic(panicValue)
78 }
79
80 return result
57 return b.doWork(state, work)
8158 }
8259
8360 // Go will either return ErrBreakerOpen immediately if the circuit-breaker is
9471 return ErrBreakerOpen
9572 }
9673
97 go func() {
98 var panicValue interface{}
74 // errcheck complains about ignoring the error return value, but
75 // that's on purpose; if you want an error from a goroutine you have to
76 // get it over a channel or something
77 go b.doWork(state, work)
9978
100 result := func() error {
101 defer func() {
102 panicValue = recover()
103 }()
104 return work()
79 return nil
80 }
81
82 func (b *Breaker) doWork(state state, work func() error) error {
83 var panicValue interface{}
84
85 result := func() error {
86 defer func() {
87 panicValue = recover()
10588 }()
106
107 if result == nil && panicValue == nil && state == closed {
108 // short-circuit the normal, success path without
109 // contending on the lock
110 return
111 }
112
113 b.processResult(result, panicValue)
114
115 if panicValue != nil {
116 // as close as Go lets us come to a "rethrow" although
117 // unfortunately we lose the original panicing location
118 panic(panicValue)
119 }
89 return work()
12090 }()
12191
122 return nil
92 if result == nil && panicValue == nil && state == closed {
93 // short-circuit the normal, success path without contending
94 // on the lock
95 return nil
96 }
97
98 // oh well, I guess we have to contend on the lock
99 b.processResult(result, panicValue)
100
101 if panicValue != nil {
102 // as close as Go lets us come to a "rethrow" although unfortunately
103 // we lose the original panicing location
104 panic(panicValue)
105 }
106
107 return result
123108 }
124109
125110 func (b *Breaker) processResult(result error, panicValue interface{}) {