add REQUEST_TIMEOUT config parameter

This commit is contained in:
Sebastien Blot 2021-02-08 17:21:13 +01:00
parent 71c4247d6b
commit 580fab7b02
2 changed files with 23 additions and 3 deletions

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
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