This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
MongoConfig.scala
122 lines (102 loc) · 4.15 KB
/
MongoConfig.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright 2021 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package play.modules.reactivemongo
import java.util.concurrent.TimeUnit
import javax.inject.{Inject, Singleton}
import play.api.{Configuration, Environment, Mode, PlayException}
import reactivemongo.api.FailoverStrategy
import scala.concurrent.duration.FiniteDuration
@Singleton
class MongoConfig(
environment: Environment,
configuration: Configuration,
delayFactorFinder: Option[Configuration] => Int => Double
) {
@Inject() def this(environment: Environment, configuration: Configuration) =
this(environment, configuration, DelayFactor)
lazy val uri: String =
mongoConfig
.getOptional[String]("uri")
.getOrElse(throw new IllegalStateException("mongodb.uri not defined"))
lazy val maybeFailoverStrategy: Option[FailoverStrategy] =
mongoConfig
.getOptional[Configuration]("failoverStrategy")
.map { fs =>
val initialDelay: FiniteDuration =
fs
.getOptional[Long]("initialDelayMsecs")
.map(delay => new FiniteDuration(delay, TimeUnit.MILLISECONDS))
.getOrElse(FailoverStrategy().initialDelay)
val retries: Int =
fs
.getOptional[Int]("retries")
.getOrElse(FailoverStrategy().retries)
FailoverStrategy().copy(
initialDelay = initialDelay,
retries = retries,
delayFactor = delayFactorFinder(fs.getOptional[Configuration]("delay"))
)
}
lazy val dbTimeout: Option[FiniteDuration] =
mongoConfig
.getOptional[Long]("dbTimeoutMsecs")
.map(dbTimeout => new FiniteDuration(dbTimeout, TimeUnit.MILLISECONDS))
lazy val defaultHeartbeatFrequencyMS: Option[Int] =
mongoConfig
.getOptional[Int]("defaultHeartbeatFrequencyMS")
.orElse(configuration.getOptional[Int]("platform.mongodb.defaultHeartbeatFrequencyMS")) // see BDOG-910 comments
private lazy val mongoConfig: Configuration =
configuration
.getOptional[Configuration]("mongodb")
.orElse(configuration.getOptional[Configuration](s"${environment.mode}.mongodb"))
.orElse(configuration.getOptional[Configuration](s"${Mode.Dev}.mongodb"))
.getOrElse(throw new Exception("The application does not contain required mongodb configuration"))
lazy val diagnostics: Boolean =
mongoConfig
.getOptional[Boolean]("diagnostics")
.getOrElse(false)
}
private object DelayFactor extends (Option[Configuration] => Int => Double) {
def apply(delay: Option[Configuration]): Int => Double =
delay match {
case Some(df: Configuration) =>
val delayFactor = df.getOptional[Double]("factor").getOrElse(1.0)
df.getOptional[String]("function") match {
case Some("linear") => linear(delayFactor)
case Some("exponential") => exponential(delayFactor)
case Some("static") => static(delayFactor)
case Some("fibonacci") => fibonacci(delayFactor)
case unsupported =>
throw new PlayException(
"ReactiveMongoPlugin Error",
s"Invalid Mongo configuration for delay function: unknown '$unsupported' function"
)
}
case _ => FailoverStrategy().delayFactor
}
private def linear(f: Double): Int => Double =
n => n * f
private def exponential(f: Double): Int => Double =
n => scala.math.pow(n, f)
private def static(f: Double): Int => Double =
n => f
private def fibonacci(f: Double): Int => Double =
n => f * (fib take n).last
private def fib: Stream[Long] = {
def tail(h: Long, n: Long): Stream[Long] = h #:: tail(n, h + n)
tail(0, 1)
}
}