• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TouK / nussknacker / 5976637142

25 Aug 2023 01:43PM UTC coverage: 81.47% (+0.03%) from 81.438%
5976637142

push

github

Filemon279
Fix migration in 1.11

25 of 25 new or added lines in 2 files covered. (100.0%)

14865 of 18246 relevant lines covered (81.47%)

5.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.29
/utils/http-utils/src/main/scala/pl/touk/nussknacker/http/backend/HttpClientConfig.scala
1
package pl.touk.nussknacker.http.backend
2

3
import net.ceedubs.ficus.readers.ValueReader
4
import org.asynchttpclient.DefaultAsyncHttpClientConfig
5
import pl.touk.nussknacker.http.backend.HttpClientConfig.EffectiveHttpClientConfig
6
import sttp.client3.SttpBackendOptions
7

8
import scala.concurrent.duration._
9

10
case class HttpClientConfig(timeout: Option[FiniteDuration],
11
                            connectTimeout: Option[FiniteDuration],
12
                            maxPoolSize: Option[Int],
13
                            useNative: Option[Boolean],
14
                            followRedirect: Option[Boolean],
15
                            forceShutdown: Option[Boolean],
16
                            //this can be used to tune single scenario
17
                            configForProcess: Option[Map[String, HttpClientConfig]]) {
18

19
  def toAsyncHttpClientConfig(processId: Option[String]): DefaultAsyncHttpClientConfig.Builder = {
20
    val effectiveConfig = toEffectiveHttpClientConfig(processId)
2✔
21
    new DefaultAsyncHttpClientConfig.Builder()
22
      .setConnectTimeout(effectiveConfig.connectTimeout.toMillis.toInt)
2✔
23
      .setRequestTimeout(effectiveConfig.timeout.toMillis.toInt)
2✔
24
      .setIoThreadsCount(effectiveConfig.maxPoolSize)
2✔
25
      .setUseNativeTransport(effectiveConfig.useNative)
2✔
26
      .setFollowRedirect(effectiveConfig.followRedirect)
2✔
27
      .setThreadPoolName(processId.map(_ + s"-http-pool").getOrElse(s"http-pool"))
1✔
28
  }
29

30
  def toSttpBackendOptions(processId: Option[String]): SttpBackendOptions = {
31
    val effectiveConfig = toEffectiveHttpClientConfig(processId)
×
32
    SttpBackendOptions.Default.copy(connectionTimeout = effectiveConfig.connectTimeout)
×
33
  }
34

35
  private def toEffectiveHttpClientConfig(processId: Option[String]): EffectiveHttpClientConfig = {
36
    def extractConfig[T](extract: HttpClientConfig => Option[T], default: T): T = {
37
      val specificConfig = processId.flatMap(id => configForProcess.flatMap(_.get(id)))
×
38
      specificConfig.flatMap(extract).orElse(extract(this)).getOrElse(default)
2✔
39
    }
40
    EffectiveHttpClientConfig(
2✔
41
      timeout = extractConfig(_.timeout, DefaultHttpClientConfig.timeout),
2✔
42
      connectTimeout = extractConfig(_.connectTimeout, DefaultHttpClientConfig.timeout),
2✔
43
      maxPoolSize = extractConfig(_.maxPoolSize, DefaultHttpClientConfig.maxPoolSize),
2✔
44
      //FIXME: does not work by default?
45
      useNative = extractConfig(_.useNative, false),
2✔
46
      followRedirect = extractConfig(_.followRedirect, false),
2✔
47
      forceShutdown = extractConfig(_.forceShutdown, false)
2✔
48
    )
49
  }
50
}
51

52
object HttpClientConfig {
53

54
  private case class EffectiveHttpClientConfig(timeout: FiniteDuration,
55
                                               connectTimeout: FiniteDuration,
56
                                               maxPoolSize: Int,
57
                                               useNative: Boolean,
58
                                               followRedirect: Boolean,
59
                                               forceShutdown: Boolean)
60

61
  //ArbitraryTypeReader cannot handle nested option here... :/
62
  implicit val vr: ValueReader[HttpClientConfig] = ValueReader.relative(conf => {
2✔
63
    import net.ceedubs.ficus.Ficus._
64
    def forOption[T](path: String)(implicit r: ValueReader[T]) = optionValueReader[T].read(conf, path)
×
65
    HttpClientConfig(
×
66
      timeout = forOption[FiniteDuration]("timeout"),
×
67
      connectTimeout = forOption[FiniteDuration]("connectTimeout"),
×
68
      maxPoolSize = forOption[Int]("maxPoolSize"),
×
69
      useNative = forOption[Boolean]("useNative"),
×
70
      followRedirect = forOption[Boolean]("followRedirect"),
×
71
      forceShutdown = forOption[Boolean]("forceShutdown"),
×
72
      configForProcess = forOption("configForProcess")(mapValueReader(vr))
×
73
    )
74
  })
75
}
76

77
object DefaultHttpClientConfig {
78

79
  def apply() : HttpClientConfig = HttpClientConfig(None, None, None, None, None, None, None)
2✔
80

81
  val maxPoolSize: Int = 20
2✔
82

83
  val timeout: FiniteDuration = 10 seconds
2✔
84

85
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc