Merge pull request #9 from blotus/handle-lua-request-timeout

Add REQUEST_TIMEOUT parameter
This commit is contained in:
Thibault "bui" Koechlin 2021-02-09 14:41:19 +01:00 committed by GitHub
commit 35bc3a4c21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

View file

@ -54,4 +54,5 @@ API_KEY= <-- the API Key generated with `cs
LOG_FILE=/tmp/lua_mod.log <-- path to log file
CACHE_EXPIRATION=1 <-- in seconds
CACHE_SIZE=1000 <-- cache size
REQUEST_TIMEOUT=0.2 <-- Maximum duration in seconds for a request to LAPI
```

View file

@ -4,6 +4,7 @@ local config = require "config"
local lrucache = require "lrucache"
local logging = require "logging"
local log_file = require "logging.file"
local socket = require "socket"
local http = require("socket.http")
local https = require("ssl.https")
@ -71,6 +72,11 @@ function csmod.allowIp(ip)
protocol = "tlsv1",
options = "all",
verify = "none",
create = function()
local req_sock = socket.tcp()
req_sock:settimeout(runtime.conf['REQUEST_TIMEOUT'], 't')
return req_sock
end
}
else
local body, code, headers = http.request{
@ -81,7 +87,12 @@ function csmod.allowIp(ip)
['User-Agent'] = runtime.userAgent
},
content_type = 'application/json',
sink = ltn12.sink.table(resp)
sink = ltn12.sink.table(resp),
create = function()
local req_sock = socket.tcp()
req_sock:settimeout(runtime.conf['REQUEST_TIMEOUT'], 't')
return req_sock
end
}
end

View file

@ -32,11 +32,14 @@ end
function config.loadConfig(file)
if not config.file_exists(file) then
return nil, "File".. file .." doesn't exist"
return nil, "File ".. file .." doesn't exist"
end
local conf = {}
local valid_params = {'API_URL', 'API_KEY', 'LOG_FILE'}
local valid_int_params = {'CACHE_EXPIRATION', 'CACHE_SIZE'}
local valid_int_params = {'CACHE_EXPIRATION', 'CACHE_SIZE', 'REQUEST_TIMEOUT'}
local default_values = {
['REQUEST_TIMEOUT'] = 0.2
}
for line in io.lines(file) do
local isOk = false
if starts_with(line, "#") then
@ -55,10 +58,16 @@ function config.loadConfig(file)
break
else
print("unsupported configuration '" .. v .. "'")
break
end
end
end
end
for k, v in pairs(default_values) do
if conf[k] == nil then
conf[k] = v
end
end
return conf, nil
end