// start service. func StartService(file string) { r := &Raper{ // cache domain binding data. domainBindPath: make(map[string][]string), // save domain Tags for each domain totalDomainTags: make(map[string][]string), // save each domain's message. aggreMessageChan: make(chan *AggregationData, 4096), // chan used to upload data message to tsdb aggregatedPath: make(map[string][]string), // tsdb pipe line tsdb: tsdb.NewPipeLine(), } defer r.Close() r.subscriber = sub.NewSub() r.subscriber.GetMessage() r.producer = kafka.NewPub() go r.producer.PublishToKafka() domainBindChan := getDomainPath() c := <-domainBindChan r.saveBindData(c) logger.Println("finish saving bind path for the first time") logger.Println("finish connecting to tsdb for the first time") go r.distDomainMertic() go r.createMetricTags(file) signals := make(chan os.Signal, 1) signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT) for { select { case c := <-domainBindChan: go r.saveBindData(c) case m := <-r.subscriber.LogsChan: r.analyzeMessage(m) case _ = <-signals: r.Kill(nil) r.Close() } } }
func (s *Subscriber) GetMessage() { logger.Println("start to receive message from gnatsd") s.LogsChan = make(chan LogJsonObj, 4096) s.logsChan = make(chan []byte, 4096) go s.unmarshal() s.NatsConn.QueueSubscribe(s.Topic, s.Group, func(m *nats.Msg) { s.logsChan <- m.Data }) }
func (r *Raper) saveBindData(c DomainBindInfo) { var newBindData map[string][]string = make(map[string][]string) for _, v1 := range c.Data { for _, v2 := range v1 { domain := v2.Domain path := v2.Path if ok := utils.StrInStrings(path, newBindData[domain]); !ok { newBindData[domain] = append(newBindData[domain], path) } } } r.mutexBindPath.Lock() if ok := utils.MapStrStringsCmp(r.domainBindPath, newBindData); !ok { logger.Println("save new domain bind data") r.domainBindPath = newBindData } r.mutexBindPath.Unlock() var domains []string for k := range r.domainBindPath { domains = append(domains, k) } for _, d := range domains { r.mutexBindPath.RLock() a := make([]string, len(r.domainBindPath[d])) copy(a, r.domainBindPath[d]) r.mutexBindPath.RUnlock() bcc := sorted.ByCharCount(a, "/") sort.Sort(bcc) r.mutexBindPath.Lock() r.domainBindPath[d] = bcc.List r.mutexBindPath.Unlock() } }
func usage() { logger.Println("Usage: calculator [-c config_file]") os.Exit(0) }
func (r *Raper) Close() { logger.Println("close all sockets here and exit") r.subscriber.Close() r.tsdb.Close() os.Exit(0) }