-
Notifications
You must be signed in to change notification settings - Fork 1
/
producer.go
88 lines (81 loc) · 2.07 KB
/
producer.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
package mq
import (
"context"
"fmt"
"github.com/DOSNetwork/core/edge_cm/conf"
"github.com/DOSNetwork/core/edge_cm/tools"
"github.com/apache/pulsar-client-go/pulsar"
log "github.com/sirupsen/logrus"
"time"
)
var clientInstance pulsar.Client
func client() pulsar.Client {
if clientInstance == nil {
tools.Logger.Info(fmt.Sprintf("url:%s,timeOut:%d,token:%s", conf.Conf.Pulsar.PulsarURL, conf.Conf.Pulsar.ConnectionTimeout, conf.Conf.Pulsar.Token))
cli, err := pulsar.NewClient(pulsar.ClientOptions{
URL: conf.Conf.Pulsar.PulsarURL,
ConnectionTimeout: time.Second * time.Duration(conf.Conf.Pulsar.ConnectionTimeout),
OperationTimeout: time.Second * time.Duration(conf.Conf.Pulsar.ConnectionTimeout),
Authentication: pulsar.NewAuthenticationToken(conf.Conf.Pulsar.Token),
})
if err != nil {
log.Panicf("create pulsar client error: %s", err.Error())
}
clientInstance = cli
}
return clientInstance
}
// init pulsar logger
func initLogger(l string) {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "15:04:05.000",
})
var level log.Level
switch l {
case "info":
level = log.InfoLevel
case "warn":
level = log.WarnLevel
case "error":
level = log.ErrorLevel
default:
level = log.DebugLevel
}
log.SetLevel(level)
}
func init() {
initLogger("error")
}
func Send(sync bool, topic string, payload []byte) (status bool, retErr error) {
producer, err := client().CreateProducer(pulsar.ProducerOptions{
Topic: topic,
})
if err != nil {
return false, err
}
defer producer.Close()
ctx := context.Background()
msg := &pulsar.ProducerMessage{
Payload: payload,
}
if sync {
// Attempt to send the message
if _, err := producer.Send(ctx, msg); err != nil {
status = false
retErr = err
} else {
status = true
}
} else {
// Attempt to send the message asynchronously and handle the response
producer.SendAsync(ctx, msg, func(MessageID pulsar.MessageID, pMsg *pulsar.ProducerMessage, err error) {
if err != nil {
status = false
retErr = err
}
})
status = true
}
return
}