NSQ的TCP逻辑都是这样的,调用internal/protocol/tcp_server.go中的TCPServer:
func TCPServer(listener net.Listener, handler TCPHandler, l app.Logger)接受客户端连接,在这里客户端为consumer,调用TCPHandler.Handle处理业务逻辑,TCPHandler.Handle是个接口,让不同的业务逻辑自己实现这个接口。
这里在tcp.go文件中实现:
func (p *tcpServer) Handle(clientConn net.Conn) {
p.ctx.nsqd.logf("TCP: new client(%s)", clientConn.RemoteAddr())
// The client should initialize itself by sending a 4 byte sequence indicating
// the version of the protocol that it intends to communicate, this will allow us
// to gracefully upgrade the protocol away from text/line oriented to whatever..