本文整理汇总了Golang中github.com/bitly/nsq/nsq.ReadResponse函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadResponse函数的具体用法?Golang ReadResponse怎么用?Golang ReadResponse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadResponse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: subWorker
func subWorker(n int, workers int, tcpAddr string, topic string, channel string, rdyChan chan int, goChan chan int, id int) {
conn, err := net.DialTimeout("tcp", tcpAddr, time.Second)
if err != nil {
panic(err.Error())
}
conn.Write(nsq.MagicV2)
rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
ci := make(map[string]interface{})
ci["short_id"] = "test"
ci["long_id"] = "test"
cmd, _ := nsq.Identify(ci)
cmd.Write(rw)
nsq.Subscribe(topic, channel).Write(rw)
rdyCount := int(math.Min(math.Max(float64(n/workers), 1), 2500))
rdyChan <- 1
<-goChan
nsq.Ready(rdyCount).Write(rw)
rw.Flush()
nsq.ReadResponse(rw)
nsq.ReadResponse(rw)
num := n / workers
numRdy := num/rdyCount - 1
rdy := rdyCount
for i := 0; i < num; i += 1 {
resp, err := nsq.ReadResponse(rw)
if err != nil {
panic(err.Error())
}
frameType, data, err := nsq.UnpackResponse(resp)
if err != nil {
panic(err.Error())
}
if frameType == nsq.FrameTypeError {
panic("got something else")
}
msg, err := nsq.DecodeMessage(data)
if err != nil {
panic(err.Error())
}
nsq.Finish(msg.Id).Write(rw)
rdy--
if rdy == 0 && numRdy > 0 {
nsq.Ready(rdyCount).Write(rw)
rdy = rdyCount
numRdy--
rw.Flush()
}
}
}
示例2: TestChannelUnregister
func TestChannelUnregister(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
tcpAddr, httpAddr := mustStartLookupd()
defer lookupd.Exit()
topics := lookupd.DB.FindRegistrations("topic", "*", "*")
assert.Equal(t, len(topics), 0)
topicName := "channel_unregister"
conn := mustConnectLookupd(t, tcpAddr)
tcpPort := 5000
httpPort := 5555
identify(t, conn, "ip.address", tcpPort, httpPort, "fake-version")
nsq.Register(topicName, "ch1").Write(conn)
v, err := nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
assert.Equal(t, v, []byte("OK"))
topics = lookupd.DB.FindRegistrations("topic", topicName, "")
assert.Equal(t, len(topics), 1)
channels := lookupd.DB.FindRegistrations("channel", topicName, "*")
assert.Equal(t, len(channels), 1)
nsq.UnRegister(topicName, "ch1").Write(conn)
v, err = nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
assert.Equal(t, v, []byte("OK"))
topics = lookupd.DB.FindRegistrations("topic", topicName, "")
assert.Equal(t, len(topics), 1)
// we should still have mention of the topic even though there is no producer
// (ie. we haven't *deleted* the channel, just unregistered as a producer)
channels = lookupd.DB.FindRegistrations("channel", topicName, "*")
assert.Equal(t, len(channels), 1)
endpoint := fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
data, err := nsq.ApiRequest(endpoint)
assert.Equal(t, err, nil)
returnedProducers, err := data.Get("producers").Array()
assert.Equal(t, err, nil)
assert.Equal(t, len(returnedProducers), 1)
}
示例3: TestTombstonedNodes
func TestTombstonedNodes(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
tcpAddr, httpAddr := mustStartLookupd()
defer lookupd.Exit()
lookupd.inactiveProducerTimeout = 50 * time.Millisecond
lookupdHTTPAddrs := []string{fmt.Sprintf("%s", httpAddr)}
topicName := "inactive_nodes"
conn := mustConnectLookupd(t, tcpAddr)
identify(t, conn, "ip.address", 5000, 5555, "fake-version")
nsq.Register(topicName, "channel1").Write(conn)
_, err := nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
producers, _ := lookuputil.GetLookupdProducers(lookupdHTTPAddrs)
assert.Equal(t, len(producers), 1)
assert.Equal(t, len(producers[0].Topics), 1)
assert.Equal(t, producers[0].Topics[0].Topic, topicName)
assert.Equal(t, producers[0].Topics[0].Tombstoned, false)
endpoint := fmt.Sprintf("http://%s/tombstone_topic_producer?topic=%s&node=%s", httpAddr, topicName, "ip.address:5555")
_, err = nsq.ApiRequest(endpoint)
assert.Equal(t, err, nil)
producers, _ = lookuputil.GetLookupdProducers(lookupdHTTPAddrs)
assert.Equal(t, len(producers), 1)
assert.Equal(t, len(producers[0].Topics), 1)
assert.Equal(t, producers[0].Topics[0].Topic, topicName)
assert.Equal(t, producers[0].Topics[0].Tombstoned, true)
}
示例4: subFail
func subFail(t *testing.T, conn net.Conn, topicName string, channelName string) {
err := nsq.Subscribe(topicName, channelName).Write(conn)
assert.Equal(t, err, nil)
resp, err := nsq.ReadResponse(conn)
frameType, _, err := nsq.UnpackResponse(resp)
assert.Equal(t, frameType, nsq.FrameTypeError)
}
示例5: TestBasicV2
// exercise the basic operations of the V2 protocol
func TestBasicV2(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
options := NewNsqdOptions()
options.clientTimeout = 60 * time.Second
tcpAddr, _ := mustStartNSQd(options)
defer nsqd.Exit()
topicName := "test_v2" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
msg := nsq.NewMessage(<-nsqd.idChan, []byte("test body"))
topic.PutMessage(msg)
conn, err := mustConnectNSQd(tcpAddr)
assert.Equal(t, err, nil)
identify(t, conn)
sub(t, conn, topicName, "ch")
err = nsq.Ready(1).Write(conn)
assert.Equal(t, err, nil)
resp, err := nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
frameType, data, err := nsq.UnpackResponse(resp)
msgOut, _ := nsq.DecodeMessage(data)
assert.Equal(t, frameType, nsq.FrameTypeMessage)
assert.Equal(t, msgOut.Id, msg.Id)
assert.Equal(t, msgOut.Body, msg.Body)
assert.Equal(t, msgOut.Attempts, uint16(1))
}
示例6: TestInactiveNodes
func TestInactiveNodes(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
tcpAddr, httpAddr, nsqlookupd := mustStartLookupd()
defer nsqlookupd.Exit()
nsqlookupd.inactiveProducerTimeout = 50 * time.Millisecond
lookupdHTTPAddrs := []string{fmt.Sprintf("%s", httpAddr)}
topicName := "inactive_nodes"
conn := mustConnectLookupd(t, tcpAddr)
identify(t, conn, "ip.address", 5000, 5555, "fake-version")
nsq.Register(topicName, "channel1").Write(conn)
_, err := nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
producers, _ := lookuputil.GetLookupdProducers(lookupdHTTPAddrs)
assert.Equal(t, len(producers), 1)
assert.Equal(t, len(producers[0].Topics), 1)
assert.Equal(t, producers[0].Topics[0].Topic, topicName)
assert.Equal(t, producers[0].Topics[0].Tombstoned, false)
time.Sleep(55 * time.Millisecond)
producers, _ = lookuputil.GetLookupdProducers(lookupdHTTPAddrs)
assert.Equal(t, len(producers), 0)
}
示例7: pubWorker
func pubWorker(n int, tcpAddr string, batchSize int, batch [][]byte, topic string) {
conn, err := net.DialTimeout("tcp", tcpAddr, time.Second)
if err != nil {
panic(err.Error())
}
conn.Write(nsq.MagicV2)
rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
num := n / runtime.GOMAXPROCS(0) / batchSize
for i := 0; i < num; i += 1 {
cmd, _ := nsq.MultiPublish(topic, batch)
err := cmd.Write(rw)
if err != nil {
panic(err.Error())
}
err = rw.Flush()
if err != nil {
panic(err.Error())
}
resp, err := nsq.ReadResponse(rw)
if err != nil {
panic(err.Error())
}
_, data, _ := nsq.UnpackResponse(resp)
if !bytes.Equal(data, []byte("OK")) {
panic("invalid response")
}
}
}
示例8: TestTombstoneRecover
func TestTombstoneRecover(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
tcpAddr, httpAddr := mustStartLookupd()
defer lookupd.Exit()
lookupd.tombstoneLifetime = 50 * time.Millisecond
topicName := "tombstone_recover"
topicName2 := topicName + "2"
conn := mustConnectLookupd(t, tcpAddr)
identify(t, conn, "ip.address", 5000, 5555, "fake-version")
nsq.Register(topicName, "channel1").Write(conn)
_, err := nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
nsq.Register(topicName2, "channel2").Write(conn)
_, err = nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
endpoint := fmt.Sprintf("http://%s/tombstone_topic_producer?topic=%s&node=%s", httpAddr, topicName, "ip.address:5555")
_, err = nsq.ApiRequest(endpoint)
assert.Equal(t, err, nil)
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
data, err := nsq.ApiRequest(endpoint)
assert.Equal(t, err, nil)
producers, _ := data.Get("producers").Array()
assert.Equal(t, len(producers), 0)
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName2)
data, err = nsq.ApiRequest(endpoint)
assert.Equal(t, err, nil)
producers, _ = data.Get("producers").Array()
assert.Equal(t, len(producers), 1)
time.Sleep(55 * time.Millisecond)
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
data, err = nsq.ApiRequest(endpoint)
assert.Equal(t, err, nil)
producers, _ = data.Get("producers").Array()
assert.Equal(t, len(producers), 1)
}
示例9: readValidate
func readValidate(t *testing.T, conn net.Conn, f int32, d string) {
resp, err := nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
frameType, data, err := nsq.UnpackResponse(resp)
assert.Equal(t, err, nil)
assert.Equal(t, frameType, f)
assert.Equal(t, string(data), d)
}
示例10: readValidateOK
func readValidateOK(t *testing.T, conn net.Conn) {
resp, err := nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
frameType, data, err := nsq.UnpackResponse(resp)
assert.Equal(t, err, nil)
assert.Equal(t, frameType, nsq.FrameTypeResponse)
assert.Equal(t, data, []byte("OK"))
}
示例11: identify
func identify(t *testing.T, conn net.Conn, address string, tcpPort int, httpPort int, version string) {
ci := make(map[string]interface{})
ci["tcp_port"] = tcpPort
ci["http_port"] = httpPort
ci["address"] = address //TODO: remove for 1.0
ci["broadcast_address"] = address
ci["hostname"] = address
ci["version"] = version
cmd, _ := nsq.Identify(ci)
err := cmd.Write(conn)
assert.Equal(t, err, nil)
_, err = nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
}
示例12: identifyFeatureNegotiation
func identifyFeatureNegotiation(t *testing.T, conn net.Conn) []byte {
ci := make(map[string]interface{})
ci["short_id"] = "test"
ci["long_id"] = "test"
ci["feature_negotiation"] = true
cmd, _ := nsq.Identify(ci)
err := cmd.Write(conn)
assert.Equal(t, err, nil)
resp, err := nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
frameType, data, err := nsq.UnpackResponse(resp)
assert.Equal(t, err, nil)
assert.Equal(t, frameType, nsq.FrameTypeResponse)
return data
}
示例13: readLoop
func (c *rwcClient) readLoop() {
for {
resp, err := nsq.ReadResponse(c)
if err != nil {
log.Printf("ReadResponse returned: %v", err)
break
}
_, resp, err = nsq.UnpackResponse(resp)
if bytes.Equal(resp, []byte("_heartbeat_")) {
c.write <- nsq.Nop()
continue
}
c.read <- string(resp)
}
}
示例14: TestMultipleConsumerV2
func TestMultipleConsumerV2(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
msgChan := make(chan *nsq.Message)
options := NewNsqdOptions()
options.clientTimeout = 60 * time.Second
tcpAddr, _ := mustStartNSQd(options)
defer nsqd.Exit()
topicName := "test_multiple_v2" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
msg := nsq.NewMessage(<-nsqd.idChan, []byte("test body"))
topic.GetChannel("ch1")
topic.GetChannel("ch2")
topic.PutMessage(msg)
for _, i := range []string{"1", "2"} {
conn, err := mustConnectNSQd(tcpAddr)
assert.Equal(t, err, nil)
identify(t, conn)
sub(t, conn, topicName, "ch"+i)
err = nsq.Ready(1).Write(conn)
assert.Equal(t, err, nil)
go func(c net.Conn) {
resp, _ := nsq.ReadResponse(c)
_, data, _ := nsq.UnpackResponse(resp)
msg, _ := nsq.DecodeMessage(data)
msgChan <- msg
}(conn)
}
msgOut := <-msgChan
assert.Equal(t, msgOut.Id, msg.Id)
assert.Equal(t, msgOut.Body, msg.Body)
assert.Equal(t, msgOut.Attempts, uint16(1))
msgOut = <-msgChan
assert.Equal(t, msgOut.Id, msg.Id)
assert.Equal(t, msgOut.Body, msg.Body)
assert.Equal(t, msgOut.Attempts, uint16(1))
}
示例15: TestBasicLookupd
func TestBasicLookupd(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
tcpAddr, httpAddr := mustStartLookupd()
defer lookupd.Exit()
topics := lookupd.DB.FindRegistrations("topic", "*", "*")
assert.Equal(t, len(topics), 0)
topicName := "connectmsg"
conn := mustConnectLookupd(t, tcpAddr)
tcpPort := 5000
httpPort := 5555
identify(t, conn, "ip.address", tcpPort, httpPort, "fake-version")
nsq.Register(topicName, "channel1").Write(conn)
v, err := nsq.ReadResponse(conn)
assert.Equal(t, err, nil)
assert.Equal(t, v, []byte("OK"))
endpoint := fmt.Sprintf("http://%s/nodes", httpAddr)
data, err := nsq.ApiRequest(endpoint)
log.Printf("got %v", data)
returnedProducers, err := data.Get("producers").Array()
assert.Equal(t, err, nil)
assert.Equal(t, len(returnedProducers), 1)
topics = lookupd.DB.FindRegistrations("topic", topicName, "")
assert.Equal(t, len(topics), 1)
producers := lookupd.DB.FindProducers("topic", topicName, "")
assert.Equal(t, len(producers), 1)
producer := producers[0]
assert.Equal(t, producer.peerInfo.Address, "ip.address") //TODO: remove for 1.0
assert.Equal(t, producer.peerInfo.BroadcastAddress, "ip.address")
assert.Equal(t, producer.peerInfo.Hostname, "ip.address")
assert.Equal(t, producer.peerInfo.TcpPort, tcpPort)
assert.Equal(t, producer.peerInfo.HttpPort, httpPort)
endpoint = fmt.Sprintf("http://%s/topics", httpAddr)
data, err = nsq.ApiRequest(endpoint)
assert.Equal(t, err, nil)
returnedTopics, err := data.Get("topics").Array()
log.Printf("got returnedTopics %v", returnedTopics)
assert.Equal(t, err, nil)
assert.Equal(t, len(returnedTopics), 1)
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
data, err = nsq.ApiRequest(endpoint)
assert.Equal(t, err, nil)
returnedChannels, err := data.Get("channels").Array()
assert.Equal(t, err, nil)
assert.Equal(t, len(returnedChannels), 1)
returnedProducers, err = data.Get("producers").Array()
log.Printf("got returnedProducers %v", returnedProducers)
assert.Equal(t, err, nil)
assert.Equal(t, len(returnedProducers), 1)
for i := range returnedProducers {
producer := data.Get("producers").GetIndex(i)
log.Printf("producer %v", producer)
assert.Equal(t, err, nil)
port, err := producer.Get("tcp_port").Int()
assert.Equal(t, err, nil)
assert.Equal(t, port, tcpPort)
port, err = producer.Get("http_port").Int()
assert.Equal(t, err, nil)
assert.Equal(t, port, httpPort)
address, err := producer.Get("address").String() //TODO: remove for 1.0
broadcastaddress, err := producer.Get("broadcast_address").String()
assert.Equal(t, err, nil)
assert.Equal(t, address, "ip.address")
assert.Equal(t, broadcastaddress, "ip.address")
ver, err := producer.Get("version").String()
assert.Equal(t, err, nil)
assert.Equal(t, ver, "fake-version")
}
conn.Close()
time.Sleep(10 * time.Millisecond)
// now there should be no producers, but still topic/channel entries
data, err = nsq.ApiRequest(endpoint)
assert.Equal(t, err, nil)
returnedChannels, err = data.Get("channels").Array()
assert.Equal(t, err, nil)
assert.Equal(t, len(returnedChannels), 1)
returnedProducers, err = data.Get("producers").Array()
assert.Equal(t, err, nil)
assert.Equal(t, len(returnedProducers), 0)
}