lua http套接字超时 [英] lua http socket timeout

查看:125
本文介绍了lua http套接字超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LuaSocket HTTP模块文档说可以在HTTP连接上设置超时:

The LuaSocket HTTP module documentation says that a timeout can be set on a HTTP connection:

可以设置以下常量来控制HTTP模块的默认行为:

The following constants can be set to control the default behavior of the HTTP module:

PORT:用于连接的默认端口. PROXY:用于连接的默认代理; 超时:设置所有I/O操作的超时; USERAGENT:向服务器报告的默认用户代理. http://w3.impa.br/~diego/software/luasocket/http .htm

PORT: default port used for connections; PROXY: default proxy used for connections; TIMEOUT: sets the timeout for all I/O operations; USERAGENT: default user agent reported to server. http://w3.impa.br/~diego/software/luasocket/http.htm

如何在lua脚本中设置这些常量?

How do I set these constants in a lua script?

推荐答案

您可以执行以下操作为一个请求而不是整个HTTP模块设置超时:

You can do this to set a timeout for one request instead of the entire HTTP module:

local socket = require "socket"
local http = require "socket.http"
response = http.request{url=URL, create=function()
  local req_sock = socket.tcp()
  req_sock:settimeout(5)
  return req_sock
end}


请注意,:settimeout的默认行为以及http.TIMEOUT之类的全局设置为请求中的任何单个操作设置了时间限制 -换句话说,即在超时之前,操作可能会不进行任何活动.如果您希望在操作上设置总体上限-整个请求不能超过的时间,无论活动如何-您都应该将模式参数传递为't'作为:settimeout的第二个参数,如下所示:


Note that the default behavior of :settimeout, as well as global settings like http.TIMEOUT, sets a time limit for any individual operation within the request - in other words, it's how long the operation may go without any activity before timing out. If you wish to set an overall upper bound on an operation - a time that the overall request can't exceed, regardless of activity - you should pass a mode argument of 't' as the second parameter to :settimeout, like so:

local socket = require "socket"
local http = require "socket.http"
response = http.request{url=URL, create=function()
  local req_sock = socket.tcp()
  -- note the second parameter here
  req_sock:settimeout(5, 't')
  return req_sock
end}

作为说明这两种模式之间区别的示例,假设在发出请求后,服务器每秒响应一次响应块,总共花费了七秒钟.在req_sock:settimeout(5, 'b')(或只是req_sock:settimeout(5))设置5秒的 block 超时的情况下,此请求将进行得很好,因为底层的I/O操作都不用超过五秒:但是,通过req_sock:settimeout(5, 't')设置五秒的超时,请求将在五秒后失败.

As an example to illustrate the distinction between the two modes, imagine that, after making your request, the server responded with a chunk of the response once a second, taking seven seconds overall to complete. With req_sock:settimeout(5, 'b') (or just req_sock:settimeout(5)) setting a 5-second block timeout, this request would proceed just fine, as none of the underlying I/O operations took longer than five seconds: however, with req_sock:settimeout(5, 't') setting a five-second total timeout, the request would fail after five seconds.

当然,为这些持续时间设置 的限制可能很有意义,因为 inactivity 超时时间较短,而总体时间较长 em>超时.因此,根据文档 ,您可以创建两个分别指定两个 :

Of course, it may make sense to set restrictions for both of these durations, having both a short inactivity timeout as well as a longer overall timeout. As such, per the documentation, you can make two separate calls to specify both:

local socket = require "socket"
local http = require "socket.http"
response = http.request{url=URL, create=function()
  local req_sock = socket.tcp()
  req_sock:settimeout(5, 'b')
  req_sock:settimeout(30, 't')
  return req_sock
end}

这篇关于lua http套接字超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆