Skip to content

Instantly share code, notes, and snippets.

@judwhite

judwhite/main.go Secret

Created May 9, 2016 07:33
Show Gist options
  • Save judwhite/2f172095f9039dd63520a8c421f15b71 to your computer and use it in GitHub Desktop.
Save judwhite/2f172095f9039dd63520a8c421f15b71 to your computer and use it in GitHub Desktop.
// TODO: need to supply your own startService and stopService functions
package main
import (
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/nsqio/go-nsq"
)
func main() {
stopService("nsqd")
cfg := nsq.NewConfig()
p, err := nsq.NewProducer("127.0.0.1:4150", cfg)
if err != nil {
log.Fatal(err)
}
closeChan := make(chan struct{})
var wg sync.WaitGroup
wg.Add(2)
go func() {
msg := []byte("nsq")
i := 0
loop:
for {
if i%10000 == 0 {
log.Printf("%d successful messages\n", i)
}
select {
case <-closeChan:
break loop
default:
pubErr := p.Publish("hello", msg)
if pubErr != nil {
if pubErr.Error() != "not connected" {
log.Println(pubErr)
}
} else {
i++
}
}
}
wg.Done()
}()
go func() {
loop2:
for {
select {
case <-time.After(5 * time.Second):
stopService("nsqd")
//time.Sleep(5 * time.Second)
startService("nsqd")
case <-closeChan:
break loop2
}
}
wg.Done()
}()
notify := make(chan os.Signal, 1)
signal.Notify(notify, syscall.SIGINT, syscall.SIGTERM)
<-notify
log.Println("Exiting...")
p.Stop()
close(closeChan)
wg.Wait()
stopService("nsqd")
log.Println("Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment