本篇讲nsqlookupd中tcp.go、tcp_server.go

tcp_server.go位于util目录下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package util
       
import (
    "log"
    "net"
    "runtime"
    "strings"
)

type TCPHandler interface {
    Handle(net.Conn)
}

/**
*本方法开始接受客户端连接,并注册处理方法
*/
func TCPServer(listener net.Listener, handler TCPHandler) {
    log.Printf("TCP: listening on %s", listener.Addr().String())

    for {
        clientConn, err := listener.Accept()
        if err != nil {
            if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
                log.Printf("NOTICE: temporary Accept() failure - %s", err.Error())
                runtime.Gosched()
                continue
            }
            // theres no direct way to detect this error because it is not exposed
            if !strings.Contains(err.Error(), "use of closed network connection") {
                log.Printf("ERROR: listener.Accept() - %s", err.Error())
            }
            break
        }
        //handler由调用时通过参数传入,作为工具方法,将可能的变化放在外部,以保证本方法可以有尽可能多的适用范围
        go handler.Handle(clientConn)
    }

    log.Printf("TCP: closing %s", listener.Addr().String())
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package nsqlookupd

import (
    "io"
    "log"
    "net"

    "github.com/bitly/nsq/util"
)

type tcpServer struct {
    context *Context
}

//
// nsqlookupd接收到tcp数据时,会调用本方法处理。
//实现了/util/tcp_server.go中定义的TCPHandler接口
//

func (p *tcpServer) Handle(clientConn net.Conn) {
    log.Printf("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...
    //客户端在初始化自己的时候,需要发送4字节的数据用来标识它自己所有使用协议版本。将来升级协议的时候可以避免使用旧协议的客户端不能使用。
    buf := make([]byte, 4)
    _, err := io.ReadFull(clientConn, buf)
    if err != nil {
        log.Printf("ERROR: failed to read protocol version - %s", err.Error())
        return
    }
    //获取协议内容
    protocolMagic := string(buf)

    log.Printf("CLIENT(%s): desired protocol magic '%s'", clientConn.RemoteAddr(), protocolMagic)

    //util\Protocol.go中定义了一个Protocol的接口
    var prot util.Protocol

    switch protocolMagic {
    case "  V1":
        //当前只支持"  V1"协议(前两有两个空格,所以总共是4字节),协议在nsqlookupd\lookup_protocol_v1.go文件中定义,这里创建了LookupProtocolV1的实例
        //LookupProtocolV1实现了Protocol接口
        prot = &LookupProtocolV1{context: p.context}
    default:
        //如果不是"  V1"协议,则协议出错,断开链接,返回。
        util.SendResponse(clientConn, []byte("E_BAD_PROTOCOL"))
        clientConn.Close()
        log.Printf("ERROR: client(%s) bad protocol magic '%s'", clientConn.RemoteAddr(), protocolMagic)
        return
    }

    //如果是"  V1"f协议,这里就进入了LookupProtocolV1的IOLoop方法。此方法里有for死循环运行,直到出现error时,才会执行下面的代码。
    err = prot.IOLoop(clientConn)
    if err != nil {
        log.Printf("ERROR: client(%s) - %s", clientConn.RemoteAddr(), err.Error())
        return
    }
}

从以上代码可看出,tcp.go和tcp_server.go主要功能就是监听并接受TCP请求,收到请求后,将数据转给lookup_protocol_v1.go来处理。这个下篇再讲。

go语言nsq源码解读六 tcp.go、tcp_server.go的更多相关文章

  1. go语言nsq源码解读九 tcp和http中channel、topic的增删

    通过前面多篇文章,nsqlookupd基本已经解读完毕了,不过在关于channel和topic的增删上还比较模糊,所以本篇将站在宏观的角度来总结一下,tcp.go和http.go两个文件中关于chan ...

  2. go语言nsq源码解读八 http.go、http_server.go

    这篇讲另两个文件http.go.http_server.go,这两个文件和第六讲go语言nsq源码解读六 tcp.go.tcp_server.go里的两个文件是相对应的.那两个文件用于处理tcp请求, ...

  3. (转)go语言nsq源码解读二 nsqlookupd、nsqd与nsqadmin

    转自:http://www.baiyuxiong.com/?p=886 ---------------------------------------------------------------- ...

  4. go语言 nsq源码解读三 nsqlookupd源码nsqlookupd.go

    从本节开始,将逐步阅读nsq各模块的代码. 读一份代码,我的思路一般是: 1.了解用法,知道了怎么使用,对理解代码有宏观上有很大帮助. 2.了解各大模块的功能特点,同时再想想,如果让自己来实现这些模块 ...

  5. go语言nsq源码解读七 lookup_protocol_v1.go

    本篇将解读nsqlookup处理tcp请求的核心代码文件lookup_protocol_v1.go. 1234567891011121314151617181920212223242526272829 ...

  6. go语言nsq源码解读二 nsqlookupd、nsqd与nsqadmin

    nsqlookupd: 官方文档解释见:http://bitly.github.io/nsq/components/nsqlookupd.html 用官方话来讲是:nsqlookupd管理拓扑信息,客 ...

  7. go语言nsq源码解读一-基本介绍

    简单介绍一下nsq. 参考 http://feilong.me/2013/05/nsq-realtime-message-processing-system 的介绍:NSQ是由知名短链接服务商bitl ...

  8. go语言 nsq源码解读四 nsqlookupd源码options.go、context.go和wait_group_wrapper.go

    本节会解读nsqlookupd.go文件中涉及到的其中三个文件:options.go.context.go和wait_group_wrapper.go. options.go 123456789101 ...

  9. go语言nsq源码解读五 nsqlookupd源码registration_db.go

    本篇将讲解registration_db.go文件. 1234567891011121314151617181920212223242526272829303132333435363738394041 ...

随机推荐

  1. C#编译器优化那点事

    使用C#编写程序,给最终用户的程序,是需要使用release配置的,而release配置和debug配置,有一个关键区别,就是release的编译器优化默认是启用的. 优化代码开关即optimize开 ...

  2. Java编程语言下Selenium 对于下拉框,单选,多选等选择器的操作

    WebElement selector = driver.findElement(By.id("Selector")); Select select = new Select(se ...

  3. 关于mybatis更新数据的问题

    前两天用mybatis的时候,发现这样一个问题,日志显示mytatis更新数据已经成功了,但是实际上数据库是没有更新到的,经过一番查找,发现mybatis更新的时候默认返回的是查找到的数据(Rows  ...

  4. Windows ML,系统内置的机器学习平台初探

    人工智能现在很火,虽然最近风头隐隐有被区块链盖过,但仍是未来技术转型的首选方向之一.作为AI核心的机器学习,目前也进化到了可以基于平台自动训练模型的地步,例如Azure Machine Learnin ...

  5. ws-trust、域、webservice接口的总结

    最近燃料公司门户做了一个待办的汇总,从三个数据源拿数据汇总到首页,这三个数据源分别是域认证的接口,域认证的webservices,证书加密的接口,下面就这些接口,做一下简单总结 1 pfx证书的探索过 ...

  6. PCA算法和python实现

    第十三章 利用PCA来简化数据 一.降维技术 当数据的特征很多的时候,我们把一个特征看做是一维的话,我们数据就有很高的维度.高维数据会带来计算困难等一系列的问题,因此我们需要进行降维.降维的好处有很多 ...

  7. NPOI生成不规则Excel表格(并以流的形式下载,不将文件保存在服务器上,直接在客户端导出excel)

    //下载NPOI类库并添加引用 using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; using NPOI.SS.Util; public stati ...

  8. 学生管理系统_排序后通过name删除列表里的字典

    l = [{'name': 'wangfan', 'age': 18, 'sex': 'nan'}, {'name': 'wangerfan', 'age': 10, 'sex': 'nan'}, { ...

  9. 深入理解SpringBoot之自动装配

    SpringBoot的自动装配是拆箱即用的基础,也是微服务化的前提.其实它并不那么神秘,我在这之前已经写过最基本的实现了,大家可以参考这篇文章.这次主要的议题是,来看看它是怎么样实现的,我们透过源代码 ...

  10. 管理和安装 chart - 每天5分钟玩转 Docker 容器技术(168)

    安装 chart 当我们觉得准备就绪,就可以安装 chart,Helm 支持四种安装方法: 安装仓库中的 chart,例如:helm install stable/nginx 通过 tar 包安装,例 ...