Skip to content
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 graceful errors and graceful shutdowns #53

Open
joeyak opened this issue Mar 20, 2019 · 6 comments
Open

Add graceful errors and graceful shutdowns #53

joeyak opened this issue Mar 20, 2019 · 6 comments
Assignees
Labels
enhancement New feature or request front-end Anything HTML/CSS/JS hardening Changes that are not new features, but benefit the developer

Comments

@joeyak
Copy link
Collaborator

joeyak commented Mar 20, 2019

This would have 3 parts.

  1. Have the server send an error object to the client so the client can tell what an error message is.
    This can be a wrapper around errors.
type Something struct {
err Error
}
func (s *Something) HTML() { return "blah" + err.Error() }
  1. Have the client send a ClientData object with a special type for name changing. The first successful name change means that the client can join the chat room, because it will be upgraded from a temporary connection. Apply this change to the /nick command too.

  2. Have movie night shutdown gracefully on ctrl+c with sending an error message saying something along the lines of "The server has been shut down". The code below can be used to have a graceful shutdown, that if it takes too long, just stops.

sigintCh chan os.Signal = make(chan os.Signal)
...
func main() {
...
	go func() {
		<-sigintCh
		// Killswitch if takes too long
		go func() {
			wait := waitMs*time.Duration(count) + time.Second*5

			// Make a reasonable wait time to kill a program
			if wait > time.Second*20 {
				wait = time.Second * 20
			}

			time.Sleep(wait)
			fmt.Println("Had to force close of program because it took too long to close")
			os.Exit(3)
		}()

		// Close connections
		fmt.Printf("Closing %d connections\n", count)
		handler.close()
		os.Exit(2)
}()
@joeyak joeyak added enhancement New feature or request front-end Anything HTML/CSS/JS hardening Changes that are not new features, but benefit the developer labels Mar 20, 2019
@joeyak
Copy link
Collaborator Author

joeyak commented Mar 21, 2019

The closing of the client connections can be done in the handleInterrupt function in fac6e28

@joeyak joeyak closed this as completed Mar 21, 2019
@joeyak
Copy link
Collaborator Author

joeyak commented Mar 21, 2019

Accidentally closed it

@zorchenhimer
Copy link
Owner

I was playing around with expanding the code that you had already written for this to include http.Server.Shutdown(). I'm not sure it's strictly needed, but it might be a good idea to use that.

Essentially, a function is registered to be called once server.Shutdown() is called that can handle closing non-http connections (ie, the websockets). I had it setup to call a new ChatRoom.Shutdown() function that simply closes all the websocket connections and disallows new joins. This could be used to send a message to the client before closing the connection.

I can push the branch that I made while testing if you want to have a look.

@joeyak
Copy link
Collaborator Author

joeyak commented Mar 27, 2019

sure push it. I'm interested what the change looks like. Also nice find.

@zorchenhimer
Copy link
Owner

Pushed the new branch: e879112

@zorchenhimer zorchenhimer self-assigned this Apr 12, 2019
@zorchenhimer
Copy link
Owner

I was looking at this again the other day to see what it would take to finish this up. Shutting down the HTTP server is rather straight forward, however shutting down the RTMP server is a different story. It doesn't look like it's possible without handling packets manually, but I haven't been able to really dig into the library code all that much.

I may just say "screw it" for the RTMP server stuff for now and just focus on the HTTP component. I've been wanting to get this done so I can properly daemonize the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request front-end Anything HTML/CSS/JS hardening Changes that are not new features, but benefit the developer
Projects
None yet
Development

No branches or pull requests

2 participants