-
Notifications
You must be signed in to change notification settings - Fork 3
/
consumer.go
108 lines (84 loc) · 2.69 KB
/
consumer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package nsq
import (
"context"
"github.com/alexfalkowski/go-service/otel"
"github.com/alexfalkowski/go-service/transport/nsq/handler"
"github.com/alexfalkowski/go-service/transport/nsq/logger"
lzap "github.com/alexfalkowski/go-service/transport/nsq/logger/zap"
"github.com/alexfalkowski/go-service/transport/nsq/marshaller"
"github.com/alexfalkowski/go-service/transport/nsq/meta"
"github.com/alexfalkowski/go-service/transport/nsq/metrics/prometheus"
notel "github.com/alexfalkowski/go-service/transport/nsq/otel"
"github.com/nsqio/go-nsq"
"go.uber.org/fx"
"go.uber.org/zap"
)
// ConsumerOption for NSQ.
type ConsumerOption interface{ apply(*consumerOptions) }
type consumerOptions struct {
logger *zap.Logger
tracer notel.Tracer
metrics *prometheus.ConsumerMetrics
}
type consumerOptionFunc func(*consumerOptions)
func (f consumerOptionFunc) apply(o *consumerOptions) { f(o) }
// WithConsumerLogger for NSQ.
func WithConsumerLogger(logger *zap.Logger) ConsumerOption {
return consumerOptionFunc(func(o *consumerOptions) {
o.logger = logger
})
}
// WithConsumerConfig for NSQ.
func WithConsumerTracer(tracer notel.Tracer) ConsumerOption {
return consumerOptionFunc(func(o *consumerOptions) {
o.tracer = tracer
})
}
// WithConsumerMetrics for NSQ.
func WithConsumerMetrics(metrics *prometheus.ConsumerMetrics) ConsumerOption {
return consumerOptionFunc(func(o *consumerOptions) {
o.metrics = metrics
})
}
// ConsumerParams for NSQ.
type ConsumerParams struct {
Lifecycle fx.Lifecycle
Topic, Channel string
Config *Config
Handler handler.Handler
Marshaller marshaller.Marshaller
}
// RegisterConsumer for NSQ.
func RegisterConsumer(params ConsumerParams, opts ...ConsumerOption) error {
defaultOptions := &consumerOptions{tracer: otel.NewNoopTracer("nsq")}
for _, o := range opts {
o.apply(defaultOptions)
}
cfg := nsq.NewConfig()
c, err := nsq.NewConsumer(params.Topic, params.Channel, cfg)
if err != nil {
return err
}
c.SetLogger(logger.NewLogger(), nsq.LogLevelInfo)
h := params.Handler
if defaultOptions.logger != nil {
h = lzap.NewHandler(lzap.HandlerParams{Topic: params.Topic, Channel: params.Channel, Logger: defaultOptions.logger, Handler: h})
}
if defaultOptions.metrics != nil {
h = defaultOptions.metrics.Handler(params.Topic, params.Channel, h)
}
h = notel.NewHandler(params.Topic, params.Channel, defaultOptions.tracer, h)
h = meta.NewHandler(h)
c.AddHandler(handler.New(handler.Params{Handler: h, Marshaller: params.Marshaller}))
err = c.ConnectToNSQLookupd(params.Config.LookupHost)
if err != nil {
return err
}
params.Lifecycle.Append(fx.Hook{
OnStop: func(context.Context) error {
c.Stop()
return nil
},
})
return nil
}