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

nsqd: fix stall in Exit() due to tcp producer conns #1198

Merged
merged 1 commit into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions nsqd/nsqd.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type NSQD struct {

lookupPeers atomic.Value

tcpServer *tcpServer
tcpListener net.Listener
httpListener net.Listener
httpsListener net.Listener
Expand Down Expand Up @@ -154,6 +155,7 @@ func New(opts *Options) (*NSQD, error) {
n.logf(LOG_INFO, version.String("nsqd"))
n.logf(LOG_INFO, "ID: %d", opts.ID)

n.tcpServer = &tcpServer{}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to put this over here to appease the race-detector

n.tcpListener, err = net.Listen("tcp", opts.TCPAddress)
if err != nil {
return nil, fmt.Errorf("listen (%s) failed - %s", opts.TCPAddress, err)
Expand Down Expand Up @@ -255,14 +257,16 @@ func (n *NSQD) Main() error {
})
}

tcpServer := &tcpServer{ctx: ctx}
n.tcpServer.ctx = ctx
n.waitGroup.Wrap(func() {
exitFunc(protocol.TCPServer(n.tcpListener, tcpServer, n.logf))
exitFunc(protocol.TCPServer(n.tcpListener, n.tcpServer, n.logf))
})

httpServer := newHTTPServer(ctx, false, n.getOpts().TLSRequired == TLSRequired)
n.waitGroup.Wrap(func() {
exitFunc(http_api.Serve(n.httpListener, httpServer, "HTTP", n.logf))
})

if n.tlsConfig != nil && n.getOpts().HTTPSAddress != "" {
httpsServer := newHTTPServer(ctx, true, true)
n.waitGroup.Wrap(func() {
Expand Down Expand Up @@ -423,6 +427,9 @@ func (n *NSQD) Exit() {
if n.tcpListener != nil {
n.tcpListener.Close()
}
if n.tcpServer != nil {
n.tcpServer.CloseAll()
}

if n.httpListener != nil {
n.httpListener.Close()
Expand Down
16 changes: 14 additions & 2 deletions nsqd/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package nsqd
import (
"io"
"net"
"sync"

"github.com/nsqio/nsq/internal/protocol"
)

type tcpServer struct {
ctx *context
ctx *context
conns sync.Map
}

func (p *tcpServer) Handle(clientConn net.Conn) {
Expand Down Expand Up @@ -41,9 +43,19 @@ func (p *tcpServer) Handle(clientConn net.Conn) {
return
}

p.conns.Store(clientConn.RemoteAddr(), clientConn)

err = prot.IOLoop(clientConn)
if err != nil {
p.ctx.nsqd.logf(LOG_ERROR, "client(%s) - %s", clientConn.RemoteAddr(), err)
return
}

p.conns.Delete(clientConn.RemoteAddr())
}

func (p *tcpServer) CloseAll() {
p.conns.Range(func(k, v interface{}) bool {
v.(net.Conn).Close()
return true
})
}