question

Upvotes
Accepted
1 0 0 1

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

#technologyema-apic++
1708001491029.png (282.4 KiB)
1708001502570.png (285.0 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvote
Accepted
21.9k 58 14 21

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.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
21.9k 58 14 21

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.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

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.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.