|
| 1 | +diff --git lib/ngx/balancer.lua lib/ngx/balancer.lua |
| 2 | +index 18bdc2c..3a98f53 100644 |
| 3 | +--- lib/ngx/balancer.lua |
| 4 | ++++ lib/ngx/balancer.lua |
| 5 | +@@ -3,7 +3,7 @@ |
| 6 | + |
| 7 | + local base = require "resty.core.base" |
| 8 | + base.allows_subsystem('http', 'stream') |
| 9 | +- |
| 10 | ++require "resty.core.hash" |
| 11 | + |
| 12 | + local ffi = require "ffi" |
| 13 | + local C = ffi.C |
| 14 | +@@ -20,6 +20,7 @@ local error = error |
| 15 | + local type = type |
| 16 | + local tonumber = tonumber |
| 17 | + local max = math.max |
| 18 | ++local ngx_crc32_long = ngx.crc32_long |
| 19 | + |
| 20 | + local subsystem = ngx.config.subsystem |
| 21 | + local ngx_lua_ffi_balancer_set_current_peer |
| 22 | +@@ -35,8 +36,7 @@ if subsystem == 'http' then |
| 23 | + ffi.cdef[[ |
| 24 | + int ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r, |
| 25 | + const unsigned char *addr, size_t addr_len, int port, |
| 26 | +- const unsigned char *host, ssize_t host_len, |
| 27 | +- char **err); |
| 28 | ++ unsigned int cpool_crc32, unsigned int cpool_size, char **err); |
| 29 | + |
| 30 | + int ngx_http_lua_ffi_balancer_enable_keepalive(ngx_http_request_t *r, |
| 31 | + unsigned long timeout, unsigned int max_requests, char **err); |
| 32 | +@@ -130,6 +130,7 @@ else |
| 33 | + error("unknown subsystem: " .. subsystem) |
| 34 | + end |
| 35 | + |
| 36 | ++local DEFAULT_KEEPALIVE_POOL_SIZE = 30 |
| 37 | + local DEFAULT_KEEPALIVE_IDLE_TIMEOUT = 60000 |
| 38 | + local DEFAULT_KEEPALIVE_MAX_REQUESTS = 100 |
| 39 | + |
| 40 | +@@ -143,27 +144,61 @@ local peer_state_names = { |
| 41 | + local _M = { version = base.version } |
| 42 | + |
| 43 | + if subsystem == "http" then |
| 44 | +- function _M.set_current_peer(addr, port, host) |
| 45 | ++ function _M.set_current_peer(addr, port, opts) |
| 46 | + local r = get_request() |
| 47 | + if not r then |
| 48 | + error("no request found") |
| 49 | + end |
| 50 | + |
| 51 | ++ local pool_crc32 |
| 52 | ++ local pool_size |
| 53 | ++ if opts then |
| 54 | ++ if type(opts) ~= "table" then |
| 55 | ++ error("bad argument #3 to 'set_current_peer' " .. |
| 56 | ++ "(table expected, got " .. type(opts) .. ")", 2) |
| 57 | ++ end |
| 58 | ++ |
| 59 | ++ local pool = opts.pool |
| 60 | ++ pool_size = opts.pool_size |
| 61 | ++ |
| 62 | ++ if pool then |
| 63 | ++ if type(pool) ~= "string" then |
| 64 | ++ error("bad option 'pool' to 'set_current_peer' " .. |
| 65 | ++ "(string expected, got " .. type(pool) .. ")", 2) |
| 66 | ++ end |
| 67 | ++ |
| 68 | ++ pool_crc32 = ngx_crc32_long(pool) |
| 69 | ++ end |
| 70 | ++ |
| 71 | ++ if pool_size then |
| 72 | ++ if type(pool_size) ~= "number" then |
| 73 | ++ error("bad option 'pool_size' to 'set_current_peer' " .. |
| 74 | ++ "(number expected, got " .. type(pool_size) .. ")", 2) |
| 75 | ++ |
| 76 | ++ elseif pool_size < 1 then |
| 77 | ++ error("bad option 'pool_size' to 'set_current_peer' " .. |
| 78 | ++ "(expected > 0)", 2) |
| 79 | ++ end |
| 80 | ++ end |
| 81 | ++ end |
| 82 | ++ |
| 83 | + if not port then |
| 84 | + port = 0 |
| 85 | ++ |
| 86 | + elseif type(port) ~= "number" then |
| 87 | + port = tonumber(port) |
| 88 | + end |
| 89 | + |
| 90 | +- if host ~= nil and type(host) ~= "string" then |
| 91 | +- error("bad argument #3 to 'set_current_peer' " |
| 92 | +- .. "(string expected, got " .. type(host) .. ")") |
| 93 | ++ if not pool_crc32 then |
| 94 | ++ pool_crc32 = 0 |
| 95 | + end |
| 96 | + |
| 97 | +- local rc = ngx_lua_ffi_balancer_set_current_peer(r, addr, #addr, |
| 98 | +- port, |
| 99 | +- host, |
| 100 | +- host and #host or 0, |
| 101 | ++ if not pool_size then |
| 102 | ++ pool_size = DEFAULT_KEEPALIVE_POOL_SIZE |
| 103 | ++ end |
| 104 | ++ |
| 105 | ++ local rc = ngx_lua_ffi_balancer_set_current_peer(r, addr, #addr, port, |
| 106 | ++ pool_crc32, pool_size, |
| 107 | + errmsg) |
| 108 | + if rc == FFI_OK then |
| 109 | + return true |
| 110 | +@@ -172,26 +207,26 @@ if subsystem == "http" then |
| 111 | + return nil, ffi_str(errmsg[0]) |
| 112 | + end |
| 113 | + else |
| 114 | +- function _M.set_current_peer(addr, port, host) |
| 115 | ++ function _M.set_current_peer(addr, port, opts) |
| 116 | + local r = get_request() |
| 117 | + if not r then |
| 118 | + error("no request found") |
| 119 | + end |
| 120 | + |
| 121 | ++ if opts then |
| 122 | ++ error("bad argument #3 to 'set_current_peer' ('opts' not yet " .. |
| 123 | ++ "implemented in " .. subsystem .. " subsystem)", 2) |
| 124 | ++ end |
| 125 | ++ |
| 126 | + if not port then |
| 127 | + port = 0 |
| 128 | ++ |
| 129 | + elseif type(port) ~= "number" then |
| 130 | + port = tonumber(port) |
| 131 | + end |
| 132 | + |
| 133 | +- if host ~= nil then |
| 134 | +- error("bad argument #3 to 'set_current_peer' ('host' not yet " .. |
| 135 | +- "implemented in " .. subsystem .. " subsystem)", 2) |
| 136 | +- end |
| 137 | +- |
| 138 | + local rc = ngx_lua_ffi_balancer_set_current_peer(r, addr, #addr, |
| 139 | +- port, |
| 140 | +- errmsg) |
| 141 | ++ port, errmsg) |
| 142 | + if rc == FFI_OK then |
| 143 | + return true |
| 144 | + end |
0 commit comments