DRY up breaker.Go and breaker.Run
Evan Huus
9 years ago
54 | 54 | return ErrBreakerOpen |
55 | 55 | } |
56 | 56 | |
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) | |
81 | 58 | } |
82 | 59 | |
83 | 60 | // Go will either return ErrBreakerOpen immediately if the circuit-breaker is |
94 | 71 | return ErrBreakerOpen |
95 | 72 | } |
96 | 73 | |
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) | |
99 | 78 | |
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() | |
105 | 88 | }() |
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() | |
120 | 90 | }() |
121 | 91 | |
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 | |
123 | 108 | } |
124 | 109 | |
125 | 110 | func (b *Breaker) processResult(result error, panicValue interface{}) { |