Skip to content

Commit

Permalink
Fix redcon.Serve never returning.
Browse files Browse the repository at this point in the history
Server should return when the base Listener has been closed.

See #46
  • Loading branch information
tidwall committed Mar 22, 2022
1 parent 30bcac2 commit e0f7ba0
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions redcon.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,12 @@ func NewServerNetwork(
if handler == nil {
panic("handler is nil")
}
s := &Server{
net: net,
laddr: laddr,
handler: handler,
accept: accept,
closed: closed,
conns: make(map[*conn]bool),
}
s := newServer()
s.net = net
s.laddr = laddr
s.handler = handler
s.accept = accept
s.closed = closed
return s
}

Expand Down Expand Up @@ -221,22 +219,26 @@ func (s *TLSServer) ListenAndServe() error {
return s.ListenServeAndSignal(nil)
}

func newServer() *Server {
s := &Server{
conns: make(map[*conn]bool),
}
return s
}

// Serve creates a new server and serves with the given net.Listener.
func Serve(ln net.Listener,
handler func(conn Conn, cmd Command),
accept func(conn Conn) bool,
closed func(conn Conn, err error),
) error {
s := &Server{
net: ln.Addr().Network(),
laddr: ln.Addr().String(),
ln: ln,
handler: handler,
accept: accept,
closed: closed,
conns: make(map[*conn]bool),
}

s := newServer()
s.net = ln.Addr().Network()
s.laddr = ln.Addr().String()
s.ln = ln
s.handler = handler
s.accept = accept
s.closed = closed
return serve(s)
}

Expand Down Expand Up @@ -345,6 +347,10 @@ func serve(s *Server) error {
if done {
return nil
}
if errors.Is(err, net.ErrClosed) {
// see https://github.com/tidwall/redcon/issues/46
return nil
}
if s.AcceptError != nil {
s.AcceptError(err)
}
Expand Down

0 comments on commit e0f7ba0

Please sign in to comment.