tidwall / redcon

Redis compatible server framework for Go
MIT License
2.16k stars 159 forks source link

redcon.Serve() never returns. #46

Open lithdew opened 3 years ago

lithdew commented 3 years ago

redcon.serve() only returns if (*Server).done is ever set to true, which is only possible if (*Server).Close() is called.

Given that the use of redcon.Serve(ln net.Listener, ...) unlike any of the other methods exposed in the package assumes that the user is not keeping around a *redcon.Server instance, it wouldn't be possible for the user to force redcon.Serve() to ever return, which is behavior that is desired for e.g. graceful shutdown.

Rather than ignoring errors from net.Listener.Accept() in redcon.serve() unless (*Server).done is set to true, what about breaking early from redcon.serve()'s accept loop if an error is returned by net.Listener.Accept() which indicates that the listener is closed?

For example:

lnconn, err := s.ln.Accept()
if err != nil {
    // .....
    if errors.Is(err, net.ErrClosed) {
        return nil
    }
}
tidwall commented 2 years ago

Yes. This makes sense. I just pushed a fix that works as you describe.