|
| 1 | +-- |
| 2 | +-- Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +-- contributor license agreements. See the NOTICE file distributed with |
| 4 | +-- this work for additional information regarding copyright ownership. |
| 5 | +-- The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +-- (the "License"); you may not use this file except in compliance with |
| 7 | +-- the License. You may obtain a copy of the License at |
| 8 | +-- |
| 9 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +-- |
| 11 | +-- Unless required by applicable law or agreed to in writing, software |
| 12 | +-- distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +-- See the License for the specific language governing permissions and |
| 15 | +-- limitations under the License. |
| 16 | +-- |
| 17 | + |
| 18 | +--- Outbound HTTP client selection. |
| 19 | +-- Shared by any module that makes outbound HTTP calls, so the choice between |
| 20 | +-- `ngx_http_ffi_client` and `lua-resty-http` is made in one place rather than |
| 21 | +-- per plugin. The caller decides where the preference comes from (its own |
| 22 | +-- config key) and passes the name in. |
| 23 | + |
| 24 | +local core = require("apisix.core") |
| 25 | +local pcall = pcall |
| 26 | +local require = require |
| 27 | +local tostring = tostring |
| 28 | +local type = type |
| 29 | + |
| 30 | +local FFI_CLIENT = "ngx_http_ffi_client" |
| 31 | +local LUA_RESTY_HTTP = "lua-resty-http" |
| 32 | + |
| 33 | +-- the client name used in configuration is not the module name |
| 34 | +local CLIENT_MODULES = { |
| 35 | + [FFI_CLIENT] = "resty.ngx_http_ffi_client", |
| 36 | + [LUA_RESTY_HTTP] = "resty.http", |
| 37 | +} |
| 38 | + |
| 39 | +local loaded = {} |
| 40 | + |
| 41 | + |
| 42 | +local _M = { |
| 43 | + version = 0.1, |
| 44 | + FFI_CLIENT = FFI_CLIENT, |
| 45 | + LUA_RESTY_HTTP = LUA_RESTY_HTTP, |
| 46 | + DEFAULT_CLIENT = FFI_CLIENT, |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +--- Schema fragment for a client-name config field. |
| 51 | +-- Callers embed this in their own attribute schema so every module validates |
| 52 | +-- the name the same way. |
| 53 | +_M.client_schema = { |
| 54 | + type = "string", |
| 55 | + enum = {FFI_CLIENT, LUA_RESTY_HTTP}, |
| 56 | + default = FFI_CLIENT, |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +--- Load the module for a client name. |
| 61 | +-- `ngx_http_ffi_client` is a C client with the same object API as |
| 62 | +-- lua-resty-http and around half its outbound CPU cost, and it exists only |
| 63 | +-- when the gateway runtime was built with the module. A name that cannot be |
| 64 | +-- loaded is an error, never a silent switch to the other client. |
| 65 | +-- Cached per name once loaded, so a failure is retried rather than remembered. |
| 66 | +local function load_client(name) |
| 67 | + local cached = loaded[name] |
| 68 | + if cached then |
| 69 | + return cached |
| 70 | + end |
| 71 | + |
| 72 | + local module_name = CLIENT_MODULES[name] |
| 73 | + if not module_name then |
| 74 | + return nil, "unknown http client: " .. tostring(name) |
| 75 | + end |
| 76 | + |
| 77 | + local ok, mod = pcall(require, module_name) |
| 78 | + if not ok or type(mod) ~= "table" then |
| 79 | + core.log.error(module_name, " is not available: ", mod) |
| 80 | + return nil, module_name .. " is not available: " .. tostring(mod) |
| 81 | + end |
| 82 | + |
| 83 | + -- Cosockets have their names resolved by apisix/patch.lua, which routes |
| 84 | + -- them through core.resolver and so honours dns_resolver, /etc/hosts and |
| 85 | + -- the search domains. The C client dials from C and never touches a |
| 86 | + -- cosocket, so without this it would see only nginx's `resolver`. Handing |
| 87 | + -- it the same resolver keeps every outbound name on one set of rules. |
| 88 | + -- A client too old to take one is an error rather than a client quietly |
| 89 | + -- resolving names by different rules than the rest of the gateway. |
| 90 | + if name == FFI_CLIENT then |
| 91 | + if type(mod.set_resolver) ~= "function" then |
| 92 | + core.log.error(module_name, " does not support set_resolver, ", |
| 93 | + "the runtime is older than the pinned one") |
| 94 | + return nil, module_name .. " does not support set_resolver, " |
| 95 | + .. "the runtime is older than the pinned one" |
| 96 | + end |
| 97 | + |
| 98 | + mod.set_resolver(core.resolver.parse_domain) |
| 99 | + end |
| 100 | + |
| 101 | + loaded[name] = mod |
| 102 | + |
| 103 | + return mod |
| 104 | +end |
| 105 | + |
| 106 | + |
| 107 | +--- Create an HTTP client. |
| 108 | +-- @tparam string|nil name client name; defaults to DEFAULT_CLIENT |
| 109 | +-- @treturn table|nil the client |
| 110 | +-- @treturn string|nil error message |
| 111 | +function _M.new(name) |
| 112 | + name = name or _M.DEFAULT_CLIENT |
| 113 | + |
| 114 | + local mod, err = load_client(name) |
| 115 | + if not mod then |
| 116 | + return nil, err |
| 117 | + end |
| 118 | + |
| 119 | + -- The Lua half of `ngx_http_ffi_client` loads even when the C module is |
| 120 | + -- not compiled into the runtime; new() is what reports that. |
| 121 | + return mod.new() |
| 122 | +end |
| 123 | + |
| 124 | + |
| 125 | +return _M |
0 commit comments