Mercurial > prosody-modules
comparison mod_firewall/definitions.lib.lua @ 2522:72cbec103709
mod_firewall: Improve HTTP polling logic
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Mon, 20 Feb 2017 09:26:16 +0000 |
| parents | c6fd8975704b |
| children | a3a18d09ae8a |
comparison
equal
deleted
inserted
replaced
| 2521:66b81e144ded | 2522:72cbec103709 |
|---|---|
| 94 if opts.hash then | 94 if opts.hash then |
| 95 assert(opts.hash:match("^%w+$") and type(hashes[opts.hash]) == "function", "invalid hash function: "..opts.hash); | 95 assert(opts.hash:match("^%w+$") and type(hashes[opts.hash]) == "function", "invalid hash function: "..opts.hash); |
| 96 self.hash_function = hashes[opts.hash]; | 96 self.hash_function = hashes[opts.hash]; |
| 97 end | 97 end |
| 98 local etag; | 98 local etag; |
| 99 local failure_count = 0; | |
| 100 local retry_intervals = { 60, 120, 300 }; | |
| 99 local function update_list() | 101 local function update_list() |
| 100 http.request(url, { | 102 http.request(url, { |
| 101 headers = { | 103 headers = { |
| 102 ["If-None-Match"] = etag; | 104 ["If-None-Match"] = etag; |
| 103 }; | 105 }; |
| 104 }, function (body, code, response) | 106 }, function (body, code, response) |
| 107 local next_poll = poll_interval; | |
| 105 if code == 200 and body then | 108 if code == 200 and body then |
| 106 etag = response.headers.etag; | 109 etag = response.headers.etag; |
| 107 local items = {}; | 110 local items = {}; |
| 108 for entry in body:gmatch(pattern) do | 111 for entry in body:gmatch(pattern) do |
| 109 items[entry] = true; | 112 items[entry] = true; |
| 110 end | 113 end |
| 111 self.items = items; | 114 self.items = items; |
| 112 module:log("debug", "Fetched updated list from <%s>", url); | 115 module:log("debug", "Fetched updated list from <%s>", url); |
| 113 elseif code == 304 then | 116 elseif code == 304 then |
| 114 module:log("debug", "List at <%s> is unchanged", url); | 117 module:log("debug", "List at <%s> is unchanged", url); |
| 115 else | 118 elseif code == 0 or (code >= 400 and code <=599) then |
| 116 module:log("warn", "Failed to fetch list from <%s>: %d %s", url, code, tostring(body)); | 119 module:log("warn", "Failed to fetch list from <%s>: %d %s", url, code, tostring(body)); |
| 120 next_poll = 300; | |
| 121 failure_count = failure_count + 1; | |
| 122 next_poll = retry_intervals[failure_count] or retry_intervals[#retry_intervals]; | |
| 117 end | 123 end |
| 118 if poll_interval > 0 then | 124 if next_poll > 0 then |
| 119 timer.add_task(poll_interval, update_list); | 125 timer.add_task(next_poll+math.random(0, 60), update_list); |
| 120 end | 126 end |
| 121 end); | 127 end); |
| 122 end | 128 end |
| 123 update_list(); | 129 update_list(); |
| 124 timer.add_task(0, update_list); | |
| 125 end; | 130 end; |
| 126 contains = function (self, item) | 131 contains = function (self, item) |
| 127 if self.hash_function then | 132 if self.hash_function then |
| 128 item = self.hash_function(item); | 133 item = self.hash_function(item); |
| 129 end | 134 end |
