Skip to content

Commit 3af5d31

Browse files
author
Ryo-Natori
committed
Added concurrency option
1 parent 0613ebf commit 3af5d31

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

src/main/scala/com/github/zabbicook/api/ZabbixApi.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ class ZabbixApi(conf: ZabbixApiConf) extends Logging {
2323
private[this] val sequence = new Random
2424

2525
private[this] val throttle: Throttle = {
26-
if (conf.interval.isZero) {
26+
if (conf.concurrency && conf.interval.isZero) {
2727
NonStrictThrottle
2828
} else {
2929
new StrictThrottle(
30-
concurrency = 1,
30+
concurrency = if (conf.concurrency) 10 else 1,
3131
timeout = conf.timeout.multipliedBy(3),
3232
startInterval = conf.interval
3333
)

src/main/scala/com/github/zabbicook/api/ZabbixApiConf.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import scala.concurrent.ExecutionContext
1212
* @param authUser admin user
1313
* @param authPass admin password
1414
* @param interval interval of calling api
15+
* @param concurrency flag of concurrency
1516
* @param executionContext execution context
1617
*/
1718
case class ZabbixApiConf(
@@ -20,6 +21,7 @@ case class ZabbixApiConf(
2021
authUser: String,
2122
authPass: String,
2223
interval: Duration,
24+
concurrency: Boolean,
2325
executionContext: ExecutionContext = ExecutionContext.global,
2426
timeout: Duration = Duration.ofSeconds(20)
2527
) {

src/main/scala/com/github/zabbicook/cli/Arguments.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ case class Arguments(
1616
showDoc: Boolean = false,
1717
docDepth: Int = Int.MaxValue,
1818
docRoot: String = "",
19-
apiInterval: Duration = Duration.ZERO
19+
apiInterval: Duration = Duration.ZERO,
20+
apiConcurrency: Boolean = false
2021
)
2122

2223
object Arguments {
@@ -72,12 +73,18 @@ object Arguments {
7273
.action((x, c) => c.copy(docRoot = x))
7374
.text("The root of the tree displayed by '--doc'.")
7475

75-
opt[Int]('s', "sleep").optional()
76+
opt[Int]("interval").optional()
7677
.valueName("<interval msec>")
7778
.action((x, c) => c.copy(apiInterval = Duration.ofMillis(Math.min(Math.max(x, 0), 1000))))
7879
.text("Specify the interval of calling zabbix api in milliseconds. The default is 0 and maximum value is 1000" + "" +
7980
" If you do not want to load the zabbix server and database, set the interval.")
8081

82+
opt[Int]('c', "concurrency").optional()
83+
.action((_, c) => c.copy(apiConcurrency = true))
84+
.text("By default, calling Zabbix api is serialized. When set this option, optimized for concurrent api operations." +
85+
" If you do not want to stress the server, leave it as default.")
86+
87+
8188
opt[Unit]('d', "debug").optional()
8289
.action((_, c) => c.copy(isDebug = true))
8390
.text("Enables debug logging.")

src/main/scala/com/github/zabbicook/cli/Runner.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class Runner(conf: Arguments) extends Logging {
2222
apiPath = conf.url.toString,
2323
authUser = conf.user,
2424
authPass = conf.pass,
25-
interval = conf.apiInterval
25+
interval = conf.apiInterval,
26+
concurrency = conf.apiConcurrency
2627
)
2728

2829
def run(): Future[RunResult] = {

src/test/scala/com/github/zabbicook/test/TestConfig.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,23 @@ trait TestConfig {
2323

2424
object TestConfig {
2525
// to avoid 'DBEXECUTE_ERROR' on travis.ci (experimental)
26-
val sleep: Duration = Duration.ofMillis(System.getProperty("sleep", "100").toInt)
26+
val sleep: Duration = Duration.ofMillis(System.getProperty("sleep", "0").toInt)
2727
println(s"sleep: ${sleep.toMillis} msec")
2828

2929
val apiConfs: Map[Version, ZabbixApiConf] = Map(
3030
Version(3,0,5) -> ZabbixApiConf(
3131
apiPath = "http://localhost:8080/api_jsonrpc.php",
3232
authUser = "Admin",
3333
authPass = "zabbix",
34-
interval = sleep
34+
interval = sleep,
35+
concurrency = false
3536
),
3637
Version(3,2,1) -> ZabbixApiConf(
3738
apiPath = "http://localhost:8081/api_jsonrpc.php",
3839
authUser = "Admin",
3940
authPass = "zabbix",
40-
interval = sleep
41+
interval = sleep,
42+
concurrency = false
4143
)
4244
)
4345
}

0 commit comments

Comments
 (0)