Hello, everybody
We are using in our project OpenResty's websocket library extensively (resty.websocket.server). An
emerging need now is to find out how "extensively" these websocket
connections are used. Basically, we need to count the number of active websocket connections currently opened
in NginX.
The websocket handling code is basically a "while true do" infinite loop.
The method I thought about is to use an ngx.shared.DICT in which to insert/update a key for
each websocket connection with a short expiry time. Then, all I would
have to do is to count the dictionary entries.
Something like this:
while true do
....
-- do this every second or so:
-- get an unique websocket ID,
either by generating a random large string, or by some other method
local ws_uid = '???'
local wslist =
ngx.shared.wslist
wslist:set(ws_uid, true, 2)
...
end
Then, in another place (where we render the stats page) I should count
the keys in ngx.shared.wslist.
My questions are:
1. How do I efficiently get the number of keys in a shared dictionary?
(will it have to be locked to get all the keys, then count them?)
2. Will this method have a bad impact on performance, considering I will
update the shared dict every second or so from many concurrent
websockets (thousands, or event tens of thousands)
3. Is there a better, more efficient way to count the opened websocket
connections?
Thank you
Kind regards,
Bogdan
|