feat: vertd server fallbacks (#157)

fixes #157
This commit is contained in:
Maya 2025-10-25 09:36:23 +03:00
parent d1ef377068
commit 3b0eb40c08
No known key found for this signature in database
1 changed files with 25 additions and 3 deletions

View File

@ -81,13 +81,35 @@ export class VertdInstance {
}
public async url() {
const reachable = async (url: string) => {
try {
const res = await fetch(url + "/api/version", {
method: "GET",
cache: "no-store",
});
return res.ok;
} catch {
return false;
}
};
switch (this.inner.type) {
case "auto": {
if (!this.cachedIp) {
this.cachedIp = await ip();
if (!this.cachedIp) this.cachedIp = await ip();
const ipInfo = this.cachedIp;
const primary = this.geographicallyOptimalInstance(ipInfo);
// try primary (closest) first
if (await reachable(primary)) return primary;
// fall back to other locations
for (const location of LOCATIONS) {
if (location.url === primary) continue;
if (await reachable(location.url)) return location.url;
}
return this.geographicallyOptimalInstance(this.cachedIp);
// if none are reachable, fall back to custom
return Settings.instance.settings.vertdURL;
}
case "eu": {