facebook / wangle

Wangle is a framework providing a set of common client/server abstractions for building services in a consistent, modular, and composable way.
Apache License 2.0
3.04k stars 536 forks source link

How to "attach" stateful data? #84

Closed Salcoj closed 7 years ago

Salcoj commented 7 years ago

Great afternoon!

I'm currently programming on a server and i'm wondering about the best (or intended) way to store information about sessions. One connection can send messages relayed through the server to another connection so i need some kind of session object which stores information like encryption keys(each connection could have its down) and functionality to send messages(to the connected peer) or disconnect itself.

My current approach is to build a pipeline like this: (inbound) socket->frame detection->message deserialization->my service dispatcher(single instance, shared among all pipeline instances)

The service dispatcher creates a session when a new connection appears, stores it in a map and deletes it on readEOF. When a wild message appears in read, it performs a lookup of the session and then uses it to dispatch the message with service objects. This breaks the "1 handler 1 job" rule and it makes me feel dirty inside somehow as i feel like i could avoid the lookup here. I thought of another handler that just creates a session for each instance of the handler and wraps it into some kind of Request(session, message).