package nsqd

import (
    "bytes"
    "encoding/json"
    "net"
    "os"
    "strconv"
    "time"

    "github.com/nsqio/go-nsq"
    "github.com/nsqio/nsq/internal/version"
)

func connectCallback(n *NSQD, hostname string, syncTopicChan chan *lookupPeer) func(*lookupPeer) {
    return func(lp *lookupPeer) {
        ci := make(map[string]interface{})
        ci["version"] = version.Binary
        ci["tcp_port"] = n.RealTCPAddr().Port
        ci["http_port"] = n.RealHTTPAddr().Port
        ci["hostname"] = hostname
        ci["broadcast_address"] = n.getOpts().BroadcastAddress

        cmd, err := nsq.Identify(ci)
        if err != nil {
            lp.Close()
            return
        }
        resp, err := lp.Command(cmd)
        if err != nil {
            n.logf("LOOKUPD(%s): ERROR %s - %s", lp, cmd, err)
        } else if bytes.Equal(resp, []byte("E_INVALID")) {
            n.logf("LOOKUPD(%s): lookupd returned %s", lp, resp)
        } else {
            err = json.Unmarshal(resp, &lp.Info)
            if err != nil {
                n.logf("LOOKUPD(%s): ERROR parsing response - %s", lp, resp)
            } else {
                n.logf("LOOKUPD(%s): peer info %+v", lp, lp.Info)
            }
        }

        go func() {
            syncTopicChan <- lp
        }()
    }
}

func (n *NSQD) lookupLoop() {
    var lookupPeers []*lookupPeer
    var lookupAddrs []string
    syncTopicChan := make(chan *lookupPeer)
    connect := true

    hostname, err := os.Hostname()
    if err != nil {
        n.logf("FATAL: failed to get hostname - %s", err)
        os.Exit(1)
    }

    // for announcements, lookupd determines the host automatically
    ticker := time.Tick(15 * time.Second)
    for {
        if connect {
            for _, host := range n.getOpts().NSQLookupdTCPAddresses {
                if in(host, lookupAddrs) {
                    continue
                }
                n.logf("LOOKUP(%s): adding peer", host)
                lookupPeer := newLookupPeer(host, n.getOpts().MaxBodySize, n.getOpts().Logger,
                    connectCallback(n, hostname, syncTopicChan))
                lookupPeer.Command(nil) // start the connection
                lookupPeers = append(lookupPeers, lookupPeer)
                lookupAddrs = append(lookupAddrs, host)
            }
            n.lookupPeers.Store(lookupPeers)
            connect = false
        }

        select {
        case <-ticker:
            // send a heartbeat and read a response (read detects closed conns)
            for _, lookupPeer := range lookupPeers {
                n.logf("LOOKUPD(%s): sending heartbeat", lookupPeer)
                cmd := nsq.Ping()
                _, err := lookupPeer.Command(cmd)
                if err != nil {
                    n.logf("LOOKUPD(%s): ERROR %s - %s", lookupPeer, cmd, err)
                }
            }
        case val := <-n.notifyChan:
            var cmd *nsq.Command
            var branch string

            switch val.(type) {
            case *Channel:
                // notify all nsqlookupds that a new channel exists, or that it's removed
                branch = "channel"
                channel := val.(*Channel)
                if channel.Exiting() == true {
                    cmd = nsq.UnRegister(channel.topicName, channel.name)
                } else {
                    cmd = nsq.Register(channel.topicName, channel.name)
                }
            case *Topic:
                // notify all nsqlookupds that a new topic exists, or that it's removed
                branch = "topic"
                topic := val.(*Topic)
                if topic.Exiting() == true {
                    cmd = nsq.UnRegister(topic.name, "")
                } else {
                    cmd = nsq.Register(topic.name, "")
                }
            }

            for _, lookupPeer := range lookupPeers {
                n.logf("LOOKUPD(%s): %s %s", lookupPeer, branch, cmd)
                _, err := lookupPeer.Command(cmd)
                if err != nil {
                    n.logf("LOOKUPD(%s): ERROR %s - %s", lookupPeer, cmd, err)
                }
            }
        case lookupPeer := <-syncTopicChan:
            var commands []*nsq.Command
            // build all the commands first so we exit the lock(s) as fast as possible
            n.RLock()
            for _, topic := range n.topicMap {
                topic.RLock()
                if len(topic.channelMap) == 0 {
                    commands = append(commands, nsq.Register(topic.name, ""))
                } else {
                    for _, channel := range topic.channelMap {
                        commands = append(commands, nsq.Register(channel.topicName, channel.name))
                    }
                }
                topic.RUnlock()
            }
            n.RUnlock()

            for _, cmd := range commands {
                n.logf("LOOKUPD(%s): %s", lookupPeer, cmd)
                _, err := lookupPeer.Command(cmd)
                if err != nil {
                    n.logf("LOOKUPD(%s): ERROR %s - %s", lookupPeer, cmd, err)
                    break
                }
            }
        case <-n.optsNotificationChan:
            var tmpPeers []*lookupPeer
            var tmpAddrs []string
            for _, lp := range lookupPeers {
                if in(lp.addr, n.getOpts().NSQLookupdTCPAddresses) {
                    tmpPeers = append(tmpPeers, lp)
                    tmpAddrs = append(tmpAddrs, lp.addr)
                    continue
                }
                n.logf("LOOKUP(%s): removing peer", lp)
                lp.Close()
            }
            lookupPeers = tmpPeers
            lookupAddrs = tmpAddrs
            connect = true
        case <-n.exitChan:
            goto exit
        }
    }

exit:
    n.logf("LOOKUP: closing")
}

func in(s string, lst []string) bool {
    for _, v := range lst {
        if s == v {
            return true
        }
    }
    return false
}

func (n *NSQD) lookupdHTTPAddrs() []string {
    var lookupHTTPAddrs []string
    lookupPeers := n.lookupPeers.Load()
    if lookupPeers == nil {
        return nil
    }
    for _, lp := range lookupPeers.([]*lookupPeer) {
        if len(lp.Info.BroadcastAddress) <= 0 {
            continue
        }
        addr := net.JoinHostPort(lp.Info.BroadcastAddress, strconv.Itoa(lp.Info.HTTPPort))
        lookupHTTPAddrs = append(lookupHTTPAddrs, addr)
    }
    return lookupHTTPAddrs
}

lookup.go的更多相关文章

  1. SQL Server-聚焦移除Bookmark Lookup、RID Lookup、Key Lookup提高SQL查询性能(六)

    前言 前面几节都是讲的基础内容,本节我们讲讲索引性能优化,当对大数据进行处理时首先想到的就是索引,一旦遇到这样的问题则手忙脚乱,各种查资料,为何平常不扎实基本功呢,我们由浅入深,简短的内容,深入的理解 ...

  2. Salesforce的sharing Rule 不支持Lookup型字段解决方案

    Salesforce 中 sharing rule 并不支持Look up 字段 和 formula 字段.但在实际项目中,有时会需要在sharing rule中直接取Look up型字段的值,解决方 ...

  3. eclipse调试(debug)的时候,出现Source not found,Edit Source Lookup Path,一闪而过

    问题描述 使用Eclipse调试代码的时候,打了断点,经常出现Source not found,网上找了半天,大部分提示点击Edit Source Lookup Path,添加被调试的工程,然而往往没 ...

  4. mongodb 3.x 之实用新功能窥看[2] ——使用$lookup做多表关联处理

    这篇我们来看mongodb另一个非常有意思的东西,那就是$lookup,我们知道mongodb是一个文档型的数据库,而且它也是最像关系型数据库的 一种nosql,但是呢,既然mongodb是无模式的, ...

  5. Lookup component 用法

    Lookup component 类似于Tsql的join子句, select a.* ,b.* from dbo.tis a left join dbo. tdes b on a.code=b.co ...

  6. [SharePoint]javascript client object model 获取lookup 类型的field的值,包括user类型(单人或者多人)的值。how to get the multiple user type/lookup type field value by Javascript client object model

    1. how to get value var context = new SP.ClientContext.get_current(); var web = context.get_web(); v ...

  7. [SharePoint 2010] Modify lookup mapping with PowerShell

    SharePoint支持将列表保存成列表模板,但当列表包含Lookup字段时,通过模板创建的列表会丢失Lookup字段的信息. 通过PowerShell,可以修改Lookup字段的xml内容. Fun ...

  8. Informatica Lookup Transformation组件的Connect 与Unconnected类型用法

    Informatica Lookup Transformation组件的Connect 与Unconnected类型用法及区别:下面是通一个Lookup在不同Mapping中的使用: 1. Conne ...

  9. AX 2012 两种lookup 的显示方式

    第一种:只能单选的lookup: 代码: public void BusinessUnitLookup(FormStringControl _formstrcontroll) { //OMOperat ...

  10. 创建一个List获取数据的lookup

    第一步,在类:syslookup中新建方法 public static client void lookupList(FormStringControl _formStringControl, Lis ...

随机推荐

  1. mongodb3.6 (四)net 客户端如何连接、访问mongodb集群

    前言 在是一篇文章mongodb如何做数据备灾 中已经介绍mongodb集群是如何工作,可能很多人都有这样一个疑问:客户端如何知道主服务挂了呢?这一篇文章将介绍如何在net中访问这个集群. 第一步.安 ...

  2. javascript预览图片——IT轮子系列(九)

    再使用htm控件 <input type="file" name="file" /> 上传图片的时候,往往需要先预览图片,然后点击保存按钮,把图片上 ...

  3. String内存分配

    Java 把内存划分成两种:一种是栈内存,另一种是堆内存.在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的 栈内存中分配,当在一段代码块定义一个变量时,Java 就在栈中为这个变量分配内存 ...

  4. 前端技术之_CSS详解第六天--完结

    前端技术之_CSS详解第六天--完结 一.复习第五天的知识 a标签的伪类4个: a:link 没有被点击过的链接 a:visited 访问过的链接 a:hover 悬停 a:active 按下鼠标不松 ...

  5. SQL Server复制表结构和表数据生成新表的语句

    参考:http://topic.csdn.net/t/20020621/09/820025.html SELECT   *   INTO   newTableName   FROM   oldTabl ...

  6. I want to try to improve myself from today

    I involved in the Internet of Things project team in my university in 2015 and now I have completed ...

  7. 分布式消息队列XXL-MQ

    <分布式消息队列XXL-MQ>     一.简介 1.1 概述 XXL-MQ是一款轻量级分布式消息队列,支持串行.并行和广播等多种消息模型.现已开放源代码,开箱即用. 支持三种消息模式: ...

  8. django相关网站

    记录django的学习笔记:http://www.cnblogs.com/qwj-sysu/tag/django/ uwsgi的文档:http://uwsgi-docs.readthedocs.io/ ...

  9. JavaScrip对象

    一.JavaScript对象概述 JavaScript 中的所有事物都是对象:字符串.数值.数组.函数...此外,JavaScript 允许自定义对象.JavaScript 提供多个内建对象,比如 S ...

  10. JS响应数据

    页面中展示的信息都是存储在服务器中的数据,离开数据的页面就像是一块画板的作用,如何通过数据来描述一个页面,又怎么映射数据变化和页面渲染的关系. 当然,最直接的方法就是操作节点,页面加载之后获取节点,再 ...