Releasing OmmConsumer resources in a multithreaded environment C++

I have an HTTP server (cpp-httplib library) that creates and should destroy an instance of the OmmConsumer class when processing a POST request. The library creates 10 threads to handle requests, and it feels like the OmmConsumer instance is not fully destroyed in each of them. Am I doing something wrong or is there a memory leak?

void handle_post_request(const httplib::Request &req, httplib::Response &res){
    try{
        OmmConsumer consumer(OmmConsumerConfig().username("username").host("127.0.0.1:1234"));
        res.set_content("OK", "application/json");
    }
    catch (const std::exception &excp){
        res.set_content("BAD", "application/json");
    }
}

int main(int argc, char *argv[]){
    httplib::Server server;
    auto endpoint = "/test/";
    auto host = "0.0.0.0";
    auto port = 8080;
    try{
        server.Post(endpoint, handle_post_request);
    }
    catch (const std::exception &e){
        return 54;
    }
    server.listen(host, port);
}

Before requests:

1708001491029.png

After requests:

1708001502570.png

Best Answer

  • Gurpreet
    Answer ✓

    Thank you for the answer, but in my case, I receive HTTP requests with a username for connection and further access verification to the tools, so I can’t create consumers in advance.

    Since, the application is serving over http, and you are snapshoting the data, another option is to skip the http-server part altogether and use the REST interface, which is available in the newer version of RTMDS. You should be able to pass in the username of requesting entity in the REST call as well.

    See this article for more details.

Answers

  • Hi @switcherman089,

    Creating and destroying the consumers with each incoming http request would be a bad design choice. I would recommend that you create a pool of OMMConsumer objects, which connect and login to the market data system.

    Upon every incoming http request - just round-robin the subscription requests to one of these consumer objects.

  • Thank you for the answer, but in my case, I receive HTTP requests with a username for connection and further access verification to the tools, so I can’t create consumers in advance.

  • Creating the consumer is a resource hungry operation and this should not be done on the fly. You might need to re-visit the application architecture in that case. If you are connecting to the local RTMDS, look into other means of user-reporting like OpenDACS etc.