@@ -155,25 +155,14 @@ protected void recordResult(R result, Throwable exception) {
155155 * Transitions to the {@code newState} if not already in that state and calls any associated event listener.
156156 */
157157 protected void transitionTo (State newState , EventListener <CircuitBreakerStateChangedEvent > listener ,
158- ExecutionContext <R > context ) {
158+ ExecutionContext <R > context ) {
159159 boolean transitioned = false ;
160160 State currentState ;
161161
162162 synchronized (this ) {
163163 currentState = getState ();
164- if (!getState ().equals (newState )) {
165- switch (newState ) {
166- case CLOSED :
167- state .set (new ClosedState <>(this ));
168- break ;
169- case OPEN :
170- Duration computedDelay = computeDelay (context );
171- state .set (new OpenState <>(this , state .get (), computedDelay != null ? computedDelay : config .getDelay ()));
172- break ;
173- case HALF_OPEN :
174- state .set (new HalfOpenState <>(this ));
175- break ;
176- }
164+ if (!currentState .equals (newState )) {
165+ updateStateBasedOn (newState , context );
177166 transitioned = true ;
178167 }
179168 }
@@ -186,6 +175,23 @@ protected void transitionTo(State newState, EventListener<CircuitBreakerStateCha
186175 }
187176 }
188177
178+ // Extracted from the switch block in transitionTo
179+ private void updateStateBasedOn (State newState , ExecutionContext <R > context ) {
180+ switch (newState ) {
181+ case CLOSED :
182+ state .set (new ClosedState <>(this ));
183+ break ;
184+ case OPEN :
185+ Duration computedDelay = computeDelay (context );
186+ state .set (new OpenState <>(this , state .get (), computedDelay != null ? computedDelay : config .getDelay ()));
187+ break ;
188+ case HALF_OPEN :
189+ state .set (new HalfOpenState <>(this ));
190+ break ;
191+ }
192+ }
193+
194+
189195 /**
190196 * Records an execution failure.
191197 */
0 commit comments