-
Notifications
You must be signed in to change notification settings - Fork 18
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 #36 from lloydmeta/feature/cacheless-hystrix-prope…
…rties-strategy Implement a custom HystrixPropertiesStrategy plugin
- Loading branch information
Showing
4 changed files
with
87 additions
and
3 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
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
21 changes: 21 additions & 0 deletions
21
app/com/m3/octoparts/hystrix/KeyAndBuilderValuesHystrixPropertiesStrategy.scala
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,21 @@ | ||
package com.m3.octoparts.hystrix | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.netflix.hystrix.{ HystrixCommandKey, HystrixCommandProperties } | ||
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy | ||
|
||
/** | ||
* Custom [[HystrixPropertiesStrategy]] implementation | ||
*/ | ||
class KeyAndBuilderValuesHystrixPropertiesStrategy extends HystrixPropertiesStrategy { | ||
private val mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL) | ||
|
||
/** | ||
* Overriden to return a [[String]] that is a combination of the commandKey name and a JSON string | ||
* of the builder values | ||
*/ | ||
override def getCommandPropertiesCacheKey(commandKey: HystrixCommandKey, builder: HystrixCommandProperties.Setter): String = | ||
s"${commandKey.name()}-${mapper.writeValueAsString(builder)}" | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
test/com/m3/octoparts/hystrix/KeyAndBuilderValuesHystrixPropertiesStrategySpec.scala
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,40 @@ | ||
package com.m3.octoparts.hystrix | ||
|
||
import com.netflix.hystrix.strategy.HystrixPlugins | ||
import com.netflix.hystrix.strategy.properties.HystrixPropertiesFactory | ||
import com.netflix.hystrix.{ HystrixCommandProperties, HystrixCommandKey } | ||
import org.scalatest.{ Matchers, FunSpec } | ||
|
||
class KeyAndBuilderValuesHystrixPropertiesStrategySpec extends FunSpec with Matchers { | ||
|
||
val subject = new KeyAndBuilderValuesHystrixPropertiesStrategy | ||
val commandKey = HystrixCommandKey.Factory.asKey("hello") | ||
val commandProps = HystrixCommandProperties.Setter() | ||
describe("getCommandPropertiesCacheKey") { | ||
it("should return a combination of the commandKey name and commandProps JSON value") { | ||
val r1 = subject.getCommandPropertiesCacheKey(commandKey, commandProps) | ||
r1 should be("""hello-{}""") | ||
val r2 = subject.getCommandPropertiesCacheKey(commandKey, HystrixCommandProperties.Setter().withExecutionIsolationThreadTimeoutInMilliseconds(100)) | ||
r2 should be("""hello-{"executionIsolationThreadTimeoutInMilliseconds":100}""") | ||
} | ||
} | ||
|
||
// The following works if this test is run by itself, but | ||
describe("after registering with HystrixPlugins") { | ||
|
||
it("should allow HystrixPropertiesFactory.getCommandProperties to instantiate different HystrixCommandProperties for the same command key") { | ||
if (HystrixPlugins.getInstance().getPropertiesStrategy.getClass != subject.getClass) { | ||
fail("HystrixPlugins.getPropertiesStrategy did not return KeyAndBuilderValuesHystrixPropertiesStrategy") | ||
} | ||
val properties1 = HystrixPropertiesFactory.getCommandProperties( | ||
commandKey, | ||
HystrixCommandProperties.Setter().withExecutionIsolationThreadTimeoutInMilliseconds(300)) | ||
val properties2 = HystrixPropertiesFactory.getCommandProperties( | ||
commandKey, | ||
HystrixCommandProperties.Setter().withExecutionIsolationThreadTimeoutInMilliseconds(600)) | ||
properties1.executionIsolationThreadTimeoutInMilliseconds.get should be(300) | ||
properties2.executionIsolationThreadTimeoutInMilliseconds.get should be(600) | ||
} | ||
} | ||
|
||
} |