Skip to content

Instantly share code, notes, and snippets.

@larzconwell
Created July 20, 2016 13:49
Show Gist options
  • Save larzconwell/767daa616e4718dc2d4bc0b3e4495599 to your computer and use it in GitHub Desktop.
Save larzconwell/767daa616e4718dc2d4bc0b3e4495599 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"sync"
nsq "github.com/bitly/go-nsq"
)
const (
addr = "127.0.0.1:4150"
topic = "ex_topic"
batches = 10000
)
var (
message = []byte("example")
messages = [][]byte{
message, message,
message, message,
message, message,
message, message,
message, message,
}
)
type Logger struct{}
func (Logger) Output(c int, s string) error {
log.Println(s)
return nil
}
func main() {
config := nsq.NewConfig()
producer, err := nsq.NewProducer(addr, config)
if err != nil {
log.Println(err)
return
}
producer.SetLogger(new(Logger), nsq.LogLevelInfo)
defer producer.Stop()
var wg sync.WaitGroup
defer wg.Wait()
wg.Add(batches)
for i := 0; i < batches; i++ {
go func() {
defer wg.Done()
err = producer.MultiPublish(topic, messages)
if err != nil {
log.Println(err)
}
}()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment