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

Using a go routine does not keep the connection alive #35

Open
lookfirst opened this issue Jun 6, 2019 · 1 comment
Open

Using a go routine does not keep the connection alive #35

lookfirst opened this issue Jun 6, 2019 · 1 comment

Comments

@lookfirst
Copy link
Contributor

Using a go routine does not keep the connection alive.

  1. compile program.
  2. telnet to port 3000
  3. kill -s SIGUSR2 PID

If you do this same program without the go routine wrapper, the connection is not dropped.

package main

import (
        "fmt"
        "log"
        "net/http"

        "github.com/jpillora/overseer"
)

//create another main() to run the overseer process
//and then convert your old main() into a 'prog(state)'
func main() {
        overseer.Run(overseer.Config{
                Debug: true,
                Program: prog,
                Address: ":3000",
        })
}

//prog(state) runs in a child process
func prog(state overseer.State) {
        log.Printf("app (%s) listening...", state.ID)
        http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                fmt.Fprintf(w, "app (%s) says hello\n", state.ID)
        }))
        go func() {
                if err := http.Serve(state.Listener, nil); err != nil {
                     log.Println(err)
                }
        }()
        <-state.GracefulShutdown
}
@lookfirst
Copy link
Contributor Author

Ah, I think I understand now. The issue is that normally the state.Listener, which gets converted into a overseeerListener would block being closed, so the connection remains open. But if it is run in a go routine, that blocking doesn't happen. I wonder how to make that happen still.

The issue is that I would like to be able to start up multiple listeners and have zero downtime restarts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant