-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pick closest Xirsys server based on users geo location
- Loading branch information
1 parent
d0bd627
commit f75101e
Showing
11 changed files
with
183 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
insert_final_newline = true | ||
|
||
[{*.kt,*.kts}] | ||
ktlint_code_style = intellij_idea | ||
|
||
# Disable wildcard imports entirely | ||
ij_kotlin_name_count_to_use_star_import = 2147483647 | ||
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647 | ||
ij_kotlin_packages_to_use_import_on_demand = unset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,6 @@ interface XirsysProperties { | |
fun secret(): String | ||
|
||
fun channelNamespace(): String | ||
|
||
fun geoIpPath(): String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/com/faforever/icebreaker/service/xirsys/geolocation/GeoLocation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.faforever.icebreaker.service.xirsys.geolocation | ||
|
||
import kotlin.math.atan2 | ||
import kotlin.math.cos | ||
import kotlin.math.sin | ||
import kotlin.math.sqrt | ||
|
||
internal interface GeoLocation { | ||
val latitude: Double | ||
val longitude: Double | ||
|
||
// Haversine formula to calculate distance between two points in kilometers | ||
fun distanceTo(other: GeoLocation): Double { | ||
val r = 6371 // Radius of the earth in kilometers | ||
val latDistance = Math.toRadians(other.latitude - this.latitude) | ||
val lonDistance = Math.toRadians(other.longitude - this.longitude) | ||
val a = | ||
sin(latDistance / 2) * sin(latDistance / 2) + | ||
cos(Math.toRadians(this.latitude)) * cos(Math.toRadians(other.latitude)) * | ||
sin(lonDistance / 2) * sin(lonDistance / 2) | ||
val c = 2 * atan2(sqrt(a), sqrt(1 - a)) | ||
return r * c // Distance in kilometers | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/com/faforever/icebreaker/service/xirsys/geolocation/RegionSelectorService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.faforever.icebreaker.service.xirsys.geolocation | ||
|
||
import com.faforever.icebreaker.service.xirsys.XirsysProperties | ||
import com.maxmind.geoip2.DatabaseReader | ||
import com.maxmind.geoip2.model.CityResponse | ||
import jakarta.inject.Singleton | ||
import org.slf4j.LoggerFactory | ||
import java.net.InetAddress | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
|
||
private val LOG = LoggerFactory.getLogger(RegionSelectorService::class.java) | ||
|
||
@Singleton | ||
class RegionSelectorService( | ||
xirsysProperties: XirsysProperties, | ||
) { | ||
private val geoIpDatabasePath = Paths.get(xirsysProperties.geoIpPath()) | ||
|
||
private val geoLocationReader: DatabaseReader? by lazy { | ||
if (!Files.exists(geoIpDatabasePath)) { | ||
LOG.warn("No geo database found at $geoIpDatabasePath! Will use fallback region ${XirsysRegion.fallbackRegion.name}") | ||
null | ||
} else { | ||
DatabaseReader.Builder(geoIpDatabasePath.toFile()).build() | ||
} | ||
} | ||
|
||
private data class UserLocation( | ||
override val latitude: Double, | ||
override val longitude: Double, | ||
) : GeoLocation | ||
|
||
fun getClosestRegion(ipAddress: InetAddress): XirsysRegion = | ||
geoLocationReader | ||
?.tryCity(ipAddress) | ||
?.map(CityResponse::getLocation) | ||
?.map { getClosestRegion(UserLocation(latitude = it.latitude, longitude = it.longitude)) } | ||
?.orElse(null) | ||
?: XirsysRegion.fallbackRegion | ||
|
||
private fun getClosestRegion(userLocation: UserLocation): XirsysRegion = XirsysRegion.allRegions.minBy { it.distanceTo(userLocation) } | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/com/faforever/icebreaker/service/xirsys/geolocation/XirsysRegion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.faforever.icebreaker.service.xirsys.geolocation | ||
|
||
data class XirsysRegion( | ||
val domain: String, | ||
val name: String, | ||
val city: String, | ||
override val latitude: Double, | ||
override val longitude: Double, | ||
) : GeoLocation { | ||
companion object { | ||
// listed on https://docs.xirsys.com/?pg=api-intro (manually enriched with geo coordinates) | ||
val allRegions = | ||
listOf( | ||
XirsysRegion("ws.xirsys.com", "US West", "San Jose, CA", 37.33939000, -121.89496000), | ||
XirsysRegion("us.xirsys.com", "US East", "Washington, DC", 38.89511000, -77.03637000), | ||
XirsysRegion("es.xirsys.com", "Europe", "Amsterdam", 52.37403000, 4.88969000), | ||
XirsysRegion("bs.xirsys.com", "India", "Bangalore", 12.97194000, 77.59369000), | ||
XirsysRegion("tk.xirsys.com", "Japan", "Tokyo", 35.67619190, 139.65031060), | ||
XirsysRegion("hk.xirsys.com", "China", "Hong Kong", 22.396428, 114.109497), | ||
XirsysRegion("ss.xirsys.com", "Asia", "Singapore", 1.352083, 103.819836), | ||
XirsysRegion("ms.xirsys.com", "Australia", "Sydney", -33.86785, 151.20732), | ||
XirsysRegion("sp.xirsys.com", "Brazil", "São Paulo", -23.5475, -46.63611), | ||
XirsysRegion("to.xirsys.com", "Canada East", "Toronto", 43.70011, -79.4163), | ||
XirsysRegion("jb.xirsys.com", "South Africa", "Johannesburg", -26.20227, 28.04363), | ||
XirsysRegion("fr.xirsys.com", "Germany", "Frankfurt", 50.11552, 8.68417), | ||
) | ||
|
||
val fallbackRegion = allRegions.last() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters