@@ -26,7 +26,6 @@ var _ Bus = &DefaultBus{}
26
26
type DefaultBus struct {
27
27
* Safety
28
28
* Glogged
29
- Outgoing * AMQPOutbox
30
29
Outbox TxOutbox
31
30
PrefetchCount uint
32
31
AmqpConnStr string
@@ -41,6 +40,7 @@ type DefaultBus struct {
41
40
amqpErrors chan * amqp.Error
42
41
amqpBlocks chan amqp.Blocking
43
42
Registrations []* Registration
43
+ amqpOutbox * AMQPOutbox
44
44
45
45
RPCHandlers map [string ]MessageHandler
46
46
deadletterHandler func (tx * sql.Tx , poision amqp.Delivery ) error
@@ -54,16 +54,16 @@ type DefaultBus struct {
54
54
started bool
55
55
Glue SagaGlue
56
56
TxProvider TxProvider
57
- IsTxnl bool
58
- WorkerNum uint
59
- Serializer Serializer
60
- DLX string
61
- DefaultPolicies []MessagePolicy
62
- Confirm bool
63
- healthChan chan error
64
- backpressure bool
65
- DbPingTimeout time.Duration
66
- amqpConnected bool
57
+
58
+ WorkerNum uint
59
+ Serializer Serializer
60
+ DLX string
61
+ DefaultPolicies []MessagePolicy
62
+ Confirm bool
63
+ healthChan chan error
64
+ backpressure bool
65
+ DbPingTimeout time.Duration
66
+ amqpConnected bool
67
67
}
68
68
69
69
var (
@@ -203,37 +203,28 @@ func (b *DefaultBus) Start() error {
203
203
b .egressConn .NotifyClose (b .amqpErrors )
204
204
b .egressConn .NotifyBlocked (b .amqpBlocks )
205
205
b .egressChannel .NotifyClose (b .amqpErrors )
206
- //TODO:Figure out what should be done
207
206
208
- //init the outbox that sends the messages to the amqp transport and handles publisher confirms
209
- if e := b .Outgoing .init (b .egressChannel , b .Confirm , true ); e != nil {
210
- return e
211
- }
212
207
/*
213
208
start the transactional outbox, make sure calling b.TxOutgoing.Start() is done only after b.Outgoing.init is called
214
209
TODO://the design is crap and needs to be refactored
215
210
*/
216
- if b .IsTxnl {
217
-
218
- var amqpChan * amqp.Channel
219
- if amqpChan , e = b .createAMQPChannel (b .egressConn ); e != nil {
220
- b .Log ().WithError (e ).Error ("failed to create amqp channel for transactional outbox" )
221
- return e
222
- }
223
- amqpChan .NotifyClose (b .amqpErrors )
224
- amqpOutbox := & AMQPOutbox {
225
- SvcName : b .SvcName ,
226
- }
227
- err := amqpOutbox .init (amqpChan , b .Confirm , false )
228
- if err != nil {
229
- b .Log ().WithError (err ).Error ("failed initializing amqpOutbox" )
230
- return err
231
- }
232
- if startErr := b .Outbox .Start (amqpOutbox ); startErr != nil {
233
- b .Log ().WithError (startErr ).Error ("failed to start transactional outbox" )
234
- return startErr
235
- }
236
-
211
+ var amqpChan * amqp.Channel
212
+ if amqpChan , e = b .createAMQPChannel (b .egressConn ); e != nil {
213
+ b .Log ().WithError (e ).Error ("failed to create amqp channel for transactional outbox" )
214
+ return e
215
+ }
216
+ amqpChan .NotifyClose (b .amqpErrors )
217
+ b .amqpOutbox = & AMQPOutbox {
218
+ SvcName : b .SvcName ,
219
+ }
220
+ err := b .amqpOutbox .init (amqpChan , b .Confirm , false )
221
+ if err != nil {
222
+ b .Log ().WithError (err ).Error ("failed initializing amqpOutbox" )
223
+ return err
224
+ }
225
+ if startErr := b .Outbox .Start (b .amqpOutbox ); startErr != nil {
226
+ b .Log ().WithError (startErr ).Error ("failed to start transactional outbox" )
227
+ return startErr
237
228
}
238
229
239
230
//declare queue
@@ -244,10 +235,10 @@ func (b *DefaultBus) Start() error {
244
235
b .serviceQueue = q
245
236
246
237
//bind queue
247
- err := b .bindServiceQueue ()
248
- if err != nil {
238
+ bindErr := b .bindServiceQueue ()
239
+ if bindErr != nil {
249
240
b .Log ().WithError (err ).Error ("could not bind service to queue" )
250
- return err
241
+ return bindErr
251
242
}
252
243
253
244
//declare rpc queue
@@ -299,7 +290,6 @@ func (b *DefaultBus) createBusWorkers(workerNum uint) ([]*worker, error) {
299
290
q : b .serviceQueue ,
300
291
rpcq : b .rpcQueue ,
301
292
svcName : b .SvcName ,
302
- isTxnl : b .IsTxnl ,
303
293
txProvider : b .TxProvider ,
304
294
rpcLock : b .RPCLock ,
305
295
rpcHandlers : b .RPCHandlers ,
@@ -339,23 +329,19 @@ func (b *DefaultBus) Shutdown() (shutdwonErr error) {
339
329
return err
340
330
}
341
331
}
342
- b .Outgoing .shutdown ()
343
332
344
333
if err := b .Glue .Stop (); err != nil {
345
334
return err
346
335
}
347
336
b .started = false
348
- if b .IsTxnl {
349
-
350
- err := b .Outbox .Stop ()
351
-
352
- if err != nil {
353
- b .Log ().WithError (err ).Error ("could not shutdown outbox" )
354
- return err
355
- }
356
- b .TxProvider .Dispose ()
337
+ err := b .Outbox .Stop ()
357
338
339
+ if err != nil {
340
+ b .Log ().WithError (err ).Error ("could not shutdown outbox" )
341
+ return err
358
342
}
343
+ b .amqpOutbox .Shutdown ()
344
+ b .TxProvider .Dispose ()
359
345
360
346
return nil
361
347
}
@@ -370,11 +356,8 @@ func (b *DefaultBus) NotifyHealth(health chan error) {
370
356
371
357
//GetHealth implements Health.GetHealth
372
358
func (b * DefaultBus ) GetHealth () HealthCard {
373
- var dbConnected bool
374
359
375
- if b .IsTxnl {
376
- dbConnected = b .TxProvider .Ping (b .DbPingTimeout )
377
- }
360
+ dbConnected := b .TxProvider .Ping (b .DbPingTimeout )
378
361
379
362
return HealthCard {
380
363
DbConnected : dbConnected ,
@@ -387,11 +370,11 @@ func (b *DefaultBus) withTx(action func(tx *sql.Tx) error, ambientTx *sql.Tx) er
387
370
var shouldCommitTx bool
388
371
var activeTx * sql.Tx
389
372
//create a new transaction only if there is no active one already passed in
390
- if b . IsTxnl && ambientTx == nil {
373
+ if ambientTx == nil {
391
374
392
375
/*
393
376
if the passed in ambient transaction is not nil it means that some caller has created the transaction
394
- and knows when should this transaction bee committed or rolledback.
377
+ and knows when should this transaction be committed or rolledback.
395
378
In these cases we only invoke the passed in action with the passed in transaction
396
379
and do not commit/rollback the transaction.action
397
380
If no ambient transaction is passed in then we create a new transaction and commit or rollback after
@@ -414,11 +397,7 @@ func (b *DefaultBus) withTx(action func(tx *sql.Tx) error, ambientTx *sql.Tx) er
414
397
}
415
398
actionErr := b .SafeWithRetries (retryAction , MaxRetryCount )
416
399
417
- /*
418
- if the bus is transactional and there is no ambient transaction then create a new one else use the ambient tranaction.
419
- if the bus is not transactional a nil transaction reference will be passed
420
- */
421
- if b .IsTxnl && shouldCommitTx {
400
+ if shouldCommitTx {
422
401
if actionErr != nil {
423
402
err := activeTx .Rollback ()
424
403
if err != nil {
@@ -464,7 +443,12 @@ func (b *DefaultBus) RPC(ctx context.Context, service string, request, reply *Bu
464
443
rpcID : rpcID }
465
444
466
445
b .Serializer .Register (reply .Payload )
467
- err := b .sendImpl (ctx , nil , service , b .rpcQueue .Name , "" , "" , request , rpc )
446
+
447
+ sendRPC := func (tx * sql.Tx ) error {
448
+ return b .sendImpl (ctx , tx , service , b .rpcQueue .Name , "" , "" , request , rpc )
449
+ }
450
+
451
+ err := b .withTx (sendRPC , nil )
468
452
if err != nil {
469
453
b .Log ().WithError (err ).Error ("could not send message" )
470
454
return nil , err
@@ -624,22 +608,13 @@ func (b *DefaultBus) monitorAMQPErrors() {
624
608
625
609
func (b * DefaultBus ) publish (tx * sql.Tx , exchange , routingKey string , msg * amqp.Publishing ) error {
626
610
publish := func () error {
627
- //send to the transactional outbox if the bus is transactional
628
- //otherwise send directly to amqp
629
- if b .IsTxnl && tx != nil {
630
- b .Log ().WithField ("message_id" , msg .MessageId ).Debug ("sending message to outbox" )
631
- saveErr := b .Outbox .Save (tx , exchange , routingKey , * msg )
632
- if saveErr != nil {
633
- b .Log ().WithError (saveErr ).Error ("failed to save to transactional outbox" )
634
- }
635
- return saveErr
636
- }
637
- //do not attempt to contact the borker if backpressure is being applied
638
- if b .backpressure {
639
- return errors .New ("can't send message due to backpressure from amqp broker" )
611
+
612
+ b .Log ().WithField ("message_id" , msg .MessageId ).Debug ("sending message to outbox" )
613
+ saveErr := b .Outbox .Save (tx , exchange , routingKey , * msg )
614
+ if saveErr != nil {
615
+ b .Log ().WithError (saveErr ).Error ("failed to save to transactional outbox" )
640
616
}
641
- _ , outgoingErr := b .Outgoing .Post (exchange , routingKey , * msg )
642
- return outgoingErr
617
+ return saveErr
643
618
}
644
619
//currently only one thread can publish at a time
645
620
//TODO:add a publishing workers
0 commit comments