New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add an udp server example #502
Conversation
Thank you for opening the PR! |
|
||
|
||
class MyUDPServer < Concurrent::Actor::RestartingContext | ||
@@thread = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am curious why is a class variable used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't figure out a way to properly exit the thread if the actor dies so I tried another road which was to keep the receiving thread linked to the socket alive and only restart the actor itself, the thread variable is used so that the thread is only spawned once.
As I said in my pr message I not that entirely happy with this solution but I tested it and at least it works, at least from what I remember when I tried :)
Thank you @schmurfy I have look later and I'll add it to our documentation. |
it should be refined before adding to the documentation but I wanted to start the conversation, if you have ideas on improvements I can give them a try, the main thing I was looking for was to have a reliable, fault resilient actor receiving the udp packets, it might miss a few but no matter what happens it should resume listening. |
@schmurfy could you have look if this https://gist.github.com/pitr-ch/119e48c75ab5ccbbdac9 works for you? If yes, please update the PR. |
Sure but after a quick look I am pretty sure it has the same issue I tried to dodge: if the server crash it will not be able to rebind on the same port because it will be already used (the old thread won't be dead), so if your actor crash for any reason you won't ever receive another message. |
Isn't the tread terminated on actor crash? https://gist.github.com/pitr-ch/119e48c75ab5ccbbdac9#file-udp_server-rb-L38 |
I just did another test with your version, I just added a crash condition as I did in mine when receiving "crash" and here what I get hust after the crash:
The messages appear to be received so I imagined that the socket is either never closed or the reopening works but the read handler is never called after the crash Edit: I use an external client (nc) to send the data to ensure nothing is interacting in either way with the server code, in the server I just create the actor at the end and call sleep |
Sorry for letting this PR without attention for so long. I'll get back to in next release. |
This might not be mergeable as is but so far this is the best I got for creating an udp server, what I was going for here is that if anything bad happens the server should restart and still be able to receive data.
The design started from a discussion in #144 with the modified code @pitr-ch and after playing around a bit I came to this, I am not entirely happy with this but it works, if you send "crash" to it it will crash and get restarted to continue process the inputs, the udp socket itself is never stopped.
If anyone has any idea to improve this you are welcome :)