-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.go
67 lines (56 loc) · 1.49 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
package pulsarKit
import (
"context"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/richelieu-yang/chimera/v2/src/core/errorKit"
"github.com/richelieu-yang/chimera/v2/src/log/logrusKit"
"github.com/sirupsen/logrus"
"sync"
)
var setupOnce sync.Once
var config *Config
func MustSetUp(config *Config) {
err := SetUp(config)
if err != nil {
logrusKit.DisableQuote(nil)
logrus.Fatalf("%+v", err)
}
}
func SetUp(pulsarConfig *Config) (err error) {
setupOnce.Do(func() {
config = pulsarConfig
if config == nil {
err = errorKit.New("config == nil")
} else {
err = verify(config.VerifyConfig)
}
if err != nil {
config = nil
}
})
return err
}
// NewProducer
/*
前提: 成功调用 SetUp() || MustSetUp().
@param options 必需属性: Topic、SendTimeout
@param logPath 客户端的日志输出("": 输出到控制台)
*/
func NewProducer(ctx context.Context, options pulsar.ProducerOptions, logPath string) (*Producer, error) {
if config == nil {
return nil, NotSetupError
}
return NewProducerOriginally(ctx, config.Addresses, options, logPath)
}
// NewConsumer
/*
前提: 成功调用 SetUp() || MustSetUp().
@param options 必需属性: Topic、SubscriptionName、Type
@param logPath 客户端的日志输出("": 输出到控制台)
*/
func NewConsumer(ctx context.Context, options pulsar.ConsumerOptions, logPath string) (*Consumer, error) {
if config == nil {
return nil, NotSetupError
}
return NewConsumerOriginally(ctx, config.Addresses, options, logPath)
}