nats-c library usage with libuv

120 views
Skip to first unread message

Anton Pantuykhin

unread,
Mar 6, 2020, 5:14:15 AM3/6/20
to nats
Hello! I have a question regarding usage of libuv event loop with nats-c library. I'm not very familiar with libuv, but afaik its uv_run(UV_RUN_DEFAULT) will exit when no active handles remain (in case of nats - probably when it's connection is closed). Does this mean, that if nats connection which is configured to automatically reconnect gets closed, uv_run(UV_RUN_DEFAULT) will exit and it should be restarted for reconnects to actually take place? Or maybe library handles this on it's own and uv_run(UV_RUN_DEFAULT) does not return until all reconnect attempts are exhausted and connection is permanently closed?

Ivan Kozlovic

unread,
Mar 6, 2020, 11:02:12 AM3/6/20
to nats
Anton,

If the connection is attempting to reconnect, the libuv loop will not exit, only after exhausting the number of reconnects. You can try for yourself using one of the example: https://github.com/nats-io/nats.c/blob/master/examples/libuv-sub.c

You will need to enable compilation of the libuv examples (make edit_cache) or pass "-DNATS_BUILD_LIBUV_EXAMPLE=ON" to the cmake command (check https://github.com/nats-io/nats.c#building for more details on how to build this project using CMake).

Running the sub program, you can send 1,000,000 messages to it and will see that it exit and reports the performance (you can use examples/nats-publisher or examples/nats-libuv-pub for that). Restart the sub, and this time stop the NATS Server, you will see that the sub program is still connected. Now restart the server, and then restart your publisher and you should see that the sub receive them all (it did reconnect). To see that the sub program would exit if the NATS Server is not restarted, you would have to wait for the max reconnect/delay to pass. By default it is 60 attempts with 2sec in between, so it may be a bit long. You could add these lines here and recompile to speed it up:

    if (s == NATS_OK)
       s = natsOptions_SetMaxReconnect(opts, 2);
   if (s == NATS_OK)
       s = natsOptions_SetReconnectWait(opts, 1000);


Hope this helps.

Anton Pantuykhin

unread,
Mar 6, 2020, 11:41:24 AM3/6/20
to nat...@googlegroups.com
Thanks, Ivan. This helped me a lot!

пятница, 6 марта 2020 г. пользователь Ivan Kozlovic <iv...@synadia.com> написал:
--
You received this message because you are subscribed to the Google Groups "nats" group.
To unsubscribe from this group and stop receiving emails from it, send an email to natsio+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/natsio/e7803a0c-90a6-49c2-bd03-f448ad99beb9%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages