Skip to content

Commit c41228f

Browse files
authored
fix: randomize fallback hosts (#319)
1 parent 163e3b7 commit c41228f

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

client/src/commonMain/kotlin/com/algolia/search/transport/internal/Host.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@ internal val ApplicationID.searchHosts
1010
get() = listOf(
1111
RetryableHost("$this-dsn.algolia.net", CallType.Read),
1212
RetryableHost("$this.algolia.net", CallType.Write),
13+
) + mutableListOf(
1314
RetryableHost("$this-1.algolianet.com"),
1415
RetryableHost("$this-2.algolianet.com"),
1516
RetryableHost("$this-3.algolianet.com")
16-
)
17+
).apply { shuffle() }
1718

1819
internal val insightHosts = listOf(RetryableHost("insights.algolia.io"))
1920
internal val Region.Analytics.hosts get() = listOf(RetryableHost("analytics.$this.algolia.com"))
2021
internal val Region.Personalization.hosts get() = listOf(RetryableHost("personalization.$this.algolia.com"))
2122
internal val placesHosts = listOf(
2223
RetryableHost("places-dsn.algolia.net"),
24+
) + mutableListOf(
2325
RetryableHost("places-1.algolianet.com"),
2426
RetryableHost("places-2.algolianet.com"),
2527
RetryableHost("places-3.algolianet.com")
26-
)
28+
).apply { shuffle() }
2729

2830
@PublishedApi
2931
internal fun RetryableHost.reset() {

client/src/commonTest/kotlin/Helper.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ internal infix fun <T> Collection<T>?.shouldNotContain(element: T) {
3838
(this?.contains(element) ?: false).shouldBeFalse()
3939
}
4040

41+
internal infix fun <T> Collection<T>?.shouldContainAll(collection: Collection<T>) {
42+
(this?.containsAll(collection) ?: false).shouldBeTrue()
43+
}
44+
4145
internal fun <T> Collection<T>.shouldBeEmpty() {
4246
this.isEmpty().shouldBeTrue()
4347
}
@@ -77,3 +81,4 @@ internal inline fun <reified T : Throwable> shouldFailWith(noinline block: suspe
7781
}
7882
}
7983
}
84+

client/src/commonTest/kotlin/configuration/TestConfiguration.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import io.ktor.client.features.logging.LogLevel
2020
import shouldBeNull
2121
import shouldEqual
2222
import kotlin.test.Test
23+
import shouldContainAll
2324

2425
internal class TestConfiguration {
2526

@@ -39,7 +40,7 @@ internal class TestConfiguration {
3940
writeTimeout shouldEqual DEFAULT_WRITE_TIMEOUT
4041
readTimeout shouldEqual DEFAULT_READ_TIMEOUT
4142
logLevel shouldEqual LogLevel.NONE
42-
hosts shouldEqual applicationID.searchHosts
43+
hosts shouldContainAll applicationID.searchHosts
4344
defaultHeaders.shouldBeNull()
4445
engine.shouldBeNull()
4546
httpClientConfig.shouldBeNull()

client/src/commonTest/kotlin/transport/TestCallableHosts.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.algolia.search.transport.internal.Transport
88
import runBlocking
99
import shouldEqual
1010
import kotlin.test.Test
11+
import shouldContainAll
1112

1213
internal class TestCallableHosts {
1314

@@ -24,7 +25,7 @@ internal class TestCallableHosts {
2425
runBlocking {
2526
val hosts = transport.callableHosts(CallType.Read)
2627

27-
hosts shouldEqual listOf(
28+
hosts shouldContainAll listOf(
2829
hostRead,
2930
hostFallback1,
3031
hostFallback2,
@@ -38,7 +39,7 @@ internal class TestCallableHosts {
3839
runBlocking {
3940
val hosts = transport.callableHosts(CallType.Write)
4041

41-
hosts shouldEqual listOf(
42+
hosts shouldContainAll listOf(
4243
hostWrite,
4344
hostFallback1,
4445
hostFallback2,
@@ -67,7 +68,7 @@ internal class TestCallableHosts {
6768

6869
val hosts = transport.callableHosts(CallType.Read)
6970

70-
hosts shouldEqual listOf(hostfallback3)
71+
hosts shouldEqual listOf(transport.hosts.last())
7172
}
7273
}
7374

@@ -78,7 +79,7 @@ internal class TestCallableHosts {
7879

7980
val hosts = transport.callableHosts(CallType.Write)
8081

81-
hosts shouldEqual listOf(
82+
hosts shouldContainAll listOf(
8283
hostWrite,
8384
hostFallback1,
8485
hostFallback2,

client/src/commonTest/kotlin/transport/TestRetryableHost.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.algolia.search.transport.internal.reset
99
import com.algolia.search.transport.internal.searchHosts
1010
import shouldEqual
1111
import kotlin.test.Test
12+
import shouldContainAll
1213

1314
internal class TestRetryableHost {
1415

@@ -17,7 +18,7 @@ internal class TestRetryableHost {
1718

1819
@Test
1920
fun searchHosts() {
20-
applicationID.searchHosts shouldEqual listOf(
21+
applicationID.searchHosts shouldContainAll listOf(
2122
RetryableHost("$applicationID-dsn.algolia.net", CallType.Read),
2223
RetryableHost("$applicationID.algolia.net", CallType.Write),
2324
RetryableHost("$applicationID-1.algolianet.com"),

0 commit comments

Comments
 (0)