gnet
gnet copied to clipboard
Proposal: add WakeWithCb(cb func(conn gnet.Conn) error)
I use gnet to write websocket server and it looks very promising, thanks for that framework! :)
But I think it can be event better if there is API to modify gnet.Conn
context.
Is your feature request related to a problem? Please describe.
There is no straightforward way to change gnet.Conn
context without external mutex.
This feature might be useful if I have a something like connection manager that store and manage connections grouped by topics for example. So if I want to add some metadata to connections contexts (for example - mark all topic connections to do something when that conns will be disconnected) I do not have any straightforward solutions to do that somewhere not inside loops
like wake-up loop or ticker loop (timer is not usable here if I use multicore since when OnTick
fired there is no any information about loop, and conns do not provide that info too).
Describe the solution you'd like
Add to gnet.Conn
method that allow not only wake-up but wake-up and execute something.
It may looks like: WakeCb(cb func(conn gnet.Conn) error)
.
It should not require much changes - in current Wake
method we should not call loopWake
, but provided func instead. That allow us to serialize access to conn
context without locks.
Describe alternatives you've considered
In Tick
event might be provided event-loop index, and in gnet.Conn
that loop index might be exposed too. That allow us serialize access in every tick and natively distribute connections via loop index to prevent mutex contention. This solution still require externally managed mutex but at least allow us to reduce contention (for example we can have individual mutex for every event loop and lock for one will not stop entire server).
Additional context I have long-lived websocket connections that distributed to «topics» (something like websocket pub/sub system). When connection is arrived I put it to topic connections list (managed separately in separate goroutine), and when I want to send message to topic I iterate over stored conns and AsyncWrite to them.
Sometimes I want to track some connections little bit closer and fire event to external system when that connection stops. So if I have a way to modify its context I can throw away many synchronization works and just fire (pooled) send in OnClosed
event if context contains some flag.
Thanks for opening a new issue. The team has been notified and will review it as soon as possible. For urgent issues and priority support, visit https://xscode.com/panjf2000/gnet
Investigating on this requirement, maybe start developing this feature in the near few days.
Hello again! I have drafted initial implementation for my needs. It still WIP (I try to solve my initial needs with this solution and change it when I need). https://github.com/panjf2000/gnet/compare/master...ligser:feature/add-async-execute Feel free to get some inspiration if you'll found that way as valid. If not — I'll wait until until you present your vision and will use my solution before that happen.
I'd encourage you to open a PR for this feature since you've already implemented it.
PR created.