mirror of
https://github.com/movie-web/movie-web.git
synced 2024-11-11 04:55:07 +01:00
get worker urls from settings instead of config
This commit is contained in:
parent
1176908129
commit
a2b262b4ab
@ -1,15 +1,6 @@
|
||||
import { FetchOptions, FetchResponse, ofetch } from "ofetch";
|
||||
|
||||
import { conf } from "@/setup/config";
|
||||
|
||||
let proxyUrlIndex = Math.floor(Math.random() * conf().PROXY_URLS.length);
|
||||
|
||||
// round robins all proxy urls
|
||||
function getProxyUrl(): string {
|
||||
const url = conf().PROXY_URLS[proxyUrlIndex];
|
||||
proxyUrlIndex = (proxyUrlIndex + 1) % conf().PROXY_URLS.length;
|
||||
return url;
|
||||
}
|
||||
import { getLoadbalancedProxyUrl } from "@/utils/providers";
|
||||
|
||||
type P<T> = Parameters<typeof ofetch<T, any>>;
|
||||
type R<T> = ReturnType<typeof ofetch<T, any>>;
|
||||
@ -54,7 +45,7 @@ export function proxiedFetch<T>(url: string, ops: P<T>[1] = {}): R<T> {
|
||||
parsedUrl.searchParams.set(k, v);
|
||||
});
|
||||
|
||||
return baseFetch<T>(getProxyUrl(), {
|
||||
return baseFetch<T>(getLoadbalancedProxyUrl(), {
|
||||
...ops,
|
||||
baseURL: undefined,
|
||||
params: {
|
||||
@ -88,7 +79,7 @@ export function rawProxiedFetch<T>(
|
||||
parsedUrl.searchParams.set(k, v);
|
||||
});
|
||||
|
||||
return baseFetch.raw(getProxyUrl(), {
|
||||
return baseFetch.raw(getLoadbalancedProxyUrl(), {
|
||||
...ops,
|
||||
baseURL: undefined,
|
||||
params: {
|
||||
|
@ -8,15 +8,28 @@ import {
|
||||
} from "@movie-web/providers";
|
||||
|
||||
import { conf } from "@/setup/config";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
|
||||
const urls = conf().PROXY_URLS;
|
||||
const fetchers = urls.map((v) => makeSimpleProxyFetcher(v, fetch));
|
||||
let fetchersIndex = Math.floor(Math.random() * fetchers.length);
|
||||
const originalUrls = conf().PROXY_URLS;
|
||||
let fetchersIndex = -1;
|
||||
|
||||
export function getLoadbalancedProxyUrl() {
|
||||
const fetchers = useAuthStore.getState().proxySet ?? originalUrls;
|
||||
if (fetchersIndex === -1 || fetchersIndex >= fetchers.length) {
|
||||
fetchersIndex = Math.floor(Math.random() * fetchers.length);
|
||||
}
|
||||
const proxyUrl = fetchers[fetchersIndex];
|
||||
fetchersIndex = (fetchersIndex + 1) % fetchers.length;
|
||||
return proxyUrl;
|
||||
}
|
||||
|
||||
function makeLoadBalancedSimpleProxyFetcher() {
|
||||
const fetcher: ProviderBuilderOptions["fetcher"] = (a, b) => {
|
||||
fetchersIndex += 1 % fetchers.length;
|
||||
return fetchers[fetchersIndex](a, b);
|
||||
const currentFetcher = makeSimpleProxyFetcher(
|
||||
getLoadbalancedProxyUrl(),
|
||||
fetch
|
||||
);
|
||||
return currentFetcher(a, b);
|
||||
};
|
||||
return fetcher;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user