Skip to content

Commit 4d00a94

Browse files
committed
Update
1 parent 13b6d9a commit 4d00a94

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

searxng.html

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
const TIMEOUT = 5000;
2626
const ATTEMPTS = 10;
2727
const CUSTOM_PARAMS = ['attempts', 'languages', 'fast', 'instances', 'timeout'];
28+
const STATS_KEY = 'searxngRedirectorStats';
29+
const STATS_TTL_SECS = 24 * 60 * 60;
2830

2931
const tasks = [];
3032
const schedule = (f, pause) => tasks.push(setTimeout(f, pause));
@@ -33,6 +35,8 @@
3335
tasks.length = 0;
3436
};
3537

38+
const currentTime = _ => Math.round(Date.now() / 1000);
39+
3640
const loadAndShuffleInstances = async params => {
3741
const attempts = Number(params.get('attempts')) || ATTEMPTS;
3842
const instancesFromParams = params.get('instances')?.split(',') || [];
@@ -53,6 +57,8 @@
5357
const loadedInstances = (await response.json()).instances;
5458
console.log('instances', loadedInstances);
5559

60+
const searxngRedirectorStats = JSON.parse(localStorage.getItem(STATS_KEY) || '{}');
61+
console.log('stats', searxngRedirectorStats);
5662
const onion = window.location.host.endsWith('.onion');
5763
const best = Object
5864
.entries(loadedInstances)
@@ -71,7 +77,9 @@
7177
}
7278
})
7379
.map(([u, i]) => {
74-
const key = [i?.uptime?.uptimeDay, i?.uptime?.uptimeMonth, 1 / i?.engines?.google?.error_rate, 1 / i?.engines?.duckduckgo?.error_rate];
80+
const s = searxngRedirectorStats[u.hostname];
81+
const localOkRate = (s && s.ttl > currentTime()) ? (s.ok + 1) / (s.failures + 1) : 1;
82+
const key = [localOkRate, i?.uptime?.uptimeDay, i?.uptime?.uptimeMonth, 1 / i?.engines?.google?.error_rate, 1 / i?.engines?.duckduckgo?.error_rate];
7583
return [u, key];
7684
})
7785
.sort(([_a, a], [_b, b]) => {
@@ -145,6 +153,15 @@
145153
const getMethod = params.get('get') === '1';
146154
for (let i = 0; i < shuffledInstances.length; i++) schedule(_ => {
147155
window.stop();
156+
if (i > 0) {
157+
const searxngRedirectorStats = JSON.parse(localStorage.getItem(STATS_KEY) || '{}');
158+
const stats = searxngRedirectorStats[new URL(shuffledInstances[i - 1]).hostname] || { ok: 0, failures: 0, ttl: 0 };
159+
stats.failures++;
160+
stats.ttl = currentTime() + STATS_TTL_SECS;
161+
console.log('updating stats', searxngRedirectorStats);
162+
localStorage.setItem(STATS_KEY, JSON.stringify(searxngRedirectorStats));
163+
}
164+
148165
const retryTargetUrl = new URL(targetUrl.toString());
149166
retryTargetUrl.host = new URL(shuffledInstances[i]).host;
150167
if (i > 0) {

0 commit comments

Comments
 (0)