|
| 1 | +-- Copyright (C) Yichun Zhang (agentzh) |
| 2 | +-- |
| 3 | +-- Author: Thibault Charbonnier (thibaultcha) |
| 4 | + |
| 5 | + |
| 6 | +local base = require "resty.core.base" |
| 7 | +base.allows_subsystem("http", "stream") |
| 8 | + |
| 9 | + |
| 10 | +local ffi = require "ffi" |
| 11 | +local C = ffi.C |
| 12 | +--local FFI_OK = base.FFI_OK |
| 13 | +local FFI_ERROR = base.FFI_ERROR |
| 14 | +local FFI_DECLINED = base.FFI_DECLINED |
| 15 | +local ffi_str = ffi.string |
| 16 | +local get_string_buf = base.get_string_buf |
| 17 | +local get_size_ptr = base.get_size_ptr |
| 18 | +local find = string.find |
| 19 | +local type = type |
| 20 | + |
| 21 | + |
| 22 | +local ERR_BUF_SIZE = 128 |
| 23 | + |
| 24 | + |
| 25 | +ffi.cdef [[ |
| 26 | + unsigned int ngx_http_lua_ffi_is_configure_phase(); |
| 27 | + |
| 28 | + int ngx_http_lua_ffi_configure_shared_dict(ngx_str_t *name, |
| 29 | + ngx_str_t *size, unsigned char *errstr, size_t *errlen); |
| 30 | + |
| 31 | + void ngx_http_lua_ffi_configure_max_pending_timers(int n_timers); |
| 32 | + |
| 33 | + void ngx_http_lua_ffi_configure_max_running_timers(int n_timers); |
| 34 | + |
| 35 | + int ngx_http_lua_ffi_configure_env(const unsigned char *value, |
| 36 | + size_t name_len, size_t len); |
| 37 | +]] |
| 38 | + |
| 39 | + |
| 40 | +local _M = { version = base.version } |
| 41 | + |
| 42 | + |
| 43 | +function _M.is_configure_phase() |
| 44 | + return C.ngx_http_lua_ffi_is_configure_phase() == 1 |
| 45 | +end |
| 46 | + |
| 47 | + |
| 48 | +do |
| 49 | + local name_str_t = ffi.new("ngx_str_t[1]") |
| 50 | + local size_str_t = ffi.new("ngx_str_t[1]") |
| 51 | + |
| 52 | + |
| 53 | + function _M.shared_dict(name, size) |
| 54 | + if not _M.is_configure_phase() then |
| 55 | + error("API disabled in the current context", 2) |
| 56 | + end |
| 57 | + |
| 58 | + if type(name) ~= "string" then |
| 59 | + error("name must be a string", 2) |
| 60 | + end |
| 61 | + |
| 62 | + if type(size) ~= "string" then |
| 63 | + error("size must be a string", 2) |
| 64 | + end |
| 65 | + |
| 66 | + local name_len = #name |
| 67 | + if name_len == 0 then |
| 68 | + error("invalid lua shared dict name", 2) |
| 69 | + end |
| 70 | + |
| 71 | + local size_len = #size |
| 72 | + if size_len == 0 then |
| 73 | + error("invalid lua shared dict size", 2) |
| 74 | + end |
| 75 | + |
| 76 | + local name_t = name_str_t[0] |
| 77 | + local size_t = size_str_t[0] |
| 78 | + |
| 79 | + name_t.data = name |
| 80 | + name_t.len = name_len |
| 81 | + |
| 82 | + size_t.data = size |
| 83 | + size_t.len = size_len |
| 84 | + |
| 85 | + local err = get_string_buf(ERR_BUF_SIZE) |
| 86 | + local errlen = get_size_ptr() |
| 87 | + errlen[0] = ERR_BUF_SIZE |
| 88 | + |
| 89 | + local rc = C.ngx_http_lua_ffi_configure_shared_dict(name_str_t, |
| 90 | + size_str_t, err, |
| 91 | + errlen) |
| 92 | + if rc == FFI_ERROR then |
| 93 | + error(ffi_str(err, errlen[0])) |
| 94 | + end |
| 95 | + |
| 96 | + if rc == FFI_DECLINED then |
| 97 | + return nil, "exists" |
| 98 | + end |
| 99 | + |
| 100 | + -- NGINX_OK/FFI_OK |
| 101 | + |
| 102 | + return true |
| 103 | + end |
| 104 | +end |
| 105 | + |
| 106 | + |
| 107 | +function _M.max_pending_timers(n_timers) |
| 108 | + if not _M.is_configure_phase() then |
| 109 | + error("API disabled in the current context", 2) |
| 110 | + end |
| 111 | + |
| 112 | + if type(n_timers) ~= "number" then |
| 113 | + error("n_timers must be a number", 2) |
| 114 | + end |
| 115 | + |
| 116 | + if n_timers < 0 then |
| 117 | + error("n_timers must be positive", 2) |
| 118 | + end |
| 119 | + |
| 120 | + C.ngx_http_lua_ffi_configure_max_pending_timers(n_timers) |
| 121 | +end |
| 122 | + |
| 123 | + |
| 124 | +function _M.max_running_timers(n_timers) |
| 125 | + if not _M.is_configure_phase() then |
| 126 | + error("API disabled in the current context", 2) |
| 127 | + end |
| 128 | + |
| 129 | + if type(n_timers) ~= "number" then |
| 130 | + error("n_timers must be a number", 2) |
| 131 | + end |
| 132 | + |
| 133 | + if n_timers < 0 then |
| 134 | + error("n_timers must be positive", 2) |
| 135 | + end |
| 136 | + |
| 137 | + C.ngx_http_lua_ffi_configure_max_running_timers(n_timers) |
| 138 | +end |
| 139 | + |
| 140 | + |
| 141 | +function _M.env(value) |
| 142 | + if not _M.is_configure_phase() then |
| 143 | + error("API disabled in the current context", 2) |
| 144 | + end |
| 145 | + |
| 146 | + if type(value) ~= "string" then |
| 147 | + error("value must be a string", 2) |
| 148 | + end |
| 149 | + |
| 150 | + local len = #value |
| 151 | + local idx = find(value, "=") |
| 152 | + |
| 153 | + local rc = C.ngx_http_lua_ffi_configure_env(value, idx and idx - 1 or len, |
| 154 | + len) |
| 155 | + if rc == FFI_ERROR then |
| 156 | + error("no memory") |
| 157 | + end |
| 158 | +end |
| 159 | + |
| 160 | + |
| 161 | +return _M |
0 commit comments