Skip to content
Permalink
Browse files

Using DSLMarker annotation to so that callers will not have implicit …

…access to multiple receivers whose classes are in the set of annotated classes.
  • Loading branch information...
johanhaleby committed Oct 21, 2018
1 parent 517b2b4 commit f3f3462c5618eb8e8f654603760a66f79308e515
@@ -55,11 +55,24 @@ inline fun <reified T : Any> hystrixObservableCommand(block: KystrixObservableCo
*}
* ```
*/
inline fun <reified T : Any> hystrixCommand(block: KystrixCommand<T>.() -> Unit): T = KystrixCommand<T>().apply(block).build().execute()
fun <T> hystrixCommand(block: KystrixCommand<T>.() -> Unit): T {
val kystrixCommand = KystrixCommand<T>()
block(kystrixCommand)
val hystrixCommand: HystrixCommand<T> = kystrixCommand.build()
return hystrixCommand.execute()
}

/**
* Kystrix DSL marker annotation which is used to limit callers so that they will not have implicit access to multiple receivers whose classes are in the set of annotated classes.
*/
@DslMarker
@Target(AnnotationTarget.TYPE, AnnotationTarget.CLASS)
annotation class KystrixDSL

/**
* A base-class for shared Kystrix DSL functionality. This should never be used directly.
*/
@KystrixDSL
abstract class KystrixBase {
internal lateinit var commandKey: HystrixCommandKey
internal lateinit var groupKey: HystrixCommandGroupKey
@@ -96,26 +109,27 @@ abstract class KystrixBase {
/**
* Configure the Hystrix command properties, see [HystrixCommandProperties].
*/
fun commandProperties(block: HystrixCommandProperties.Setter.() -> Unit) {
fun commandProperties(block: (@KystrixDSL HystrixCommandProperties.Setter).() -> Unit) {
commandProperties = HystrixCommandProperties.Setter().apply(block)
}
}

/**
* Class used in the Kystrix DSL to define aspects of [HystrixObservableCommand]'s. Should not be used directly, rather refer to [hystrixObservableCommand].
*/
@KystrixDSL
class KystrixObservableCommand<T> : KystrixBase() {

private lateinit var command: () -> Observable<T?>
private var fallback: (() -> Observable<T?>)? = null
private lateinit var command: (@KystrixDSL Unit).() -> Observable<T?>
private var fallback: ((@KystrixDSL Unit).() -> Observable<T?>)? = null

/**
* Define the actual logic that will be called by Hystrix and thus wrapped in a circuit-breaker. This is typically an HTTP or database request.
*
* @param block The code block that will be executed by Hystrix.
* @see HystrixObservableCommand.construct
*/
fun command(block: () -> Observable<T?>) {
fun command(block: (@KystrixDSL Unit).() -> Observable<T?>) {
command = block
}

@@ -125,7 +139,7 @@ class KystrixObservableCommand<T> : KystrixBase() {
* @param block The code block that will be executed by Hystrix when the circuit-breaker is open.
* @see HystrixObservableCommand.resumeWithFallback
*/
fun fallback(block: () -> Observable<T?>) {
fun fallback(block: (@KystrixDSL Unit).() -> Observable<T?>) {
fallback = block
}

@@ -137,20 +151,21 @@ class KystrixObservableCommand<T> : KystrixBase() {
.andCommandKey(commandKey)
.andCommandPropertiesDefaults(commandProperties)
return object : HystrixObservableCommand<T>(settings) {
override fun construct(): Observable<T?> = command()
override fun resumeWithFallback(): Observable<T?> = if (fallback == null) super.resumeWithFallback() else fallback!!()
override fun construct(): Observable<T?> = command(Unit)
override fun resumeWithFallback(): Observable<T?> = if (fallback == null) super.resumeWithFallback() else fallback!!(Unit)
}.toObservable()
}
}

/**
* Class used in the Kystrix DSL to define aspects of [HystrixCommand]'s. Should not be used directly, rather refer to [hystrixCommand].
*/
@KystrixDSL
class KystrixCommand<T> : KystrixBase() {

private lateinit var command: () -> T?
private lateinit var command: (@KystrixDSL Unit).() -> T?
private var threadPoolKey: HystrixThreadPoolKey? = null
private var definedFallback: (() -> T?)? = null
private var definedFallback: ((@KystrixDSL Unit).() -> T?)? = null
private var threadPoolProperties: HystrixThreadPoolProperties.Setter = HystrixThreadPoolProperties.Setter()

/**
@@ -159,7 +174,7 @@ class KystrixCommand<T> : KystrixBase() {
* @param block The code block that will be executed by Hystrix.
* @see HystrixCommand.run
*/
fun command(block: () -> T?) {
fun command(block: (@KystrixDSL Unit).() -> T?) {
command = block
}

@@ -169,7 +184,7 @@ class KystrixCommand<T> : KystrixBase() {
* @param block The code block that will be executed by Hystrix when the circuit-breaker is open.
* @see HystrixCommand.getFallback
*/
fun fallback(block: () -> T?) {
fun fallback(block: (@KystrixDSL Unit).() -> T?) {
definedFallback = block
}

@@ -190,7 +205,7 @@ class KystrixCommand<T> : KystrixBase() {
/**
* Configure the Hystrix thread-pool properties, see [HystrixThreadPoolProperties].
*/
fun threadPoolProperties(block: HystrixThreadPoolProperties.Setter.() -> Unit) {
fun threadPoolProperties(block: (@KystrixDSL HystrixThreadPoolProperties.Setter).() -> Unit) {
threadPoolProperties = HystrixThreadPoolProperties.Setter().apply(block)
}

@@ -205,8 +220,8 @@ class KystrixCommand<T> : KystrixBase() {
.andThreadPoolPropertiesDefaults(threadPoolProperties)

return object : HystrixCommand<T>(settings) {
override fun run() = command()
override fun getFallback(): T? = if (definedFallback == null) super.getFallback() else definedFallback!!()
override fun run() = command.invoke(Unit)
override fun getFallback(): T? = if (definedFallback == null) super.getFallback() else definedFallback!!(Unit)
}
}
}
@@ -31,7 +31,7 @@ import rx.RxReactiveStreams
*
* **NOTE:** This command will block the Mono! You might want to look at using a [KystrixObservableCommand] (i.e. creating a DSL from [hystrixObservableCommand] instead).
*/
fun <T> KystrixCommand<T>.monoCommand(block: () -> Mono<T>) {
fun <T> KystrixCommand<T>.monoCommand(block: (@KystrixDSL Unit).() -> Mono<T>) {
command {
block().block()
}
@@ -61,7 +61,7 @@ fun <T> KystrixCommand<T>.monoCommand(block: () -> Mono<T>) {
*}
* ```
*/
fun <T> KystrixObservableCommand<T>.monoCommand(block: () -> Mono<T>) {
fun <T> KystrixObservableCommand<T>.monoCommand(block: (@KystrixDSL Unit).() -> Mono<T>) {
command {
Observable.from(block().toFuture())
}
@@ -92,7 +92,7 @@ fun <T> KystrixObservableCommand<T>.monoCommand(block: () -> Mono<T>) {
*}
* ```
*/
fun <T> KystrixObservableCommand<T>.fluxCommand(block: () -> Flux<T>) {
fun <T> KystrixObservableCommand<T>.fluxCommand(block: (@KystrixDSL Unit).() -> Flux<T>) {
command {
RxReactiveStreams.toObservable(block())
}

0 comments on commit f3f3462

Please sign in to comment.
You can’t perform that action at this time.