topic_discoverer.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package main
  2. import (
  3. "os"
  4. "regexp"
  5. "sync"
  6. "time"
  7. "github.com/nsqio/go-nsq"
  8. "github.com/nsqio/nsq/internal/clusterinfo"
  9. "github.com/nsqio/nsq/internal/http_api"
  10. "github.com/nsqio/nsq/internal/lg"
  11. )
  12. type TopicDiscoverer struct {
  13. logf lg.AppLogFunc
  14. opts *Options
  15. ci *clusterinfo.ClusterInfo
  16. topics map[string]*FileLogger
  17. hupChan chan os.Signal
  18. termChan chan os.Signal
  19. wg sync.WaitGroup
  20. cfg *nsq.Config
  21. }
  22. func newTopicDiscoverer(logf lg.AppLogFunc, opts *Options, cfg *nsq.Config, hupChan chan os.Signal, termChan chan os.Signal) *TopicDiscoverer {
  23. client := http_api.NewClient(nil, opts.HTTPClientConnectTimeout, opts.HTTPClientRequestTimeout)
  24. return &TopicDiscoverer{
  25. logf: logf,
  26. opts: opts,
  27. ci: clusterinfo.New(nil, client),
  28. topics: make(map[string]*FileLogger),
  29. hupChan: hupChan,
  30. termChan: termChan,
  31. cfg: cfg,
  32. }
  33. }
  34. func (t *TopicDiscoverer) updateTopics(topics []string) {
  35. for _, topic := range topics {
  36. if _, ok := t.topics[topic]; ok {
  37. continue
  38. }
  39. if !t.isTopicAllowed(topic) {
  40. t.logf(lg.WARN, "skipping topic %s (doesn't match pattern %s)", topic, t.opts.TopicPattern)
  41. continue
  42. }
  43. fl, err := NewFileLogger(t.logf, t.opts, topic, t.cfg)
  44. if err != nil {
  45. t.logf(lg.ERROR, "couldn't create logger for new topic %s: %s", topic, err)
  46. continue
  47. }
  48. t.topics[topic] = fl
  49. t.wg.Add(1)
  50. go func(fl *FileLogger) {
  51. fl.router()
  52. t.wg.Done()
  53. }(fl)
  54. }
  55. }
  56. func (t *TopicDiscoverer) run() {
  57. var ticker <-chan time.Time
  58. if len(t.opts.Topics) == 0 {
  59. ticker = time.Tick(t.opts.TopicRefreshInterval)
  60. }
  61. t.updateTopics(t.opts.Topics)
  62. forloop:
  63. for {
  64. select {
  65. case <-ticker:
  66. newTopics, err := t.ci.GetLookupdTopics(t.opts.NSQLookupdHTTPAddrs)
  67. if err != nil {
  68. t.logf(lg.ERROR, "could not retrieve topic list: %s", err)
  69. continue
  70. }
  71. t.updateTopics(newTopics)
  72. case <-t.termChan:
  73. for _, fl := range t.topics {
  74. close(fl.termChan)
  75. }
  76. break forloop
  77. case <-t.hupChan:
  78. for _, fl := range t.topics {
  79. fl.hupChan <- true
  80. }
  81. }
  82. }
  83. t.wg.Wait()
  84. }
  85. func (t *TopicDiscoverer) isTopicAllowed(topic string) bool {
  86. if t.opts.TopicPattern == "" {
  87. return true
  88. }
  89. match, err := regexp.MatchString(t.opts.TopicPattern, topic)
  90. if err != nil {
  91. return false
  92. }
  93. return match
  94. }