Overview
Monitors primary search API health and automatically routes queries to a backup provider when the primary fails. Implements a circuit breaker that trips after consecutive errors and resets after a cooldown period with successful health checks.
Trigger
Every search API call (wrapper function)
Schedule
On every search call (middleware pattern)
Workflow Steps
Check circuit breaker state
Before each search call, check if the circuit breaker is open (tripped). If open and cooldown has not elapsed, route directly to the fallback provider.
Execute primary search
Send the query to the primary search API (Scavio). Track response time and HTTP status code.
Record outcome
On success with acceptable latency, clear the failure counter. On error or timeout, increment the failure counter.
Trip circuit breaker if threshold reached
After 3 consecutive failures, trip the circuit breaker and log an alert. Set the cooldown timer.
Route to fallback
When the circuit is open, send queries to the backup search provider. Normalize the response to match the primary format.
Health check and reset
After cooldown elapses, send a canary query to the primary. If it succeeds, close the circuit and resume primary routing.
Python Implementation
import requests, os, time
H = {"x-api-key": os.environ["SCAVIO_API_KEY"]}
failures = []
circuit_open = False
circuit_opened_at = 0
COOLDOWN = 60
def search(query, platform="google"):
global circuit_open, circuit_opened_at
if circuit_open and time.time() - circuit_opened_at < COOLDOWN:
return fallback_search(query, platform)
if circuit_open:
circuit_open = False
failures.clear()
try:
r = requests.post("https://api.scavio.dev/api/v1/search", headers=H,
json={"platform": platform, "query": query}, timeout=10)
r.raise_for_status()
failures.clear()
return r.json()
except Exception:
failures.append(time.time())
if len(failures) >= 3:
circuit_open = True
circuit_opened_at = time.time()
return fallback_search(query, platform)
raise
def fallback_search(query, platform):
return {"source": "fallback", "query": query, "results": []}JavaScript Implementation
const H = {"x-api-key": process.env.SCAVIO_API_KEY, "Content-Type": "application/json"};
let failures = 0;
let circuitOpen = false;
let circuitAt = 0;
async function search(query, platform = "google") {
if (circuitOpen && Date.now() - circuitAt < 60000) {
return {source: "fallback", query, results: []};
}
if (circuitOpen) { circuitOpen = false; failures = 0; }
try {
const r = await fetch("https://api.scavio.dev/api/v1/search", {
method: "POST", headers: H,
body: JSON.stringify({platform, query})
});
if (!r.ok) throw new Error("HTTP " + r.status);
failures = 0;
return r.json();
} catch (e) {
if (++failures >= 3) { circuitOpen = true; circuitAt = Date.now(); }
throw e;
}
}Platforms Used
Web search with knowledge graph, PAA, and AI overviews
Amazon
Product search with prices, ratings, and reviews
YouTube
Video search with transcripts and metadata
Community, posts & threaded comments from any subreddit