webby icon indicating copy to clipboard operation
webby copied to clipboard

WebbyServerConfig has no user data, forces use of globals

Open seanmiddleditch opened this issue 9 years ago • 3 comments

Webby doesn't have any user data in WebbyServerConfig. This means that the callbacks like ws_connect can't receive any user data (because there's none to receive) and WebbyConnection can have only a default NULL user data, so new connections cannot know which context they are associated with. This means that whatever user data gets associated with the WebbyConnection must come from a global variable.

This effectively limits Webby to a single server instance at a time, or requires each instance to use a different ws_connect callback at the very least. While that isn't necessarily a real problem for many Webby use cases (including mine), the requirement of a global variable is somewhat problematic to writing more well-structured modular code. In my case, while I don't expect a need for more than one Webby instance, I am developing a library that uses Webby and would like to give my end users the option to create multiple instances of my library's internal state. I am effectively blocked from doing that without modifying Webby to have user data associated with WebbyServer.

A local hack I have is to add a user_data field to WebbyServerConfig that is used as the initial value of the user_data for new WebbyConnections.

seanmiddleditch avatar Jan 30 '16 22:01 seanmiddleditch

Maybe it'd be cleaner to

  1. Add a handler_userdata pointer to the config
  2. Change all handler signatures to take that userdata as 2nd parameter

deplinenoise avatar Jan 31 '16 19:01 deplinenoise

I agree, that would be a better approach. So long as you keep the WebbyConnection user data, too.

I originally started by just add the user_data to get passed along to ws_connected but then noticed that I'd need it for ws_connect too to make certain decisions, and that ws_log also could use one, and went the simple route on my end. :)

seanmiddleditch avatar Jan 31 '16 22:01 seanmiddleditch

Very related issue (not sure if you'd prefer this as a separate ticket) is that the calls to reset_connection are resetting the connection user data after each request, including after the calls to ws_connect and its ilk for Websocket connections, making the per-connection user data somewhat useless if one needs to actually set it to anything other than its default value.

For regular HTTP request handling, this makes some sense, but it makes the whole per-connection user data feature useless, even for Websocket connections. Only the per-server user data even works, and then only because I changed reset_connection to set the user data to the server config's value instead of null.

seanmiddleditch avatar Feb 01 '16 07:02 seanmiddleditch