Exemple #1
0
func main() {
	consumer := consumer.New(configure())

	for event := range consumer.Start() {
		log.Debug("event", log.Data{"event": event})

		err := consumer.Commit(event)
		if err != nil {
			log.Error(err, nil)
		}
	}
}
Exemple #2
0
func (s *service) Start() {
	chain := alice.New(s.middleware()...).Then(s.router)

	bindAddr := s.config.BindAddr()
	certFile, keyFile := s.config.CertFile(), s.config.KeyFile()

	if len(certFile) > 0 && len(keyFile) > 0 {
		log.Debug("listening tls", log.Data{"addr": bindAddr, "cert": certFile, "key": keyFile})
		err := http.ListenAndServeTLS(bindAddr, certFile, keyFile, chain)
		if err != nil {
			log.Error(err, nil)
			os.Exit(1)
		}
		return
	}

	log.Debug("listening", log.Data{"addr": bindAddr})
	err := http.ListenAndServe(bindAddr, chain)
	if err != nil {
		log.Error(err, nil)
		os.Exit(1)
	}
}
Exemple #3
0
func configure() config {
	if cfg != nil {
		return *cfg
	}

	cfg = new(config)
	if err := gofigure.Gofigure(cfg); err != nil {
		log.Error(err, nil)
		os.Exit(1)
	}

	log.Namespace = cfg.Namespace()

	return *cfg
}
Exemple #4
0
func (kc *kafkaConsumer) Start() chan Message {
	kc.sigChan = make(chan os.Signal, 1)
	msgChan := make(chan Message, 1)

	signal.Notify(kc.sigChan, os.Interrupt)
	go func() {
		<-kc.sigChan
		kc.consumerGroup.Close()
		close(msgChan)
	}()

	cfg := consumergroup.NewConfig()

	cfg.Offsets.Initial = kc.Config.InitialOffset()
	cfg.Offsets.ProcessingTimeout = kc.Config.ProcessingTimeout()

	var zookeeperNodes []string
	url := kc.Config.ZookeeperURL()
	if chroot := kc.Config.ZookeeperChroot(); len(chroot) > 0 {
		url += "/" + chroot
	}
	zookeeperNodes, cfg.Zookeeper.Chroot = kazoo.ParseConnectionString(url)

	var cg *consumergroup.ConsumerGroup
	var err error
	var attempts, curExp int

	for {
		attempts++
		cg, err = consumergroup.JoinConsumerGroup(
			kc.Config.ConsumerGroup(),
			kc.Config.Topics(),
			zookeeperNodes,
			cfg,
		)
		if err != nil {
			log.Error(err, nil)
			if attempts > maxAttempts {
				log.Debug("reached maximum attempts, exiting", nil)
				os.Exit(1)
			}
			if curExp == 0 {
				curExp = 2
			}
			curExp *= 2
			if curExp > maxExp {
				curExp = maxExp
			}
			log.Debug("sleeping", log.Data{"ms": curExp})
			time.Sleep(time.Millisecond * time.Duration(curExp))
			continue
		}
		break
	}

	kc.consumerGroup = cg

	go func() {
		for err := range cg.Errors() {
			log.Error(err, nil)
		}
	}()

	go func() {
		log.Debug("waiting for messages", nil)
		for m := range cg.Messages() {
			log.Debug("message", log.Data{"msg": m})
			msgChan <- saramaMessage{m}
		}
	}()

	return msgChan
}