forked from apache/rocketmq-client-go
/
transaction_producer.go
232 lines (196 loc) · 6.25 KB
/
transaction_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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rocketmq
/*
#cgo LDFLAGS: -L/usr/local/lib/ -lrocketmq
#include <stdio.h>
#include <stdlib.h>
#include <rocketmq/CMessage.h>
#include <rocketmq/CProducer.h>
#include <rocketmq/CSendResult.h>
#include <rocketmq/CTransactionStatus.h>
extern int localTransactionCheckerCallback(CProducer *producer, CMessageExt *msg,void *userData);
int transactionChecker_cgo(CProducer *producer, CMessageExt *msg, void *userData) {
return localTransactionCheckerCallback(producer, msg, userData);
}
extern int localTransactionExecutorCallback(CProducer *producer, CMessage *msg,void *userData);
int transactionExecutor_cgo(CProducer *producer, CMessage *msg, void *userData) {
return localTransactionExecutorCallback(producer, msg, userData);
}
*/
import "C"
import (
"errors"
log "github.com/sirupsen/logrus"
"sync"
"unsafe"
)
type TransactionStatus int
const (
CommitTransaction = TransactionStatus(C.E_COMMIT_TRANSACTION)
RollbackTransaction = TransactionStatus(C.E_ROLLBACK_TRANSACTION)
UnknownTransaction = TransactionStatus(C.E_UNKNOWN_TRANSACTION)
)
func (status TransactionStatus) String() string {
switch status {
case CommitTransaction:
return "CommitTransaction"
case RollbackTransaction:
return "RollbackTransaction"
case UnknownTransaction:
return "UnknownTransaction"
default:
return "UnknownTransaction"
}
}
func newDefaultTransactionProducer(config *ProducerConfig, listener TransactionLocalListener, arg interface{}) (*defaultTransactionProducer, error) {
if config == nil {
return nil, errors.New("config is nil")
}
if config.GroupID == "" {
return nil, errors.New("GroupId is empty")
}
if config.NameServer == "" && config.NameServerDomain == "" {
return nil, errors.New("NameServer and NameServerDomain is empty")
}
producer := &defaultTransactionProducer{config: config}
cs := C.CString(config.GroupID)
var cproduer *C.struct_CProducer
cproduer = C.CreateTransactionProducer(cs, (C.CLocalTransactionCheckerCallback)(unsafe.Pointer(C.transactionChecker_cgo)), unsafe.Pointer(&arg))
C.free(unsafe.Pointer(cs))
if cproduer == nil {
return nil, errors.New("create transaction Producer failed")
}
var err rmqError
if config.NameServer != "" {
cs = C.CString(config.NameServer)
err = rmqError(C.SetProducerNameServerAddress(cproduer, cs))
C.free(unsafe.Pointer(cs))
if err != NIL {
return nil, err
}
}
if config.NameServerDomain != "" {
cs = C.CString(config.NameServerDomain)
err = rmqError(C.SetProducerNameServerDomain(cproduer, cs))
C.free(unsafe.Pointer(cs))
if err != NIL {
return nil, err
}
}
if config.InstanceName != "" {
cs = C.CString(config.InstanceName)
err = rmqError(C.SetProducerInstanceName(cproduer, cs))
C.free(unsafe.Pointer(cs))
if err != NIL {
return nil, err
}
}
if config.Credentials != nil {
ak := C.CString(config.Credentials.AccessKey)
sk := C.CString(config.Credentials.SecretKey)
ch := C.CString(config.Credentials.Channel)
err = rmqError(C.SetProducerSessionCredentials(cproduer, ak, sk, ch))
C.free(unsafe.Pointer(ak))
C.free(unsafe.Pointer(sk))
C.free(unsafe.Pointer(ch))
if err != NIL {
return nil, err
}
}
if config.LogC != nil {
cs = C.CString(config.LogC.Path)
err = rmqError(C.SetProducerLogPath(cproduer, cs))
C.free(unsafe.Pointer(cs))
if err != NIL {
return nil, err
}
err = rmqError(C.SetProducerLogFileNumAndSize(cproduer, C.int(config.LogC.FileNum), C.long(config.LogC.FileSize)))
if err != NIL {
return nil, err
}
err = rmqError(C.SetProducerLogLevel(cproduer, C.CLogLevel(config.LogC.Level)))
if err != NIL {
return nil, err
}
}
if config.SendMsgTimeout > 0 {
err = rmqError(C.SetProducerSendMsgTimeout(cproduer, C.int(config.SendMsgTimeout)))
if err != NIL {
return nil, err
}
}
if config.CompressLevel > 0 {
err = rmqError(C.SetProducerCompressLevel(cproduer, C.int(config.CompressLevel)))
if err != NIL {
return nil, err
}
}
if config.MaxMessageSize > 0 {
err = rmqError(C.SetProducerMaxMessageSize(cproduer, C.int(config.MaxMessageSize)))
if err != NIL {
return nil, err
}
}
producer.cproduer = cproduer
transactionProducerMap.Store(cproduer, producer)
producer.listenerFuncsMap.Store(cproduer, listener)
return producer, nil
}
type defaultTransactionProducer struct {
config *ProducerConfig
cproduer *C.struct_CProducer
listenerFuncsMap sync.Map
}
func (p *defaultTransactionProducer) String() string {
return p.config.String()
}
// Start the producer.
func (p *defaultTransactionProducer) Start() error {
err := rmqError(C.StartProducer(p.cproduer))
if err != NIL {
return err
}
return nil
}
// Shutdown the producer.
func (p *defaultTransactionProducer) Shutdown() error {
err := rmqError(C.ShutdownProducer(p.cproduer))
if err != NIL {
return err
}
err = rmqError(int(C.DestroyProducer(p.cproduer)))
if err != NIL {
return err
}
return err
}
func (p *defaultTransactionProducer) SendMessageTransaction(msg *Message, arg interface{}) (*SendResult, error) {
cmsg := goMsgToC(msg)
defer C.DestroyMessage(cmsg)
var sr C.struct__SendResult_
err := rmqError(C.SendMessageTransaction(p.cproduer, cmsg, (C.CLocalTransactionExecutorCallback)(unsafe.Pointer(C.transactionExecutor_cgo)), unsafe.Pointer(&arg), &sr))
if err != NIL {
log.Warnf("send message error, error is: %s", err.Error())
return nil, err
}
result := &SendResult{}
result.Status = SendStatus(sr.sendStatus)
result.MsgId = C.GoString(&sr.msgId[0])
result.Offset = int64(sr.offset)
return result, nil
}