-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1204 from mattrjacobs/add-cancelled-state
Support unsubscription of HystrixCommands
- Loading branch information
Showing
18 changed files
with
2,325 additions
and
1,502 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
964 changes: 475 additions & 489 deletions
964
hystrix-core/src/main/java/com/netflix/hystrix/AbstractCommand.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
hystrix-core/src/main/java/com/netflix/hystrix/HystrixCachedObservable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.netflix.hystrix; | ||
|
||
import rx.Observable; | ||
import rx.Subscription; | ||
import rx.functions.Action0; | ||
import rx.functions.Action1; | ||
import rx.subjects.ReplaySubject; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class HystrixCachedObservable<R> { | ||
final AbstractCommand<R> originalCommand; | ||
final Observable<R> cachedObservable; | ||
final Subscription originalSubscription; | ||
final ReplaySubject<R> replaySubject = ReplaySubject.create(); | ||
final AtomicInteger outstandingSubscriptions = new AtomicInteger(0); | ||
|
||
/* package-private */ HystrixCachedObservable(Observable<R> originalObservable, final AbstractCommand<R> originalCommand) { | ||
this.originalSubscription = originalObservable | ||
.subscribe(replaySubject); | ||
|
||
this.cachedObservable = replaySubject | ||
.doOnUnsubscribe(new Action0() { | ||
@Override | ||
public void call() { | ||
if (outstandingSubscriptions.decrementAndGet() == 0) { | ||
originalSubscription.unsubscribe(); | ||
} | ||
} | ||
}) | ||
.doOnSubscribe(new Action0() { | ||
@Override | ||
public void call() { | ||
outstandingSubscriptions.getAndIncrement(); | ||
} | ||
}); | ||
this.originalCommand = originalCommand; | ||
} | ||
|
||
public static <R> HystrixCachedObservable<R> from(Observable<R> o, AbstractCommand<R> originalCommand) { | ||
return new HystrixCachedObservable<R>(o, originalCommand); | ||
} | ||
|
||
public static <R> HystrixCachedObservable<R> from(Observable<R> o, HystrixCollapser<?, R, ?> originalCollapser) { | ||
return new HystrixCachedObservable<R>(o, null); //??? | ||
} | ||
|
||
public static <R> HystrixCachedObservable<R> from(Observable<R> o, HystrixObservableCollapser<?, ?, R, ?> originalCollapser) { | ||
return new HystrixCachedObservable<R>(o, null); //??? | ||
} | ||
|
||
public Observable<R> toObservable() { | ||
return cachedObservable; | ||
} | ||
|
||
public Observable<R> toObservable(final AbstractCommand<R> commandToCopyStateInto) { | ||
final AtomicBoolean completionLogicRun = new AtomicBoolean(false); | ||
|
||
return cachedObservable | ||
.doOnError(new Action1<Throwable>() { | ||
@Override | ||
public void call(Throwable throwable) { | ||
if (!completionLogicRun.get()) { | ||
commandCompleted(commandToCopyStateInto); | ||
completionLogicRun.set(true); | ||
} | ||
} | ||
}) | ||
.doOnCompleted(new Action0() { | ||
@Override | ||
public void call() { | ||
if (!completionLogicRun.get()) { | ||
commandCompleted(commandToCopyStateInto); | ||
completionLogicRun.set(true); | ||
} | ||
} | ||
}) | ||
.doOnUnsubscribe(new Action0() { | ||
@Override | ||
public void call() { | ||
if (!completionLogicRun.get()) { | ||
commandUnsubscribed(commandToCopyStateInto); | ||
completionLogicRun.set(true); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private void commandCompleted(final AbstractCommand<R> commandToCopyStateInto) { | ||
commandToCopyStateInto.executionResult = originalCommand.executionResult; | ||
} | ||
|
||
private void commandUnsubscribed(final AbstractCommand<R> commandToCopyStateInto) { | ||
commandToCopyStateInto.executionResult = commandToCopyStateInto.executionResult.addEvent(HystrixEventType.CANCELLED); | ||
commandToCopyStateInto.executionResult = commandToCopyStateInto.executionResult.setExecutionLatency(-1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.