Golang 处理Kafka消息

package main

import (
	"flag"
	"github.com/optiopay/kafka"
	"log"
	"net/http"
	"net/http/pprof"
	"strings"
	"time"
	"ooxx/config"
	"ooxx/lib"
	"ooxx/model"
)

const LOG_CHANNEL_COUNT = 200
const LOG_BUFFER_COUNT = 100

var debug = flag.String("debug", "false", "debug mode")
var queue = make(chan []byte, LOG_CHANNEL_COUNT)
var buffer = make([]string, LOG_BUFFER_COUNT)
var ticker = time.NewTicker(4 * time.Second)

func save_message() {
	if len(buffer) > 0 {
		tm := lib.TimeFormat()
		log := "stats_play_" + tm
		file := config.Config.StatsLogDir + log
		content := ""
		for _, v := range buffer {
			if v == "" {
				continue
			}
			content = content + v + "\n"
		}
		if content != "" {
			lib.FilePutContents2(file, content)
			buffer = buffer[0:0]
		}
	}
}

func push_message() {
	for {
		select {
		case c := <-queue:
			buffer = append(buffer, string(c))
		case <-ticker.C:
			save_message()
		}
	}
}

func consume_flow_message(broker kafka.Client, topic string, partition int) {
	conf := kafka.NewConsumerConf(topic, int32(partition))
	conf.StartOffset = kafka.StartOffsetNewest
	consumer, err := broker.Consumer(conf)
	if err != nil {
		log.Fatalf("cannot create kafka consumer for %s:%d: %s", topic, partition, err)
	}

	for {
		msg, err := consumer.Consume()
		if err != nil {
			if err != kafka.ErrNoData {
				log.Printf("cannot consume %s:%d message: %s", topic, partition, err)
			}
			break
		}

		switch partition {
		case config.Config.KafkaPartitionFlay:
			log.Printf("%s:%d, %d: %s", topic, partition, msg.Offset, msg.Value)
		case config.Config.KafkaPartitionShow:
			log.Printf("%s:%d, %d: %s", topic, partition, msg.Offset, msg.Value)
		case config.Config.KafkaPartitionFlow:
			log.Printf("%s:%d, %d: %s", topic, partition, msg.Offset, msg.Value)
			if len(msg.Value) > 0 {
				queue <- msg.Value
			}
		}

	}
	log.Print("consume_flow_message, consumer quit, %s:%d", topic, partition)
}

func main() {
	defer func() {
		if err := recover(); err != nil {
			lib.P("panic:", err, "\nstack:"+lib.Stack(false))
		}
	}()

	defer model.Db.Close()

	flag.Parse()

	go func() {
		profServeMux := http.NewServeMux()
		profServeMux.HandleFunc("/debug/pprof/", pprof.Index)
		profServeMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
		profServeMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
		profServeMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
		err := http.ListenAndServe(":9527", profServeMux)
		if err != nil {
			panic(err)
		}
	}()

	var kafkaAddrs = strings.Split(config.Config.KafkaBrokers, ",")
	var conf = kafka.NewBrokerConf("xktv")
	conf.DialTimeout = 1 * time.Second
	conf.DialRetryLimit = 1
	broker, err := kafka.Dial(kafkaAddrs, conf)
	if err != nil {
		log.Fatalf("cannot connect to kafka cluster: %s", err)
	}

	defer broker.Close()

	go push_message()

	go consume_flow_message(broker, config.Config.KafkaTopicFlow, config.Config.KafkaPartitionFlay)
	go consume_flow_message(broker, config.Config.KafkaTopicFlow, config.Config.KafkaPartitionShow)
	consume_flow_message(broker, config.Config.KafkaTopicFlow, config.Config.KafkaPartitionFlow)
}

优化:

使用bytes.Buffer, 更高效。

func save_message() {
	if len(buffer) > 0 {
		tm := lib.TimeFormat()
		log := "stats_play_" + tm
		file := config.Config.StatsLogDir + log

		buf := bytes.Buffer{}
		for _, v := range buffer {
			if v == "" {
				continue
			}
			buf.WriteString(v)
			buf.WriteString("\n")
		}

		content := buf.String()
		if content != "" {
			lib.FilePutContents2(file, content)
			buffer = buffer[0:0]
		}
	}
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值