]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua: allow socket api settimeout to accept integers, float, and doubles
authorMark Lakes <mlakes@signalsciences.com>
Tue, 27 Mar 2018 07:48:06 +0000 (09:48 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 27 Mar 2018 12:17:02 +0000 (14:17 +0200)
Instead of hlua_socket_settimeout() accepting only integers, allow user
to specify float and double as well. Convert to milliseconds much like
cli_parse_set_timeout but also sanity check the value.

http://w3.impa.br/~diego/software/luasocket/tcp.html#settimeout

T. Fournier edit:

The main goal is to keep compatibility with the LuaSocket API. This
API only accept seconds, so using a float to specify milliseconds is
an acceptable way.

Update doc.

doc/lua-api/index.rst
src/hlua.c

index e7aa425d95b8cf210ea21c8070c4c0b5ce6f9059..a83bbde0145121b032aa1487ee141a34be63c214 100644 (file)
@@ -1906,12 +1906,13 @@ Socket class
 
   The amount of time to wait is specified as the value parameter, in seconds.
 
-  The timeout modes are bot implemented, the only settable timeout is the
+  The timeout modes are not implemented, the only settable timeout is the
   inactivity time waiting for complete the internal buffer send or waiting for
   receive data.
 
   :param class_socket socket: Is the manipulated Socket.
-  :param integer value: The timeout value.
+  :param float value: The timeout value. Use flotting point to specify
+    milliseconds.
 
 .. _regex_class:
 
index 633841c6d7aecce3e5cf893e01b5eed1cfe64e0f..60cf8f948437677b72adabc50602ca99d80349bf 100644 (file)
@@ -11,6 +11,7 @@
  */
 
 #include <ctype.h>
+#include <limits.h>
 #include <setjmp.h>
 
 #include <lauxlib.h>
@@ -2464,6 +2465,7 @@ __LJMP static int hlua_socket_settimeout(struct lua_State *L)
 {
        struct hlua_socket *socket;
        int tmout;
+       double dtmout;
        struct xref *peer;
        struct appctx *appctx;
        struct stream_interface *si;
@@ -2472,19 +2474,26 @@ __LJMP static int hlua_socket_settimeout(struct lua_State *L)
        MAY_LJMP(check_args(L, 2, "settimeout"));
 
        socket = MAY_LJMP(hlua_checksocket(L, 1));
-       tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000;
+
+       /* round up for inputs that are fractions and convert to millis */
+       dtmout = (0.5 + MAY_LJMP(luaL_checknumber(L, 2))) * 1000;
 
        /* Check for negative values */
-       if (tmout < 0)
+       if (dtmout < 0)
                WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
 
+       if (dtmout > INT_MAX) /* overflow check */
+               WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d", INT_MAX));
+
+       tmout = MS_TO_TICKS((int)dtmout);
+
        /* Check if we run on the same thread than the xreator thread.
         * We cannot access to the socket if the thread is different.
         */
        if (socket->tid != tid)
                WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
 
-       /* check for connection break. If some data where read, return it. */
+       /* check for connection break. If some data were read, return it. */
        peer = xref_get_peer_and_lock(&socket->xref);
        if (!peer) {
                hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");