|
| 1 | +local _M = {} |
| 2 | +function _M.balance_ep() |
| 3 | + local configuration = require("configuration") |
| 4 | + local cjson = require("cjson.safe") |
| 5 | + local ngx_balancer = require("ngx.balancer") |
| 6 | + local data |
| 7 | + local random_endpoint |
| 8 | + local request_uri = ngx.var.request_uri |
| 9 | + local ver = request_uri:match("/([%d%.%-]+%-[^/]+)/") |
| 10 | + local wopisrc = ngx.var.arg_WOPISrc |
| 11 | + local shardkey = ngx.var.arg_shardkey |
| 12 | + |
| 13 | + local rr_live_dict = ngx.shared.rr_live_index |
| 14 | + local rr_reserved_dict = ngx.shared.rr_reserved_index |
| 15 | + |
| 16 | + local api_key |
| 17 | + |
| 18 | + if wopisrc then |
| 19 | + api_key = wopisrc |
| 20 | + end |
| 21 | + if shardkey then |
| 22 | + api_key = shardkey |
| 23 | + end |
| 24 | + |
| 25 | + repeat |
| 26 | + data = configuration.get_backends_data() |
| 27 | + print(data) |
| 28 | + local decoded_table = cjson.decode(data) |
| 29 | + print(tostring(decoded_table)) |
| 30 | + local address = decoded_table[1].address |
| 31 | + print(cjson.encode(address)) |
| 32 | + if address == "none" then |
| 33 | + ngx.sleep(1) |
| 34 | + print("No active shards found, waiting...") |
| 35 | + end |
| 36 | + until address ~= "none" |
| 37 | + local decoded_data = cjson.decode(data) |
| 38 | + local matching_addresses = {} |
| 39 | + |
| 40 | + if not api_key and not ver then |
| 41 | + for _, entry in ipairs(decoded_data) do |
| 42 | + table.insert(matching_addresses, entry.address .. ":" .. entry.port) |
| 43 | + end |
| 44 | + end |
| 45 | + if api_key then |
| 46 | + for _, entry in ipairs(decoded_data) do |
| 47 | + table.insert(matching_addresses, entry.address .. ":" .. entry.port) |
| 48 | + end |
| 49 | + else |
| 50 | + if ver then |
| 51 | + -- Iterate through the decoded table |
| 52 | + for _, entry in ipairs(decoded_data) do |
| 53 | + if entry.ver == ver then |
| 54 | + table.insert(matching_addresses, entry.address .. ":" .. entry.port) |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + if ver and next(matching_addresses) == nil then |
| 61 | + print(string.format("WARN: Can't find endpoint in live table. VER: %s", ver)) |
| 62 | + local reserved_data |
| 63 | + repeat |
| 64 | + reserved_data = configuration.get_reserved_data() |
| 65 | + print(string.format("RESERVED_DATA:%s", reserved_data)) |
| 66 | + local decoded_reserved_table = cjson.decode(reserved_data) |
| 67 | + print(tostring(decoded_reserved_table)) |
| 68 | + local address = decoded_reserved_table[1].address |
| 69 | + print(cjson.encode(address)) |
| 70 | + if address == "none" then |
| 71 | + ngx.sleep(1) |
| 72 | + print("No active shards found, waiting...") |
| 73 | + end |
| 74 | + until address ~= "none" |
| 75 | + local reserved_decoded_data = cjson.decode(reserved_data) |
| 76 | + local reserved_addresses = {} |
| 77 | + for _, entry in ipairs(reserved_decoded_data) do |
| 78 | + if entry.ver == ver then |
| 79 | + table.insert(reserved_addresses, entry.address .. ":" .. entry.port) |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + local idx = rr_reserved_dict:get("last_used_index") or 1 |
| 84 | + |
| 85 | + random_endpoint = reserved_addresses[idx] |
| 86 | + |
| 87 | + -- Update to the next index for the next request |
| 88 | + idx = idx + 1 |
| 89 | + if idx > #matching_addresses then |
| 90 | + idx = 1 |
| 91 | + end |
| 92 | + |
| 93 | + rr_reserved_dict:set("last_used_index", idx) |
| 94 | + else |
| 95 | + local idx = rr_live_dict:get("last_used_index") or 1 |
| 96 | + |
| 97 | + random_endpoint = matching_addresses[idx] |
| 98 | + |
| 99 | + -- Update to the next index for the next request |
| 100 | + idx = idx + 1 |
| 101 | + if idx > #matching_addresses then |
| 102 | + idx = 1 |
| 103 | + end |
| 104 | + |
| 105 | + rr_live_dict:set("last_used_index", idx) |
| 106 | + end |
| 107 | + |
| 108 | + if api_key then |
| 109 | + return random_endpoint |
| 110 | + else |
| 111 | + ngx_balancer.set_more_tries(1) |
| 112 | + |
| 113 | + local ok, err = ngx_balancer.set_current_peer(random_endpoint) |
| 114 | + if not ok then |
| 115 | + ngx.log(ngx.ERR, "error while setting current upstream peer ", peer, |
| 116 | + ": ", err) |
| 117 | + end |
| 118 | + end |
| 119 | +end |
| 120 | +return _M |
0 commit comments