-
Notifications
You must be signed in to change notification settings - Fork 11
/
client.go
141 lines (122 loc) · 3.32 KB
/
client.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package pulsar
import (
"context"
"fmt"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/tuya/tuya-pulsar-sdk-go/pkg/tylog"
"strings"
"time"
)
const (
PulsarAddrCN = "pulsar+ssl://mqe.tuyacn.com:7285"
PulsarAddrEU = "pulsar+ssl://mqe.tuyaeu.com:7285"
PulsarAddrUS = "pulsar+ssl://mqe.tuyaus.com:7285"
)
type Message = pulsar.Message
type Client interface {
NewConsumer(config ConsumerConfig) (Consumer, error)
}
type ProducerMessage struct {
Payload []byte
Key string
}
type Consumer interface {
ReceiveAndHandle(ctx context.Context, handler PayloadHandlerV2)
Close() error
}
type PayloadHandlerV2 interface {
HandlePayload(ctx context.Context, msg Message, payload []byte) error
}
type clientImpl struct {
cli pulsar.Client
clientCfg ClientConfig
}
type ClientConfig struct {
PulsarAddr string
Auth interface{}
}
type ConsumerConfig struct {
Topic string
Subscription string
Auth interface{}
}
func NewClient(cfg ClientConfig) Client {
return newClientV2(cfg)
}
func newClientV2(cfg ClientConfig) Client {
return clientImpl{clientCfg: cfg}
}
func (c clientImpl) NewConsumer(config ConsumerConfig) (Consumer, error) {
if config.Auth != nil || c.cli == nil {
client, err := pulsar.NewClient(pulsar.ClientOptions{
TLSAllowInsecureConnection: true,
URL: c.clientCfg.PulsarAddr,
Authentication: config.Auth,
})
if err != nil {
tylog.Error("create clientImpl failed", tylog.ErrorField(err), tylog.Any("config", c.clientCfg))
return nil, err
}
tylog.Info("create clientImpl success", tylog.Any("config", c.clientCfg))
c.cli = client
}
consumer, err := c.cli.Subscribe(pulsar.ConsumerOptions{
Topic: config.Topic,
SubscriptionName: subscriptionName(config.Topic),
Type: pulsar.Failover,
})
if err != nil {
tylog.Error("create consumer failed", tylog.ErrorField(err), tylog.Any("config", config))
return nil, err
}
tylog.Info("create consumer success", tylog.Any("config", config))
return consumerV2{consumer}, nil
}
type consumerV2 struct {
consumer pulsar.Consumer
}
func (c consumerV2) ReceiveAndHandle(ctx context.Context, handler PayloadHandlerV2) {
for i := 0; i < 10; i++ {
go func() {
for {
msg, err := c.consumer.Receive(context.Background())
if err != nil {
tylog.Error("consumer receive failed", tylog.ErrorField(err), tylog.Any("consumer", c.consumer))
continue
}
err = handler.HandlePayload(ctx, msg, msg.Payload())
if err != nil {
tylog.Warn("consumer HandlePayload failed", tylog.ErrorField(err), tylog.Any("consumer", c.consumer), tylog.Any("msg", msg))
}
retryCount := 3
for j := 0; j < retryCount; j++ {
err := c.consumer.Ack(msg)
if err != nil {
tylog.Warn("ack failed", tylog.String("msg", string(msg.Payload())))
time.Sleep(time.Second)
}
}
}
}()
}
select {
case <-ctx.Done():
return
}
}
func (c consumerV2) Close() error {
c.consumer.Close()
return nil
}
func TopicForAccessID(accessID string) string {
topic := fmt.Sprintf("persistent://%s/out/event", accessID)
return topic
}
func subscriptionName(topic string) string {
return getTenant(topic) + "-sub"
}
func getTenant(topic string) string {
topic = strings.TrimPrefix(topic, "persistent://")
end := strings.Index(topic, "/")
return topic[:end]
}