Skip to content

Commit 714d9f5

Browse files
committed
make timeout not always an error
1 parent 7e4d967 commit 714d9f5

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ local res, err = prog('some-program')
102102

103103
```
104104

105+
### Treat timeouts as non-errors
106+
107+
By default, `sockexec` treats a timeout as an error. You can disable this by
108+
setting the object's `timeout_fatal` key to false. Examples:
109+
110+
```lua
111+
-- set timeout_fatal = false on the prog objects
112+
prog.timeout_fatal = false
113+
114+
-- or, set it at calltime:
115+
local res, err = prog({argv = {'cat'}, timeout_fatal = false})
116+
```
117+
105118
### But I actually want a shell!
106119

107120
Not a problem! You can just do something like:

dist.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = lua-resty-exec
2-
version = 1.1.2
2+
version = 1.1.3
33
abstract = Run external programs in OpenResty without spawning a shell or blocking
44
author = John Regan
55
is_original = yes

lib/resty/exec.lua

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ end
1818

1919

2020
local _M = {
21-
_VERSION = '1.1.2'
21+
_VERSION = '1.1.3'
2222
}
2323

2424
function _M.new(address)
@@ -32,7 +32,8 @@ function _M.new(address)
3232
stdin = nil,
3333
stdout = nil,
3434
stderr = nil,
35-
bufsize = 4096
35+
bufsize = 4096,
36+
timeout_fatal = true,
3637
}
3738

3839
function o.exec(self,...)
@@ -52,6 +53,7 @@ function _M.new(address)
5253
end
5354
end
5455
if args[1].stdin then self.stdin = args[1].stdin end
56+
if args[1].timeout_fatal then self.timeout_fatal = args[1].timeout_fatal end
5557
else
5658
self.argv = args
5759
end
@@ -80,8 +82,20 @@ function _M.new(address)
8082

8183
while(not err) do
8284
data, err, partial = c:receive(self.bufsize)
83-
if err and err ~= "closed" then
84-
return nil, err
85+
if err then
86+
if err == 'timeout' then
87+
if self.timeout_fatal then
88+
return nil, err
89+
else
90+
err = nil
91+
end
92+
else
93+
if err ~= 'timeout' and err ~= 'closed' then
94+
return nil, err
95+
else
96+
err = nil
97+
end
98+
end
8599
end
86100

87101
if data then
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package = "lua-resty-exec"
2+
version = "1.1.3-0"
3+
source = {
4+
url = "https://github.com/jprjr/lua-resty-exec/archive/1.1.3.tar.gz",
5+
file = "lua-resty-exec-1.1.3.tar.gz"
6+
}
7+
description = {
8+
summary = "Run external programs in OpenResty without spawning a shell",
9+
homepage = "https://github.com/jprjr/lua-resty-exec",
10+
license = "MIT"
11+
}
12+
build = {
13+
type = "builtin",
14+
modules = {
15+
["resty.exec"] = "lib/resty/exec.lua"
16+
}
17+
}
18+
dependencies = {
19+
"lua >= 5.1",
20+
"netstring >= 1.0.2"
21+
}

0 commit comments

Comments
 (0)