function do_global_read_request()

in pluginats/connect_redis.lua [101:215]


function do_global_read_request()
  ts.debug("In do_global_read_request()==========")
  local response = client:ping()

  
  if not response then
    ts.debug("In 'not response: '", response)
    return 0
  end

  
  local req_scheme = ts.client_request.get_url_scheme() or 'http'
  local req_host = ts.client_request.get_url_host() or ''
  local req_path = ts.client_request.get_uri() or ''
  local req_port = ts.client_request.get_url_port() or ''

  local wildcard_req_host = get_wildcard_domain(req_host)
  ts.debug("-----Request-----")
  ts.debug("req_scheme: "..req_scheme)
  ts.debug("req_host: " .. req_host)
  ts.debug("req_port: " .. req_port)
  ts.debug("req_path: " .. req_path)
  ts.debug("wildcard_req_host: " .. (wildcard_req_host or 'invalid domain name'))
  ts.debug("-----------------")

  
  local svcs = check_path_exact_match(req_scheme, req_host, req_path)

  if (svcs == nil or #svcs == 0) then
    
    svcs = check_path_prefix_match(req_scheme, req_host, req_path)
  end

  if (svcs == nil or #svcs == 0) and wildcard_req_host ~= nil then
    
    svcs = check_path_exact_match(req_scheme, wildcard_req_host, req_path)
  end

  if (svcs == nil or #svcs == 0) and wildcard_req_host ~= nil then
    
    svcs = check_path_prefix_match(req_scheme, wildcard_req_host, req_path)
  end

  if (svcs == nil or #svcs == 0) then
    
    svcs = check_path_exact_match(req_scheme, '*', req_path)
  end

  if (svcs == nil or #svcs == 0) then
    
    svcs = check_path_prefix_match(req_scheme, '*', req_path)
  end

  if (svcs == nil or #svcs == 0) then
    ts.error("Redis Lookup Failure: svcs == nil for hostpath")
    return 0
  end

  for _, svc in ipairs(svcs) do
    if svc == nil then
      ts.error("Redis Lookup Failure: svc == nil for hostpath")
      return 0
    end
    if string.sub(svc, 1, 1) ~= "$" then
      ts.debug("routing")
      client:select(0) 
      local ipport = client:srandmember(svc) 
      
      if ipport == nil then
        ts.error("Redis Lookup Failure: ipport == nil for svc")
        return 0
      end

      
      local values = ipport_split(ipport, '#');
      if #values ~= 3 then
        ts.error("Redis Lookup Failure: wrong format - "..ipport)
        return 0
      end

      ts.http.skip_remapping_set(1)
      ts.client_request.set_url_scheme(values[3])
      ts.client_request.set_uri(req_path)
      ts.client_request.set_url_host(values[1])
      ts.client_request.set_url_port(values[2])
    end
  end

  for _, svc in ipairs(svcs) do
    if svc == nil then
      ts.error("Redis Lookup Failure: svc == nil for hostpath")
      return 0
    end
    if string.sub(svc, 1, 1) == "$" then
      ts.debug("snippet")
      client:select(1)
      local snippets = client:smembers(svc)

      if snippets == nil then
        ts.error("Redis Lookup Failure: snippets == nil for hostpath")
        return 0
      end

      local snippet = snippets[1]
      if snippet == nil then
        ts.error("Redis Lookup Failure: snippet == nil for hostpath")
        return 0
      end

      local f = loadstring(snippet)
      f()
    end
  end

end