Skip to content

Commit

Permalink
Support configure Circuit Breaker per method
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmihailov authored and ihostage committed May 5, 2021
1 parent 0cc4803 commit 220de95
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ public class MyServiceImpl implements MyService {
* Add `org.taymyr.lagom.soap.WebFaultException` to Circuit Breakers whitelist (`lagom.circuit-breaker.default.exception-whitelist`),
because all checked SOAP exceptions boxing to `WebFaultException`. Otherwise Circuit Breaker will be opened for all SOAP exceptions.

* Configure Circuit Breaker for SOAP client `lagom.circuit-breaker.<SERVICE_CLASS>`.
* Configure Circuit Breaker for SOAP client `lagom.circuit-breaker.<SERVICE_CLASS>`.
To configure methods use `lagom.circuit-breaker.<SERVICE_CLASS>.methods.<METHOD_NAME>`
Highly recommended configuring Circuit Breaker (see all available settings in [Lagom docs](https://www.lagomframework.com/documentation/current/scala/ServiceClients.html#Circuit-Breaker-Configuration))
in `play.soap.services` block and use reference to this configuration in `lagom.circuit-breaker` block.

Expand All @@ -143,6 +144,8 @@ lagom.circuit-breaker {
]
com.foo.bar.service.Service = ${play.soap.services.com.foo.bar.service.Service.breaker}
com.foo.bar.service.Service.methods.method1 = ${play.soap.services.com.foo.bar.service.Service.methods.method1.breaker}
com.foo.bar.service.Service.methods.method2 = ${play.soap.services.com.foo.bar.service.Service.methods.method2.breaker}
}
Expand All @@ -154,6 +157,18 @@ play.soap.services {
breaker = {
call-timeout = 10s
}
methods {
method1 {
breaker {
call-timeout = 20s
}
}
method2 {
breaker {
call-timeout = 30s
}
}
}
}
}
```
Expand Down
20 changes: 15 additions & 5 deletions java/src/main/kotlin/org/taymyr/lagom/soap/ServiceProviderImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ constructor(

override fun invoke(self: Any?, thisMethod: Method?, proceed: Method?, args: Array<out Any>?): Any {
val port = if (isSingleton) this.port else createPort()
return breakersProvider.get().withCircuitBreaker(name) {
val (cbName, cbConfig) =
if (breakerConfig.hasPath("methods.${thisMethod?.name}"))
"$name.methods.${thisMethod?.name}" to breakerConfig.getConfig("methods.${thisMethod?.name}").withFallback(breakerConfig)
else name to breakerConfig
val timeout = cbConfig.getDuration("call-timeout", MILLISECONDS)
configureTimeout(port, timeout)
return breakersProvider.get().withCircuitBreaker(cbName) {
try {
invokeService(port, thisMethod!!, args ?: emptyArray())
} catch (e: IllegalAccessException) {
Expand Down Expand Up @@ -160,13 +166,17 @@ constructor(
val port = getPortMethod.invoke(service, soapHandlers as Any) as P
val proxy = Proxy.getInvocationHandler(port) as PlayJaxWsClientProxy
val httpClientPolicy = (proxy.client.conduit as HTTPConduit).client
val timeout = breakerConfig.getDuration("call-timeout", MILLISECONDS)
httpClientPolicy.receiveTimeout = timeout
httpClientPolicy.connectionTimeout = timeout
httpClientPolicy.connectionRequestTimeout = timeout
httpClientPolicy.browserType = config.extract("browser-type") ?: "lagom"
afterInit(port)
return port
}

private fun configureTimeout(port: P, timeout: Long) {
val proxy = Proxy.getInvocationHandler(port) as PlayJaxWsClientProxy
val httpClientPolicy = (proxy.client.conduit as HTTPConduit).client
httpClientPolicy.receiveTimeout = timeout
httpClientPolicy.connectionTimeout = timeout
httpClientPolicy.connectionRequestTimeout = timeout
}
}
}

0 comments on commit 220de95

Please sign in to comment.