Skip to content

Instantly share code, notes, and snippets.

@kindy
Created January 10, 2013 13:46
Show Gist options
  • Save kindy/4502137 to your computer and use it in GitHub Desktop.
Save kindy/4502137 to your computer and use it in GitHub Desktop.
Use proxy_read_timeout to simulate task queue in ngx_openresty
server {
listen 9999;
resolver 8.8.8.8;
location = /add-q {
content_by_lua "
ngx.location.capture('/i-add-q', {
args = {
uri = '/run-q'
}
})
ngx.log(ngx.WARN, 'task created')
ngx.say(':)')
";
}
location = /i-add-q {
proxy_read_timeout 20ms;
set_unescape_uri $_uri $arg_uri;
proxy_pass http://127.0.0.1:$server_port$_uri;
}
location = /run-q {
content_by_lua "
-- slow actions
local res = ngx.location.capture('/fetch-any', {
args = {
srv = 'www.baidu.com',
host = 'www.baidu.com',
uri = '/',
}
})
ngx.log(ngx.WARN, 'got resp', res.body)
";
}
location = /fetch-any {
set_unescape_uri $_srv $arg_srv;
set_unescape_uri $_host $arg_host;
set_if_empty $_host localhost;
set_unescape_uri $_uri $arg_uri;
proxy_set_header Host $_host;
proxy_pass http://$_srv$_uri;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment