-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.go
77 lines (63 loc) · 1.91 KB
/
setup.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
package pulsarKit
import (
"context"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/richelieu-yang/chimera/v3/src/core/errorKit"
"github.com/richelieu-yang/chimera/v3/src/log/logrusKit"
"github.com/richelieu-yang/chimera/v3/src/validateKit"
"github.com/sirupsen/logrus"
)
var config *Config
// MustSetUp
/*
PS: Pulsar服务中途挂掉的话,恢复后,Consumer实例、Producer实例还能正常工作.
@param verifyConfig 可以为nil(将不验证,但不推荐这么干)
*/
func MustSetUp(pulsarConfig *Config, verifyConfig *VerifyConfig) {
if err := SetUp(pulsarConfig, verifyConfig); err != nil {
logrusKit.DisableQuote(nil)
logrus.Fatalf("%+v", err)
}
}
func SetUp(pulsarConfig *Config, verifyConfig *VerifyConfig) (err error) {
defer func() {
if err != nil {
config = nil
}
}()
if err = validateKit.Struct(pulsarConfig); err != nil {
err = errorKit.Wrapf(err, "Fail to verify")
return
}
config = pulsarConfig
// verify
if err = verify(verifyConfig); err != nil {
err = errorKit.Wrapf(err, "Fail to verify")
return
}
return
}
// NewProducer
/*
前提: 成功调用 SetUp() || MustSetUp().
@param options 必需属性: Topic、SendTimeout
@param logPath 客户端的日志输出("": 输出到控制台)
*/
func NewProducer(ctx context.Context, options pulsar.ProducerOptions, clientLogPath string) (*Producer, error) {
if config == nil {
return nil, NotSetupError
}
return NewProducerOriginally(ctx, config.Addrs, options, clientLogPath)
}
// NewConsumer
/*
前提: 成功调用 SetUp() || MustSetUp().
@param options 必需属性: Topic、SubscriptionName、Type
@param logPath 客户端的日志输出("": 输出到控制台)
*/
func NewConsumer(ctx context.Context, options pulsar.ConsumerOptions, clientLogPath string) (*Consumer, error) {
if config == nil {
return nil, NotSetupError
}
return NewConsumerOriginally(ctx, config.Addrs, options, clientLogPath)
}