diff --git a/src/box/iproto.cc b/src/box/iproto.cc index 863eb4f066669669e7a8276ed7bc0af355e08354..c8b83b16bd8e866295d9841b39133d5c9117a02a 100644 --- a/src/box/iproto.cc +++ b/src/box/iproto.cc @@ -2121,6 +2121,12 @@ iproto_mem_used(void) return slab_cache_used(&net_cord.slabc) + slab_cache_used(&net_slabc); } +size_t +iproto_connection_count(void) +{ + return mempool_count(&iproto_connection_pool); +} + void iproto_reset_stat(void) { diff --git a/src/box/iproto.h b/src/box/iproto.h index 8f3607ffc53af274ff2a24542ae76bfa00c9d410..7916705ee28f4cd7e78401080a88044139e7e44c 100644 --- a/src/box/iproto.h +++ b/src/box/iproto.h @@ -62,6 +62,12 @@ extern unsigned iproto_readahead; size_t iproto_mem_used(void); +/** + * Return the number of active iproto connections. + */ +size_t +iproto_connection_count(void); + /** * Reset network statistics. */ diff --git a/src/box/lua/stat.c b/src/box/lua/stat.c index c00315cdd193816926b712ee38317b7b9c35dea8..1015253f4b73bb37f8d5167a63b47f759bbbdb00 100644 --- a/src/box/lua/stat.c +++ b/src/box/lua/stat.c @@ -140,6 +140,10 @@ static int lbox_stat_net_index(struct lua_State *L) { luaL_checkstring(L, -1); + if (strcmp("CONNECTIONS", lua_tostring(L, -1)) == 0) { + lua_pushnumber(L, iproto_connection_count()); + return 1; + } return rmean_foreach(rmean_net, seek_stat_item, L); } @@ -148,6 +152,11 @@ lbox_stat_net_call(struct lua_State *L) { lua_newtable(L); rmean_foreach(rmean_net, set_stat_item, L); + + lua_pushstring(L, "CONNECTIONS"); + lua_pushnumber(L, iproto_connection_count()); + lua_settable(L, -3); + return 1; } diff --git a/test/box/stat_net.result b/test/box/stat_net.result index b3e3db11f705fd37ec1e3b45ba05919cc1c19dce..639a830d80cedae0b9a35835398862861982eab6 100644 --- a/test/box/stat_net.result +++ b/test/box/stat_net.result @@ -34,6 +34,15 @@ LISTEN = require('uri').parse(box.cfg.listen) cn = remote.connect(LISTEN.host, LISTEN.service) --- ... +cn1 = remote.connect(LISTEN.host, LISTEN.service) +--- +... +cn2 = remote.connect(LISTEN.host, LISTEN.service) +--- +... +cn3 = remote.connect(LISTEN.host, LISTEN.service) +--- +... cn.space.tweedledum:select() --small request --- - [] @@ -46,8 +55,32 @@ box.stat.net.RECEIVED.total > 0 --- - true ... +box.stat.net.CONNECTIONS == 4 +--- +- true +... -- box.stat.net.EVENTS.total > 0 -- box.stat.net.LOCKS.total > 0 +WAIT_COND_TIMEOUT = 10 +--- +... +cn1:close() +--- +... +cn2:close() +--- +... +test_run:wait_cond(function() return box.stat.net.CONNECTIONS == 2 end, WAIT_COND_TIMEOUT) +--- +- true +... +cn3:close() +--- +... +test_run:wait_cond(function() return box.stat.net.CONNECTIONS == 1 end, WAIT_COND_TIMEOUT) +--- +- true +... -- reset box.stat.reset() --- @@ -60,6 +93,10 @@ box.stat.net.RECEIVED.total --- - 0 ... +box.stat.net.CONNECTIONS +--- +- 1 +... space:drop() -- tweedledum --- ... diff --git a/test/box/stat_net.test.lua b/test/box/stat_net.test.lua index 808bb71e76a8457e9a80d3b2345e79377b959655..9973769979a9ddee2938f82a8b3101486a66e84b 100644 --- a/test/box/stat_net.test.lua +++ b/test/box/stat_net.test.lua @@ -13,18 +13,31 @@ remote = require 'net.box' LISTEN = require('uri').parse(box.cfg.listen) cn = remote.connect(LISTEN.host, LISTEN.service) +cn1 = remote.connect(LISTEN.host, LISTEN.service) +cn2 = remote.connect(LISTEN.host, LISTEN.service) +cn3 = remote.connect(LISTEN.host, LISTEN.service) cn.space.tweedledum:select() --small request box.stat.net.SENT.total > 0 box.stat.net.RECEIVED.total > 0 +box.stat.net.CONNECTIONS == 4 -- box.stat.net.EVENTS.total > 0 -- box.stat.net.LOCKS.total > 0 +WAIT_COND_TIMEOUT = 10 + +cn1:close() +cn2:close() +test_run:wait_cond(function() return box.stat.net.CONNECTIONS == 2 end, WAIT_COND_TIMEOUT) +cn3:close() +test_run:wait_cond(function() return box.stat.net.CONNECTIONS == 1 end, WAIT_COND_TIMEOUT) + -- reset box.stat.reset() box.stat.net.SENT.total box.stat.net.RECEIVED.total +box.stat.net.CONNECTIONS space:drop() -- tweedledum cn:close()