File size: 2,877 Bytes
bde2f3a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | """
Aggressive Caching Shield — Multi-Layer API Protection for Free RPC Tiers
Protects free tier RPC API keys (Helius, QuickNode, Alchemy) from
exhaustion by frontend traffic. Enforces cache-first architecture:
1. RpcCacheClient — Redis L2 + in-memory L1 cache with TTL tiers
2. RpcRateLimiter — Token bucket rate limiting per provider
3. RpcBatcher — JSON-RPC batch request grouper (reduces call count)
4. HistoryDepthController — Caps default query depth, gates deep scans
5. WsClientManager — Connection-pooled Redis pub/sub for live streams
All modules fall back gracefully if Redis is unavailable.
Usage:
from app.caching_shield import (
get_rpc_cache,
get_rate_limiter,
get_ws_manager,
get_history_controller,
)
# Cache-first RPC query
cache = get_rpc_cache()
result = await cache.get_balance("SoL...")
# Rate-limited via token bucket
limiter = get_rate_limiter()
allowed, wait = await limiter.acquire("helius", "getBalance")
if not allowed:
raise HTTPException(429, f"Rate limited, retry in {wait:.1f}s")
# Broadcast to WebSocket stream
ws = get_ws_manager()
await ws.broadcast_scan({"token": "SoL...", "safety_score": 85})
# Clamp query depth
hdc = get_history_controller()
limit = hdc.clamp_limit(100, is_deep_scan=True)
"""
from app.caching_shield.api_registry import (
PROVIDER_REGISTRY,
ApiKey,
KeyPool,
ProviderConfig,
UnifiedApiManager,
get_api_manager,
)
from app.caching_shield.batcher import (
BATCH_WINDOW_MS,
MAX_BATCH_SIZE,
BatchRequest,
BatchResult,
RpcBatcher,
)
from app.caching_shield.funding_tracer import (
FundingTrace,
trace_funding_source,
)
from app.caching_shield.history_depth import (
DEFAULT_DEPTH,
MAX_DEPTH,
MAX_PAGINATED,
HistoryDepthController,
get_history_controller,
)
from app.caching_shield.rate_limiter import (
PROVIDER_LIMITS,
ProviderLimit,
RpcRateLimiter,
get_rate_limiter,
)
from app.caching_shield.rpc_cache import (
TTL_TABLE,
CacheStats,
RpcCacheClient,
get_rpc_cache,
)
from app.caching_shield.solana_tracker import (
SolanaTrackerClient,
get_solana_tracker,
)
from app.caching_shield.tool_data import (
ToolData,
td,
)
from app.caching_shield.unified_layer import (
ToolResult,
UnifiedDataLayer,
get_data_layer,
)
from app.caching_shield.ws_broadcaster import (
CHANNEL_ALERTS,
CHANNEL_PRICES,
CHANNEL_SCANS,
CHANNEL_TOKENS,
WsClientManager,
get_ws_manager,
)
__all__ = [
"PROVIDER_REGISTRY",
"ApiKey",
"FundingTrace",
"KeyPool",
"ProviderConfig",
"ToolData",
"ToolResult",
"UnifiedApiManager",
"UnifiedDataLayer",
"get_api_manager",
"get_data_layer",
"td",
"trace_funding_source",
]
|