本文整理汇总了Golang中github.com/bitly/nsq/nsq.NewMessage函数的典型用法代码示例。如果您正苦于以下问题:Golang NewMessage函数的具体用法?Golang NewMessage怎么用?Golang NewMessage使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewMessage函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestChannelEmptyConsumer
func TestChannelEmptyConsumer(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
tcpAddr, _, nsqd := mustStartNSQd(NewNsqdOptions())
defer nsqd.Exit()
conn, _ := mustConnectNSQd(tcpAddr)
topicName := "test_channel_empty" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
channel := topic.GetChannel("channel")
client := NewClientV2(0, conn, &Context{nsqd})
client.SetReadyCount(25)
channel.AddClient(client.ID, client)
for i := 0; i < 25; i++ {
msg := nsq.NewMessage(<-nsqd.idChan, []byte("test"))
channel.StartInFlightTimeout(msg, 0)
client.SendingMessage()
}
for _, cl := range channel.clients {
stats := cl.Stats()
assert.Equal(t, stats.InFlightCount, int64(25))
}
channel.Empty()
for _, cl := range channel.clients {
stats := cl.Stats()
assert.Equal(t, stats.InFlightCount, int64(0))
}
}
示例2: 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))
}
示例3: TestChannelEmpty
func TestChannelEmpty(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
nsqd := NewNSQd(1, NewNsqdOptions())
defer nsqd.Exit()
topicName := "test_channel_empty" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
channel := topic.GetChannel("channel")
msgs := make([]*nsq.Message, 0, 25)
for i := 0; i < 25; i++ {
msg := nsq.NewMessage(<-nsqd.idChan, []byte("test"))
channel.StartInFlightTimeout(msg, 0)
msgs = append(msgs, msg)
}
channel.RequeueMessage(0, msgs[len(msgs)-1].Id, 100*time.Millisecond)
assert.Equal(t, len(channel.inFlightMessages), 24)
assert.Equal(t, len(channel.inFlightPQ), 24)
assert.Equal(t, len(channel.deferredMessages), 1)
assert.Equal(t, len(channel.deferredPQ), 1)
channel.Empty()
assert.Equal(t, len(channel.inFlightMessages), 0)
assert.Equal(t, len(channel.inFlightPQ), 0)
assert.Equal(t, len(channel.deferredMessages), 0)
assert.Equal(t, len(channel.deferredPQ), 0)
assert.Equal(t, channel.Depth(), int64(0))
}
示例4: TestInFlightWorker
func TestInFlightWorker(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
options := NewNsqdOptions()
options.msgTimeout = 200 * time.Millisecond
nsqd := NewNSQd(1, options)
defer nsqd.Exit()
topicName := "test_in_flight_worker" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
channel := topic.GetChannel("channel")
for i := 0; i < 1000; i++ {
msg := nsq.NewMessage(<-nsqd.idChan, []byte("test"))
channel.StartInFlightTimeout(msg, 0)
}
assert.Equal(t, len(channel.inFlightMessages), 1000)
assert.Equal(t, len(channel.inFlightPQ), 1000)
// the in flight worker has a resolution of 100ms so we need to wait
// at least that much longer than our msgTimeout (in worst case)
time.Sleep(options.msgTimeout + 100*time.Millisecond)
assert.Equal(t, len(channel.inFlightMessages), 0)
assert.Equal(t, len(channel.inFlightPQ), 0)
}
示例5: putHandler
func putHandler(w http.ResponseWriter, req *http.Request) {
reqParams, err := util.NewReqParams(req)
if err != nil {
log.Printf("ERROR: failed to parse request params - %s", err.Error())
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
return
}
topicName, err := reqParams.Get("topic")
if err != nil {
util.ApiResponse(w, 500, "MISSING_ARG_TOPIC", nil)
return
}
if !nsq.IsValidTopicName(topicName) {
util.ApiResponse(w, 500, "INVALID_ARG_TOPIC", nil)
return
}
if int64(len(reqParams.Body)) > nsqd.options.maxMessageSize {
util.ApiResponse(w, 500, "MSG_TOO_BIG", nil)
return
}
topic := nsqd.GetTopic(topicName)
msg := nsq.NewMessage(<-nsqd.idChan, reqParams.Body)
err = topic.PutMessage(msg)
if err != nil {
util.ApiResponse(w, 500, "NOK", nil)
return
}
w.Header().Set("Content-Length", "2")
io.WriteString(w, "OK")
}
示例6: TestInFlightWorker
func TestInFlightWorker(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
options := NewNsqdOptions()
options.msgTimeout = 300 * time.Millisecond
nsqd = NewNSQd(1, options)
defer nsqd.Exit()
topicName := "test_in_flight_worker" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
channel := topic.GetChannel("channel")
for i := 0; i < 1000; i++ {
msg := nsq.NewMessage(<-nsqd.idChan, []byte("test"))
channel.StartInFlightTimeout(msg, NewClientV2(nil))
}
assert.Equal(t, len(channel.inFlightMessages), 1000)
assert.Equal(t, len(channel.inFlightPQ), 1000)
time.Sleep(350 * time.Millisecond)
assert.Equal(t, len(channel.inFlightMessages), 0)
assert.Equal(t, len(channel.inFlightPQ), 0)
}
示例7: BenchmarkTopicToChannelPut
func BenchmarkTopicToChannelPut(b *testing.B) {
b.StopTimer()
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
topicName := "bench_topic_to_channel_put" + strconv.Itoa(b.N)
channelName := "bench"
options := NewNsqdOptions()
options.memQueueSize = int64(b.N)
nsqd := NewNSQd(1, options)
defer nsqd.Exit()
channel := nsqd.GetTopic(topicName).GetChannel(channelName)
b.StartTimer()
for i := 0; i <= b.N; i++ {
topic := nsqd.GetTopic(topicName)
msg := nsq.NewMessage(<-nsqd.idChan, []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaa"))
topic.PutMessage(msg)
}
for {
if len(channel.memoryMsgChan) == b.N {
break
}
runtime.Gosched()
}
}
示例8: TestStats
func TestStats(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
options := NewNsqdOptions()
tcpAddr, _, nsqd := mustStartNSQd(options)
defer nsqd.Exit()
topicName := "test_stats" + 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")
stats := nsqd.getStats()
assert.Equal(t, len(stats), 1)
assert.Equal(t, len(stats[0].Channels), 1)
assert.Equal(t, len(stats[0].Channels[0].Clients), 1)
log.Printf("stats: %+v", stats)
}
示例9: messagePump
// messagePump selects over the in-memory and backend queue and
// writes messages to every channel for this topic
func (t *Topic) messagePump() {
var msg *nsq.Message
var buf []byte
var err error
for {
// do an extra check for exit before we select on all the memory/backend/exitChan
// this solves the case where we are closed and something else is writing into
// backend. we don't want to reverse that
if atomic.LoadInt32(&t.exitFlag) == 1 {
goto exit
}
select {
case msg = <-t.memoryMsgChan:
case buf = <-t.backend.ReadChan():
msg, err = nsq.DecodeMessage(buf)
if err != nil {
log.Printf("ERROR: failed to decode message - %s", err.Error())
continue
}
case <-t.exitChan:
goto exit
}
t.RLock()
// check if all the channels have been deleted
if len(t.channelMap) == 0 {
// put this message back on the queue
// we need to background because we currently hold the lock
go func() {
t.PutMessage(msg)
}()
// reset the sync.Once
t.messagePumpStarter = new(sync.Once)
t.RUnlock()
goto exit
}
for _, channel := range t.channelMap {
// copy the message because each channel
// needs a unique instance
chanMsg := nsq.NewMessage(msg.Id, msg.Body)
chanMsg.Timestamp = msg.Timestamp
err := channel.PutMessage(chanMsg)
if err != nil {
log.Printf("TOPIC(%s) ERROR: failed to put msg(%s) to channel(%s) - %s", t.name, msg.Id, channel.name, err.Error())
}
}
t.RUnlock()
}
exit:
log.Printf("TOPIC(%s): closing ... messagePump", t.name)
}
示例10: BenchmarkTopicPut
func BenchmarkTopicPut(b *testing.B) {
b.StopTimer()
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
topicName := "bench_topic_put" + strconv.Itoa(b.N)
options := NewNsqdOptions()
options.memQueueSize = int64(b.N)
nsqd := NewNSQd(1, options)
defer nsqd.Exit()
b.StartTimer()
for i := 0; i <= b.N; i++ {
topic := nsqd.GetTopic(topicName)
msg := nsq.NewMessage(<-nsqd.idChan, []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaa"))
topic.PutMessage(msg)
}
}
示例11: 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))
}
示例12: mputHandler
func (s *httpServer) mputHandler(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
return
}
reqParams, err := util.NewReqParams(req)
if err != nil {
log.Printf("ERROR: failed to parse request params - %s", err.Error())
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
return
}
topicName, err := reqParams.Get("topic")
if err != nil {
util.ApiResponse(w, 500, "MISSING_ARG_TOPIC", nil)
return
}
if !nsq.IsValidTopicName(topicName) {
util.ApiResponse(w, 500, "INVALID_ARG_TOPIC", nil)
return
}
topic := s.context.nsqd.GetTopic(topicName)
for _, block := range bytes.Split(reqParams.Body, []byte("\n")) {
if len(block) != 0 {
if int64(len(reqParams.Body)) > s.context.nsqd.options.maxMessageSize {
util.ApiResponse(w, 500, "MSG_TOO_BIG", nil)
return
}
msg := nsq.NewMessage(<-s.context.nsqd.idChan, block)
err := topic.PutMessage(msg)
if err != nil {
util.ApiResponse(w, 500, "NOK", nil)
return
}
}
}
w.Header().Set("Content-Length", "2")
io.WriteString(w, "OK")
}
示例13: TestPutMessage
// ensure that we can push a message through a topic and get it out of a channel
func TestPutMessage(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
nsqd := NewNSQd(1, NewNsqdOptions())
defer nsqd.Exit()
topicName := "test_put_message" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
channel1 := topic.GetChannel("ch")
var id nsq.MessageID
msg := nsq.NewMessage(id, []byte("test"))
topic.PutMessage(msg)
outputMsg := <-channel1.clientMsgChan
assert.Equal(t, msg.Id, outputMsg.Id)
assert.Equal(t, msg.Body, outputMsg.Body)
}
示例14: TestEphemeralChannel
func TestEphemeralChannel(t *testing.T) {
// a normal channel sticks around after clients disconnect; an ephemeral channel is
// lazily removed after the last client disconnects
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
options := NewNsqdOptions()
options.memQueueSize = 100
_, _, nsqd := mustStartNSQd(options)
topicName := "ephemeral_test" + strconv.Itoa(int(time.Now().Unix()))
doneExitChan := make(chan int)
exitChan := make(chan int)
go func() {
<-exitChan
nsqd.Exit()
doneExitChan <- 1
}()
body := []byte("an_ephemeral_message")
topic := nsqd.GetTopic(topicName)
ephemeralChannel := topic.GetChannel("ch1#ephemeral")
client := NewClientV2(0, nil, &Context{nsqd})
ephemeralChannel.AddClient(client.ID, client)
msg := nsq.NewMessage(<-nsqd.idChan, body)
topic.PutMessage(msg)
msg = <-ephemeralChannel.clientMsgChan
assert.Equal(t, msg.Body, body)
log.Printf("pulling from channel")
ephemeralChannel.RemoveClient(client.ID)
time.Sleep(50 * time.Millisecond)
assert.Equal(t, len(topic.channelMap), 0)
exitChan <- 1
<-doneExitChan
}
示例15: TestDeleteLast
func TestDeleteLast(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
nsqd := NewNSQd(1, NewNsqdOptions())
defer nsqd.Exit()
topic := nsqd.GetTopic("test")
channel1 := topic.GetChannel("ch1")
assert.NotEqual(t, nil, channel1)
err := topic.DeleteExistingChannel("ch1")
assert.Equal(t, nil, err)
assert.Equal(t, 0, len(topic.channelMap))
msg := nsq.NewMessage(<-nsqd.idChan, []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaa"))
err = topic.PutMessage(msg)
time.Sleep(100 * time.Millisecond)
assert.Equal(t, nil, err)
assert.Equal(t, topic.Depth(), int64(1))
}