-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathinvocation.go
101 lines (83 loc) · 2.87 KB
/
invocation.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
package gbus
import (
"context"
"database/sql"
"time"
"github.com/sirupsen/logrus"
)
var _ Invocation = &defaultInvocationContext{}
var _ Messaging = &defaultInvocationContext{}
type defaultInvocationContext struct {
*Glogged
invokingSvc string
bus *DefaultBus
inboundMsg *BusMessage
tx *sql.Tx
ctx context.Context
exchange string
routingKey string
deliveryInfo DeliveryInfo
}
//DeliveryInfo provdes information as to the attempted deilvery of the invocation
type DeliveryInfo struct {
Attempt uint
MaxRetryCount uint
}
func (dfi *defaultInvocationContext) InvokingSvc() string {
return dfi.invokingSvc
}
//Reply implements the Invocation.Reply signature
func (dfi *defaultInvocationContext) Reply(ctx context.Context, replyMessage *BusMessage) error {
if dfi.inboundMsg != nil {
replyMessage.CorrelationID = dfi.inboundMsg.ID
replyMessage.SagaCorrelationID = dfi.inboundMsg.SagaID
replyMessage.RPCID = dfi.inboundMsg.RPCID
}
var err error
if dfi.tx != nil {
return dfi.bus.sendWithTx(ctx, dfi.tx, dfi.invokingSvc, replyMessage)
}
if err = dfi.bus.Send(ctx, dfi.invokingSvc, replyMessage); err != nil {
//TODO: add logs?
logrus.WithError(err).Error("could not send reply")
}
return err
}
//Send implements the Invocation.Send signature
func (dfi *defaultInvocationContext) Send(ctx context.Context, toService string, command *BusMessage, policies ...MessagePolicy) error {
if dfi.tx != nil {
return dfi.bus.sendWithTx(ctx, dfi.tx, toService, command, policies...)
}
return dfi.bus.Send(ctx, toService, command, policies...)
}
//Publish implements the Invocation.Publish signature
func (dfi *defaultInvocationContext) Publish(ctx context.Context, exchange, topic string, event *BusMessage, policies ...MessagePolicy) error {
if dfi.tx != nil {
return dfi.bus.publishWithTx(ctx, dfi.tx, exchange, topic, event, policies...)
}
return dfi.bus.Publish(ctx, exchange, topic, event, policies...)
}
//RPC implements the Invocation.RPC signature
func (dfi *defaultInvocationContext) RPC(ctx context.Context, service string, request, reply *BusMessage, timeout time.Duration) (*BusMessage, error) {
return dfi.bus.RPC(ctx, service, request, reply, timeout)
}
//Bus implements the Invocation.Bus signature
func (dfi *defaultInvocationContext) Bus() Messaging {
return dfi
}
//Tx implements the Invocation.Tx signature
func (dfi *defaultInvocationContext) Tx() *sql.Tx {
return dfi.tx
}
//Ctx implements the Invocation.Ctx signature
func (dfi *defaultInvocationContext) Ctx() context.Context {
return dfi.ctx
}
//Routing implements the Invocation.Routing signature
func (dfi *defaultInvocationContext) Routing() (exchange, routingKey string) {
return dfi.exchange, dfi.routingKey
}
//DeliveryInfo implements the Invocation.DeliveryInfo signature
func (dfi *defaultInvocationContext) DeliveryInfo() DeliveryInfo {
return dfi.deliveryInfo
}