How sessions are handled in listener

Apr 24, 2014 at 5:02 AM
Can someone please help me understand how sessions are handled in the listener. In particular, how is a session created, and how do we make sure that the id is unique and complex to avoid mixup.

Also, How to we set the thread pool size and the maximum number of sessions. I want to dump libmicrohttpd and move to casablanca.
May 5, 2014 at 5:52 PM
Hi Nagendra17,

The underlying sessions and connections aren't currently exposed to users, so there isn't a public API for retrieving a session id or setting the maximum number of sessions.

Both of these seem like reasonable features that could be added to the API. For setting the maximum number of sessions the http_listener_config class is probably the correct location to expose a knob for something like this. For adding session ids, there probably is more work involved. Perhaps a property could be added to the http_request.

For both of these I recommend opening a feature request. We also accept contributions, these probably will get addressed sooner if you propose a change and take a look at adding them. If you have interest in implementing them let me know.

May 5, 2014 at 6:09 PM
I have created the request.
Is the class constructor that has the methods bound to GET, POST etc. called every time there is a new session or only once at startup?
May 6, 2014 at 3:50 AM
Hi Nagendra17,

I don’t understand what you are asking, can you please provide some detail? Specifically what class’s constructor are you talking about? The http_request class?

May 6, 2014 at 4:12 AM
I defined my class as follows:

class GenderClassifier
GenderClassifier(utility::string_t url, GenderIdOptions gender_id_options);

pplx::task<void> open() { return; }
pplx::task<void> close() { return m_listener.close(); }
void handle_get(http_request message);
void handle_put(http_request message);
void handle_post(http_request message);
void handle_delete(http_request message);

http_listener m_listener;
GenderId gender_id_;

and the GenderId object consumes a lot of memory and need not be duplicated for each context. In main I do the following (in initialize)
g_GenderClassifier = std::unique_ptr<GenderClassifier>(new GenderClassifier(addr,gender_id_options));
So I am just double-checking that the same class is passed as a reference to each of the threads invoking / handling the "handle_post" , "handle_get" etc.

On the other hand, there are some items that need to persist between multiple put calls of the same session, and need to be destroyed when the session expires. Where should I put those?
May 6, 2014 at 5:36 PM
Hi Nagendra17,

I think you have some confusion somewhere about lifetime or usage of the http_listener. So you have an http_listener object as a member variable on your class GenderClassifier. Although the code doesn't show it I assume in the constructor you make calls to http_listener::support mapping the HTTP verbs to your various handle_get, etc... member functions. There is only one http_listener instance you have here. All HTTP requests, regardless of the underlying connection will be processed with this listener.

You mention that the GenderId class consumes a lot of memory and you don't want it to be copied. From what I see here why would there ever be any copies of the made? All the http_listener knows is what functions (registered with http_listener::support) to call when a HTTP GET, PUT, etc... request comes in. It doesn't know anything about the existence of the gender_id_ variable or the GenderClassifier class.

Like I mentioned in the previous post, our library currently doesn't expose any options for determining the underlying connection a request came across. There currently is no way to determine if a request came from a new connection or an existing persistent connection. This could potentially be added.