From: Vsevolod Stakhov Date: Sat, 17 Sep 2016 12:35:15 +0000 (+0100) Subject: [Minor] Allow to set timeout for a connection X-Git-Tag: 1.4.0~417 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c67b248bdfcb8117116022980a040cf477ff84a2;p=rspamd.git [Minor] Allow to set timeout for a connection --- diff --git a/src/lua/lua_tcp.c b/src/lua/lua_tcp.c index f40aadec1..2ce644a4d 100644 --- a/src/lua/lua_tcp.c +++ b/src/lua/lua_tcp.c @@ -56,6 +56,13 @@ LUA_FUNCTION_DEF (tcp, request); * Closes TCP connection */ LUA_FUNCTION_DEF (tcp, close); +/*** + * @method tcp:set_timeout(milliseconds) + * + * Sets new timeout for a TCP connection in **milliseconds** + * @param {number} milliseconds floating point value that specifies new timeout + */ +LUA_FUNCTION_DEF (tcp, set_timeout); LUA_FUNCTION_DEF (tcp, gc); static const struct luaL_reg tcp_libf[] = { @@ -65,6 +72,7 @@ static const struct luaL_reg tcp_libf[] = { static const struct luaL_reg tcp_libm[] = { LUA_INTERFACE_DEF (tcp, close), + LUA_INTERFACE_DEF (tcp, set_timeout), {"__gc", lua_tcp_gc}, {"__tostring", rspamd_lua_class_tostring}, {NULL, NULL} @@ -271,12 +279,6 @@ lua_tcp_write_helper (struct lua_tcp_cbdata *cbd) call_finish_handler: - if (!cbd->partial) { - cbd->in = g_byte_array_new (); - rspamd_mempool_add_destructor (cbd->pool, rspamd_gstring_free_hard, - cbd->in); - } - if (cbd->do_shutdown) { /* Half close the connection */ shutdown (cbd->fd, SHUT_WR); @@ -709,6 +711,10 @@ lua_tcp_request (lua_State *L) cbd->port = port; cbd->stop_pattern = stop_pattern; cbd->connect_cb = conn_cbref; + cbd->in = g_byte_array_new (); + rspamd_mempool_add_destructor (cbd->pool, + (rspamd_mempool_destruct_t)g_byte_array_unref, + cbd->in); REF_INIT_RETAIN (cbd, lua_tcp_maybe_free); if (session) { @@ -766,6 +772,22 @@ lua_tcp_close (lua_State *L) return 0; } +static gint +lua_tcp_set_timeout (lua_State *L) +{ + struct lua_tcp_cbdata *cbd = lua_check_tcp (L, 1); + gdouble ms = lua_tonumber (L, 2); + + if (cbd == NULL) { + return luaL_error (L, "invalid arguments"); + } + + ms *= 1000.0; + double_to_tv (ms, &cbd->tv); + + return 0; +} + static gint lua_tcp_gc (lua_State *L) {