- 订阅前经 nsqd HTTP 接口创建 topic,并把 nsqlookupd 轮询间隔缩短为 15 秒:避免首次发布前查不到 topic 导致消费者最长 60 秒后才发现 nsqd、并持续输出查询失败日志 - 步骤结果为跳过(如所属账户未开实名管控)时记最近成功时间,使读入口请求检查的冷却同样生效,不再反复产生无需调网关的卡任务
257 lines
7.5 KiB
Go
257 lines
7.5 KiB
Go
// Package nsqbus 封装卡轮询使用的 NSQ 生产者与按 topic 动态订阅的消费者管理器。
|
||
// NSQ 只承担执行分发与消费者并发控制,业务状态与重试全部在 Redis(add-carrier-polling-scheduler)。
|
||
package nsqbus
|
||
|
||
import (
|
||
"context"
|
||
"net"
|
||
"net/http"
|
||
"net/url"
|
||
"strconv"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
|
||
"github.com/nsqio/go-nsq"
|
||
"go.uber.org/zap"
|
||
)
|
||
|
||
// Producer 是 nsqd 生产者。
|
||
type Producer struct {
|
||
producer *nsq.Producer
|
||
}
|
||
|
||
// NewProducer 连接 nsqd TCP 地址创建生产者。
|
||
func NewProducer(address string, logger *zap.Logger) (*Producer, error) {
|
||
cfg := nsq.NewConfig()
|
||
producer, err := nsq.NewProducer(address, cfg)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
producer.SetLogger(nsqLogger{logger: logger}, nsq.LogLevelWarning)
|
||
return &Producer{producer: producer}, nil
|
||
}
|
||
|
||
// Publish 同步发布一条消息;topic 不存在时由 nsqd 自动创建。
|
||
func (p *Producer) Publish(topic string, body []byte) error {
|
||
return p.producer.Publish(topic, body)
|
||
}
|
||
|
||
// Stop 停止生产者。
|
||
func (p *Producer) Stop() {
|
||
if p != nil && p.producer != nil {
|
||
p.producer.Stop()
|
||
}
|
||
}
|
||
|
||
// Handler 处理一条消息;返回后由管理器 FIN。消息处理期间可调用 touch 延长 NSQ 消息超时。
|
||
type Handler func(ctx context.Context, body []byte, touch func())
|
||
|
||
// ConsumerConfig 是消费者参数。
|
||
type ConsumerConfig struct {
|
||
NSQDAddress string
|
||
NSQLookupdAddresses []string
|
||
MsgTimeout time.Duration
|
||
MaxAttempts uint16
|
||
// LookupdPollInterval 消费者向 nsqlookupd 查询 nsqd 的间隔;0 表示 15 秒。
|
||
LookupdPollInterval time.Duration
|
||
}
|
||
|
||
// ConsumerManager 按 topic 动态订阅与取消订阅;每个 topic 一个消费者,max_in_flight 控制本进程对该 topic 的并发。
|
||
type ConsumerManager struct {
|
||
cfg ConsumerConfig
|
||
channel string
|
||
handler Handler
|
||
logger *zap.Logger
|
||
|
||
mu sync.Mutex
|
||
consumers map[string]*subscription
|
||
ctx context.Context
|
||
cancel context.CancelFunc
|
||
inflight sync.WaitGroup
|
||
}
|
||
|
||
type subscription struct {
|
||
consumer *nsq.Consumer
|
||
maxInFlight int
|
||
}
|
||
|
||
// NewConsumerManager 创建消费者管理器。
|
||
func NewConsumerManager(cfg ConsumerConfig, channel string, handler Handler, logger *zap.Logger) *ConsumerManager {
|
||
if logger == nil {
|
||
logger = zap.NewNop()
|
||
}
|
||
ctx, cancel := context.WithCancel(context.Background())
|
||
return &ConsumerManager{cfg: cfg, channel: channel, handler: handler, logger: logger,
|
||
consumers: map[string]*subscription{}, ctx: ctx, cancel: cancel}
|
||
}
|
||
|
||
// Sync 使订阅集合与 desired(topic → max_in_flight)一致:新增订阅、取消多余订阅、调整 max_in_flight。
|
||
func (m *ConsumerManager) Sync(desired map[string]int) {
|
||
m.mu.Lock()
|
||
defer m.mu.Unlock()
|
||
if m.ctx.Err() != nil {
|
||
return
|
||
}
|
||
for topic, sub := range m.consumers {
|
||
if _, keep := desired[topic]; !keep {
|
||
sub.consumer.Stop()
|
||
delete(m.consumers, topic)
|
||
m.logger.Info("取消订阅卡任务 topic", zap.String("topic", topic))
|
||
}
|
||
}
|
||
for topic, maxInFlight := range desired {
|
||
if maxInFlight <= 0 {
|
||
maxInFlight = 1
|
||
}
|
||
if sub, ok := m.consumers[topic]; ok {
|
||
if sub.maxInFlight != maxInFlight {
|
||
sub.consumer.ChangeMaxInFlight(maxInFlight)
|
||
sub.maxInFlight = maxInFlight
|
||
}
|
||
continue
|
||
}
|
||
consumer, err := m.subscribe(topic, maxInFlight)
|
||
if err != nil {
|
||
m.logger.Warn("订阅卡任务 topic 失败,下一轮重试", zap.String("topic", topic), zap.Error(err))
|
||
continue
|
||
}
|
||
m.consumers[topic] = &subscription{consumer: consumer, maxInFlight: maxInFlight}
|
||
m.logger.Info("订阅卡任务 topic", zap.String("topic", topic), zap.Int("max_in_flight", maxInFlight))
|
||
}
|
||
}
|
||
|
||
func (m *ConsumerManager) subscribe(topic string, maxInFlight int) (*nsq.Consumer, error) {
|
||
cfg := nsq.NewConfig()
|
||
cfg.MaxInFlight = maxInFlight
|
||
if m.cfg.MsgTimeout > 0 {
|
||
cfg.MsgTimeout = m.cfg.MsgTimeout
|
||
}
|
||
if m.cfg.MaxAttempts > 0 {
|
||
cfg.MaxAttempts = m.cfg.MaxAttempts
|
||
}
|
||
cfg.LookupdPollInterval = 15 * time.Second
|
||
if m.cfg.LookupdPollInterval > 0 {
|
||
cfg.LookupdPollInterval = m.cfg.LookupdPollInterval
|
||
}
|
||
// 先在 nsqd 上创建 topic:否则首次发布前 nsqlookupd 查不到该 topic,消费者要等下一轮轮询才能发现 nsqd。
|
||
m.ensureTopic(topic)
|
||
consumer, err := nsq.NewConsumer(topic, m.channel, cfg)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
consumer.SetLogger(nsqLogger{logger: m.logger}, nsq.LogLevelWarning)
|
||
handlerCtx := m.ctx // subscribe 在持锁时调用,捕获当前处理上下文
|
||
consumer.AddConcurrentHandlers(nsq.HandlerFunc(func(message *nsq.Message) error {
|
||
message.DisableAutoResponse()
|
||
m.inflight.Add(1)
|
||
defer m.inflight.Done()
|
||
defer message.Finish()
|
||
m.handler(handlerCtx, message.Body, message.Touch)
|
||
return nil
|
||
}), maxInFlight)
|
||
if len(m.cfg.NSQLookupdAddresses) > 0 {
|
||
err = consumer.ConnectToNSQLookupds(m.cfg.NSQLookupdAddresses)
|
||
} else {
|
||
err = consumer.ConnectToNSQD(m.cfg.NSQDAddress)
|
||
}
|
||
if err != nil {
|
||
consumer.Stop()
|
||
return nil, err
|
||
}
|
||
return consumer, nil
|
||
}
|
||
|
||
// Topics 返回当前已订阅的 topic。
|
||
func (m *ConsumerManager) Topics() []string {
|
||
m.mu.Lock()
|
||
defer m.mu.Unlock()
|
||
topics := make([]string, 0, len(m.consumers))
|
||
for topic := range m.consumers {
|
||
topics = append(topics, topic)
|
||
}
|
||
return topics
|
||
}
|
||
|
||
// StopAll 停止全部订阅;在宽限期内等待执行中的消息完成并 FIN,超过宽限期后取消处理上下文。
|
||
func (m *ConsumerManager) StopAll(grace time.Duration) {
|
||
m.mu.Lock()
|
||
for topic, sub := range m.consumers {
|
||
sub.consumer.Stop()
|
||
delete(m.consumers, topic)
|
||
}
|
||
m.mu.Unlock()
|
||
done := make(chan struct{})
|
||
go func() {
|
||
m.inflight.Wait()
|
||
close(done)
|
||
}()
|
||
select {
|
||
case <-done:
|
||
case <-time.After(grace):
|
||
m.logger.Warn("优雅退出宽限期内仍有卡任务未完成,取消执行(由租约到期后重新领取)")
|
||
}
|
||
m.cancel()
|
||
}
|
||
|
||
// Reset 在 StopAll 后重新允许订阅(切回 carrier 模式时使用)。
|
||
func (m *ConsumerManager) Reset() {
|
||
m.mu.Lock()
|
||
defer m.mu.Unlock()
|
||
if m.ctx.Err() != nil {
|
||
m.ctx, m.cancel = context.WithCancel(context.Background())
|
||
}
|
||
}
|
||
|
||
// SplitAddresses 解析逗号分隔的地址列表。
|
||
func SplitAddresses(raw string) []string {
|
||
var result []string
|
||
for _, part := range strings.Split(raw, ",") {
|
||
if part = strings.TrimSpace(part); part != "" {
|
||
result = append(result, part)
|
||
}
|
||
}
|
||
return result
|
||
}
|
||
|
||
type nsqLogger struct {
|
||
logger *zap.Logger
|
||
}
|
||
|
||
// Output 实现 go-nsq 的日志接口。
|
||
func (l nsqLogger) Output(_ int, s string) error {
|
||
if l.logger != nil {
|
||
l.logger.Warn("NSQ", zap.String("message", s))
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// ensureTopic 通过 nsqd HTTP 接口创建 topic(已存在时幂等);失败只记日志,消费者仍会在后续轮询中发现。
|
||
func (m *ConsumerManager) ensureTopic(topic string) {
|
||
httpAddress := nsqdHTTPAddress(m.cfg.NSQDAddress)
|
||
if httpAddress == "" {
|
||
return
|
||
}
|
||
client := http.Client{Timeout: 3 * time.Second}
|
||
resp, err := client.Post("http://"+httpAddress+"/topic/create?topic="+url.QueryEscape(topic), "text/plain", nil)
|
||
if err != nil {
|
||
m.logger.Debug("创建 NSQ topic 失败", zap.String("topic", topic), zap.Error(err))
|
||
return
|
||
}
|
||
_ = resp.Body.Close()
|
||
}
|
||
|
||
// nsqdHTTPAddress 由 nsqd TCP 地址推导 HTTP 地址(默认端口 4150 → 4151)。
|
||
func nsqdHTTPAddress(tcpAddress string) string {
|
||
host, port, err := net.SplitHostPort(strings.TrimSpace(tcpAddress))
|
||
if err != nil || host == "" {
|
||
return ""
|
||
}
|
||
if port == "4150" {
|
||
port = "4151"
|
||
} else if value, convErr := strconv.Atoi(port); convErr == nil {
|
||
port = strconv.Itoa(value + 1)
|
||
}
|
||
return net.JoinHostPort(host, port)
|
||
}
|