to_nsq.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // This is an NSQ client that publishes incoming messages from
  2. // stdin to the specified topic.
  3. package main
  4. import (
  5. "bufio"
  6. "flag"
  7. "fmt"
  8. "io"
  9. "log"
  10. "os"
  11. "os/signal"
  12. "sync/atomic"
  13. "syscall"
  14. "time"
  15. "github.com/nsqio/go-nsq"
  16. "github.com/nsqio/nsq/internal/app"
  17. "github.com/nsqio/nsq/internal/version"
  18. )
  19. var (
  20. topic = flag.String("topic", "", "NSQ topic to publish to")
  21. delimiter = flag.String("delimiter", "\n", "character to split input from stdin")
  22. destNsqdTCPAddrs = app.StringArray{}
  23. )
  24. func init() {
  25. flag.Var(&destNsqdTCPAddrs, "nsqd-tcp-address", "destination nsqd TCP address (may be given multiple times)")
  26. }
  27. func main() {
  28. cfg := nsq.NewConfig()
  29. flag.Var(&nsq.ConfigFlag{cfg}, "producer-opt", "option to passthrough to nsq.Producer (may be given multiple times, http://godoc.org/github.com/nsqio/go-nsq#Config)")
  30. rate := flag.Int64("rate", 0, "Throttle messages to n/second. 0 to disable")
  31. flag.Parse()
  32. if len(*topic) == 0 {
  33. log.Fatal("--topic required")
  34. }
  35. if len(*delimiter) != 1 {
  36. log.Fatal("--delimiter must be a single byte")
  37. }
  38. stopChan := make(chan bool)
  39. termChan := make(chan os.Signal, 1)
  40. signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM)
  41. cfg.UserAgent = fmt.Sprintf("to_nsq/%s go-nsq/%s", version.Binary, nsq.VERSION)
  42. // make the producers
  43. producers := make(map[string]*nsq.Producer)
  44. for _, addr := range destNsqdTCPAddrs {
  45. producer, err := nsq.NewProducer(addr, cfg)
  46. if err != nil {
  47. log.Fatalf("failed to create nsq.Producer - %s", err)
  48. }
  49. producers[addr] = producer
  50. }
  51. if len(producers) == 0 {
  52. log.Fatal("--nsqd-tcp-address required")
  53. }
  54. throttleEnabled := *rate >= 1
  55. balance := int64(1)
  56. // avoid divide by 0 if !throttleEnabled
  57. var interval time.Duration
  58. if throttleEnabled {
  59. interval = time.Second / time.Duration(*rate)
  60. }
  61. go func() {
  62. if !throttleEnabled {
  63. return
  64. }
  65. log.Printf("Throttling messages rate to max:%d/second", *rate)
  66. // every tick increase the number of messages we can send
  67. for _ = range time.Tick(interval) {
  68. n := atomic.AddInt64(&balance, 1)
  69. // if we build up more than 1s of capacity just bound to that
  70. if n > int64(*rate) {
  71. atomic.StoreInt64(&balance, int64(*rate))
  72. }
  73. }
  74. }()
  75. r := bufio.NewReader(os.Stdin)
  76. delim := (*delimiter)[0]
  77. go func() {
  78. for {
  79. var err error
  80. if throttleEnabled {
  81. currentBalance := atomic.LoadInt64(&balance)
  82. if currentBalance <= 0 {
  83. time.Sleep(interval)
  84. }
  85. err = readAndPublish(r, delim, producers)
  86. atomic.AddInt64(&balance, -1)
  87. } else {
  88. err = readAndPublish(r, delim, producers)
  89. }
  90. if err != nil {
  91. if err != io.EOF {
  92. log.Fatal(err)
  93. }
  94. close(stopChan)
  95. break
  96. }
  97. }
  98. }()
  99. select {
  100. case <-termChan:
  101. case <-stopChan:
  102. }
  103. for _, producer := range producers {
  104. producer.Stop()
  105. }
  106. }
  107. // readAndPublish reads to the delim from r and publishes the bytes
  108. // to the map of producers.
  109. func readAndPublish(r *bufio.Reader, delim byte, producers map[string]*nsq.Producer) error {
  110. line, readErr := r.ReadBytes(delim)
  111. if len(line) > 0 {
  112. // trim the delimiter
  113. line = line[:len(line)-1]
  114. }
  115. if len(line) == 0 {
  116. return readErr
  117. }
  118. for _, producer := range producers {
  119. err := producer.Publish(*topic, line)
  120. if err != nil {
  121. return err
  122. }
  123. }
  124. return readErr
  125. }