Skip to content

Commit c2de432

Browse files
authored
Merge pull request #7662 from espoon-voltti/varda-rate-limit-change
Decrease varda api rate limit and log rate wait
2 parents eb830dc + a22ee73 commit c2de432

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

service/src/main/kotlin/fi/espoo/evaka/EvakaEnv.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ data class VardaEnv(
249249
val startDate: LocalDate?,
250250
val endDate: LocalDate?,
251251
val localDevPort: Int?,
252+
val ratePerSec: Double?,
252253
) {
253254
companion object {
254255
fun fromEnvironment(env: Environment) =
@@ -261,6 +262,9 @@ data class VardaEnv(
261262

262263
// Port that's forwarded to Varda for local development
263264
localDevPort = env.lookup("evaka.integration.varda.local_dev_port"),
265+
266+
// Varda has a rate limit of 2 requests per second
267+
ratePerSec = env.lookup("evaka.integration.varda.rate_per_sec"),
264268
)
265269
}
266270
}

service/src/main/kotlin/fi/espoo/evaka/varda/VardaClient.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,11 @@ class VardaClient(
231231
private val jsonMapper: JsonMapper,
232232
vardaBaseUrl: URI,
233233
private val basicAuth: String,
234+
ratePerSec: Double,
234235
) : VardaReadClient, VardaWriteClient, VardaUnitClient {
235236
private var token: String? = null
236237
private val baseUrl = vardaBaseUrl.ensureTrailingSlash()
238+
val rateLimiter: RateLimiter = RateLimiter.create(ratePerSec)
237239

238240
override fun getOrCreateHenkilo(
239241
body: VardaReadClient.GetOrCreateHenkiloRequest
@@ -325,12 +327,14 @@ class VardaClient(
325327

326328
override fun <T : VardaEntity> delete(data: T) = request<Unit>("DELETE", data.url)
327329

328-
val rateLimiter: RateLimiter = RateLimiter.create(2.0)
329-
330330
private inline fun <reified R> request(method: String, url: URI, body: Any? = null): R {
331-
logger.info { "requesting $method $url" + if (body == null) "" else " with body $body" }
331+
val rate = rateLimiter.acquire()
332+
333+
logger.info {
334+
"VardaClient requesting $method $url" +
335+
if (body == null) "" else " with body $body and rated wait of $rate"
336+
}
332337

333-
rateLimiter.acquire()
334338
val req =
335339
Request.Builder()
336340
.method(
@@ -406,7 +410,9 @@ class VardaClient(
406410
.header("Accept", "application/json")
407411
.build()
408412

409-
rateLimiter.acquire()
413+
val rate = rateLimiter.acquire()
414+
415+
logger.info { "VardaClient requesting get user/apikey/ with rated wait of $rate" }
410416

411417
val newToken =
412418
httpClient.newCall(req).execute().use { response ->
@@ -416,6 +422,7 @@ class VardaClient(
416422
val body = response.body?.string() ?: error("Varda API token response body is null")
417423
jsonMapper.readTree(body).get("token").asText()
418424
}
425+
logger.info { "Successfully fetched new Varda API token with rate wait of $rate" }
419426
token = newToken
420427
return newToken
421428
}

service/src/main/kotlin/fi/espoo/evaka/varda/VardaUpdateService.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ class VardaUpdateService(
9898
.build()
9999

100100
private val vardaClient =
101-
VardaClient(httpClient, jsonMapper, vardaEnv.url, vardaEnv.basicAuth.value)
101+
VardaClient(
102+
httpClient,
103+
jsonMapper,
104+
vardaEnv.url,
105+
vardaEnv.basicAuth.value,
106+
vardaEnv.ratePerSec ?: 1.0,
107+
)
102108

103109
private val vardaEnabledRange =
104110
DateRange(vardaEnv.startDate ?: VARDA_START_DATE, vardaEnv.endDate)

service/src/main/resources/application-local.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ evaka:
4141
basic_auth: ""
4242
source_system: 31
4343
url: http://localhost:8888/mock-integration/varda/api
44+
rate_per_sec: 1
4445
vtj:
4546
# these are used if you run pis-service in "vtj-dev" profile (against Test VTJ).
4647
# get-deployment-local.sh should fill the trustStore location, but you need to fill vtj user/pass with details from AWS Parameter Store

0 commit comments

Comments
 (0)