Files

203 lines
5.7 KiB
Lua
Raw Permalink Normal View History

local json = require("cjson")
2025-08-18 16:25:27 +04:00
local http = require("resty.http")
2025-09-02 13:25:54 +04:00
local cache_diagnostics = require("utils.cache_diagnostics")
local _M = {}
2025-08-18 16:25:27 +04:00
-- Cache service configuration
local cache_socket_path = os.getenv("GO_CACHE_SOCKET") or "/tmp/cache.sock"
2025-08-18 16:25:27 +04:00
-- Helper function to make request to cache service via Unix socket
local function make_cache_request(endpoint, method, data)
local httpc = http.new()
httpc:set_timeout(5000) -- 5 second timeout
2025-09-02 13:25:54 +04:00
2025-08-18 16:25:27 +04:00
local body = nil
local headers = {
2025-09-02 13:25:54 +04:00
["Host"] = "cache-service",
["Content-Type"] = "application/json",
["Connection"] = "keep-alive"
2025-08-18 16:25:27 +04:00
}
if data then
2025-08-18 16:25:27 +04:00
body = json.encode(data)
end
2025-09-02 13:25:54 +04:00
-- Make request using the correct method
local ok, err = httpc:connect("unix:" .. cache_socket_path)
2025-08-18 16:25:27 +04:00
if not ok then
2025-09-02 13:25:54 +04:00
ngx.log(ngx.ERR, "[CACHE_UNIX] Failed to connect to socket: ", err)
return nil, err
2025-08-18 16:25:27 +04:00
end
2025-09-02 13:25:54 +04:00
2025-08-18 16:25:27 +04:00
local res, err = httpc:request({
method = method,
2025-09-02 13:25:54 +04:00
path = endpoint,
headers = headers,
body = body
2025-08-18 16:25:27 +04:00
})
if not res then
2025-09-02 13:25:54 +04:00
ngx.log(ngx.ERR, "[CACHE_UNIX] Failed to make request: ", err)
2025-08-18 16:25:27 +04:00
return nil, err
end
if res.status ~= 200 then
2025-09-02 13:25:54 +04:00
ngx.log(ngx.ERR, "[CACHE_UNIX] Cache service returned error status: ", res.status)
2025-08-18 16:25:27 +04:00
return nil, "HTTP " .. res.status
end
2025-09-02 13:25:54 +04:00
local response_body = res:read_body()
if not response_body then
ngx.log(ngx.ERR, "[CACHE_UNIX] Failed to read response body")
return nil, "Failed to read cache response"
end
-- Set keepalive for connection reuse
httpc:set_keepalive(60000, 10) -- 60s timeout, max 10 connections in pool
local ok, response_data = pcall(json.decode, response_body)
2025-08-18 16:25:27 +04:00
if not ok then
2025-09-02 13:25:54 +04:00
ngx.log(ngx.ERR, "[CACHE_UNIX] Failed to decode JSON response: ", response_data)
2025-08-18 16:25:27 +04:00
return nil, "Invalid JSON response"
end
return response_data, nil
end
2025-08-18 16:25:27 +04:00
-- Helper function to validate raw body
local function validate_body(body_data)
if not body_data or body_data == "" then
return false
end
2025-08-18 16:25:27 +04:00
-- Basic validation - should be JSON string
if type(body_data) ~= "string" then
return false
end
return true
end
2025-06-26 16:41:44 +04:00
2025-08-18 16:25:27 +04:00
-- Check cache for a request
2025-06-26 16:41:44 +04:00
function _M.check_cache(chain, network, body_data)
2025-08-18 16:25:27 +04:00
if not validate_body(body_data) then
ngx.log(ngx.WARN, "[CACHE_DEBUG] Invalid body data")
2025-06-26 16:41:44 +04:00
return {
cache_type = nil,
cache_key = nil,
ttl = nil,
2025-07-21 19:57:50 +04:00
cached_response = nil,
2025-09-02 13:25:54 +04:00
raw_body = nil,
chain = chain,
network = network
2025-06-26 16:41:44 +04:00
}
end
2025-08-18 16:25:27 +04:00
-- Prepare request for cache service with raw body
local cache_request = {
chain = chain,
network = network,
raw_body = body_data
}
2025-08-18 16:25:27 +04:00
-- Make GET request to cache service
local response, err = make_cache_request("/cache/get", "POST", cache_request)
if not response then
2025-09-02 13:25:54 +04:00
ngx.log(ngx.ERR, "[CACHE_GET] Failed to get from cache service: ", err)
2025-06-26 16:41:44 +04:00
return {
cache_type = nil,
cache_key = nil,
ttl = nil,
2025-07-21 19:57:50 +04:00
cached_response = nil,
2025-09-02 13:25:54 +04:00
raw_body = body_data,
chain = chain,
network = network
2025-06-26 16:41:44 +04:00
}
end
2025-08-18 16:25:27 +04:00
if not response.success then
2025-09-02 13:25:54 +04:00
ngx.log(ngx.ERR, "[CACHE_GET] Cache service returned error: ", response.error or "unknown")
return {
cache_type = nil,
cache_key = nil,
ttl = nil,
cached_response = nil,
2025-09-02 13:25:54 +04:00
raw_body = body_data,
chain = chain,
network = network
}
end
2025-08-18 16:25:27 +04:00
local cached_response = nil
2025-08-20 21:36:44 +04:00
local cache_status = response.cache_status or "MISS"
2025-09-02 13:25:54 +04:00
local cache_level = response.cache_level or "MISS"
2025-08-20 21:36:44 +04:00
2025-08-18 16:25:27 +04:00
if response.found and response.data then
cached_response = response.data
end
2025-06-26 16:41:44 +04:00
return {
2025-08-18 16:25:27 +04:00
cache_type = response.cache_type,
cache_key = response.key,
2025-09-02 13:25:54 +04:00
ttl = nil, -- cache will calculate ttl itself
2025-07-21 19:57:50 +04:00
cached_response = cached_response,
2025-08-18 16:25:27 +04:00
raw_body = body_data,
2025-08-20 21:36:44 +04:00
fresh = response.fresh,
2025-09-02 13:25:54 +04:00
cache_status = cache_status,
cache_level = cache_level,
chain = chain,
network = network
2025-06-26 16:41:44 +04:00
}
end
2025-08-18 16:25:27 +04:00
-- Save response to cache
2025-06-26 21:59:37 +04:00
function _M.save_to_cache(cache_info, response_body)
2025-08-18 16:25:27 +04:00
if not cache_info.cache_key or not cache_info.raw_body then
2025-09-02 13:25:54 +04:00
ngx.log(ngx.WARN, "[CACHE_SET] Missing cache info parameters, cannot save to cache")
return false
end
if not cache_info.cache_type then
2025-06-25 22:18:39 +04:00
return false
end
2025-08-18 16:25:27 +04:00
-- Prepare request for cache service
local cache_request = {
2025-09-02 13:25:54 +04:00
chain = cache_info.chain,
network = cache_info.network,
2025-08-18 16:25:27 +04:00
raw_body = cache_info.raw_body,
data = response_body
}
-- Add TTL if provided
if cache_info.ttl then
cache_request.ttl = cache_info.ttl
end
2025-08-18 16:25:27 +04:00
-- Make SET request to cache service
local response, err = make_cache_request("/cache/set", "POST", cache_request)
if not response then
2025-09-02 13:25:54 +04:00
ngx.log(ngx.ERR, "[CACHE_SET] Failed to save to cache service: ", err)
2025-08-18 16:25:27 +04:00
return false
end
2025-08-18 16:25:27 +04:00
if not response.success then
2025-09-02 13:25:54 +04:00
ngx.log(ngx.ERR, "[CACHE_SET] Cache service save failed: ", response.error or "unknown")
2025-08-18 16:25:27 +04:00
return false
end
return true
end
2025-08-18 16:25:27 +04:00
-- Function to reset cache instances (for testing compatibility)
function _M.reset_cache_instances()
-- No-op for HTTP-based cache
ngx.log(ngx.INFO, "[CACHE_DEBUG] reset_cache_instances called (no-op for HTTP cache)")
end
2025-09-02 13:25:54 +04:00
-- Function to run cache diagnostics
function _M.run_diagnostics()
cache_diagnostics.test_cache_connectivity()
end
2025-08-18 16:25:27 +04:00
return _M