|
25 | 25 | const TIMEOUT = 5000; |
26 | 26 | const ATTEMPTS = 10; |
27 | 27 | const CUSTOM_PARAMS = ['attempts', 'languages', 'fast', 'instances', 'timeout']; |
| 28 | +const STATS_KEY = 'searxngRedirectorStats'; |
| 29 | +const STATS_TTL_SECS = 24 * 60 * 60; |
28 | 30 |
|
29 | 31 | const tasks = []; |
30 | 32 | const schedule = (f, pause) => tasks.push(setTimeout(f, pause)); |
|
33 | 35 | tasks.length = 0; |
34 | 36 | }; |
35 | 37 |
|
| 38 | +const currentTime = _ => Math.round(Date.now() / 1000); |
| 39 | + |
36 | 40 | const loadAndShuffleInstances = async params => { |
37 | 41 | const attempts = Number(params.get('attempts')) || ATTEMPTS; |
38 | 42 | const instancesFromParams = params.get('instances')?.split(',') || []; |
|
53 | 57 | const loadedInstances = (await response.json()).instances; |
54 | 58 | console.log('instances', loadedInstances); |
55 | 59 |
|
| 60 | + const searxngRedirectorStats = JSON.parse(localStorage.getItem(STATS_KEY) || '{}'); |
| 61 | + console.log('stats', searxngRedirectorStats); |
56 | 62 | const onion = window.location.host.endsWith('.onion'); |
57 | 63 | const best = Object |
58 | 64 | .entries(loadedInstances) |
|
71 | 77 | } |
72 | 78 | }) |
73 | 79 | .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]; |
75 | 83 | return [u, key]; |
76 | 84 | }) |
77 | 85 | .sort(([_a, a], [_b, b]) => { |
|
145 | 153 | const getMethod = params.get('get') === '1'; |
146 | 154 | for (let i = 0; i < shuffledInstances.length; i++) schedule(_ => { |
147 | 155 | 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 | + |
148 | 165 | const retryTargetUrl = new URL(targetUrl.toString()); |
149 | 166 | retryTargetUrl.host = new URL(shuffledInstances[i]).host; |
150 | 167 | if (i > 0) { |
|
0 commit comments