@@ -42,14 +42,8 @@ func (p *processImpl) startConsumingProcess(ctx context.Context) error {
42
42
consumer := chainConsumerInterceptors (p .Consumer , p .ConsumerInterceptors ... )
43
43
44
44
err := p .subscribe (ctx , func (in Message ) {
45
- msg := & singleMessage {
46
- Ctx : p .createConsumingContext (),
47
- Msg : in ,
48
- EnqueuedAt : time .Now (),
49
- }
50
- p .handleMessage (msg , func (in queuedMessage ) error {
51
- m := in .(* singleMessage )
52
- return errors .WithStack (consumer .Consume (m .Ctx , m .Msg ))
45
+ p .handleMessage (p .createConsumingContext (), & singleMessage {Message : in }, func (ctx context.Context ) error {
46
+ return errors .WithStack (consumer .Consume (ctx , in ))
53
47
})
54
48
})
55
49
return errors .WithStack (err )
@@ -73,9 +67,9 @@ func (p *processImpl) startBatchConsumingProcess(ctx context.Context) error {
73
67
defer p .Logger .Print ("Finish batch consuming process" )
74
68
75
69
for m := range outCh {
76
- p . handleMessage ( m , func ( in queuedMessage ) error {
77
- m := in .( * multiMessages )
78
- return errors .WithStack (batchConsumer .BatchConsume (m . Ctx , m . Msgs ))
70
+ msgs := m . Msgs
71
+ p . handleMessage ( m . Ctx , m , func ( ctx context. Context ) error {
72
+ return errors .WithStack (batchConsumer .BatchConsume (ctx , msgs ))
79
73
})
80
74
}
81
75
}()
@@ -93,10 +87,11 @@ func (p *processImpl) createConsumingContext() context.Context {
93
87
ctx := context .Background ()
94
88
ctx = p .StatsHandler .TagProcess (ctx , & BeginTag {})
95
89
ctx = p .StatsHandler .TagProcess (ctx , & EnqueueTag {})
90
+ ctx = setEnqueuedAt (ctx , time .Now ().UTC ())
96
91
return ctx
97
92
}
98
93
99
- func (p * processImpl ) handleMessage (m queuedMessage , handle func (queuedMessage ) error ) {
94
+ func (p * processImpl ) handleMessage (ctx context. Context , m queuedMessage , handle func (context. Context ) error ) {
100
95
p .wg .Add (1 )
101
96
go func () {
102
97
defer p .wg .Done ()
@@ -107,16 +102,18 @@ func (p *processImpl) handleMessage(m queuedMessage, handle func(queuedMessage)
107
102
m .Ack ()
108
103
}
109
104
110
- p .StatsHandler .HandleProcess (m .Context (), & Dequeue {
111
- BeginTime : m .GetEnqueuedAt (),
105
+ enqueuedAt := getEnqueuedAt (ctx )
106
+
107
+ p .StatsHandler .HandleProcess (ctx , & Dequeue {
108
+ BeginTime : enqueuedAt ,
112
109
EndTime : time .Now (),
113
110
})
114
111
115
112
beginTime := time .Now ()
116
113
117
- m . SetContext ( p .StatsHandler .TagProcess (m . Context () , & ConsumeBeginTag {}) )
114
+ ctx = p .StatsHandler .TagProcess (ctx , & ConsumeBeginTag {})
118
115
119
- err := handle (m )
116
+ err := handle (ctx )
120
117
121
118
if ! p .AckImmediately {
122
119
if err != nil {
@@ -126,15 +123,15 @@ func (p *processImpl) handleMessage(m queuedMessage, handle func(queuedMessage)
126
123
}
127
124
}
128
125
129
- p .StatsHandler .HandleProcess (m . Context () , & ConsumeEnd {
126
+ p .StatsHandler .HandleProcess (ctx , & ConsumeEnd {
130
127
BeginTime : beginTime ,
131
128
EndTime : time .Now (),
132
129
Error : err ,
133
130
})
134
131
135
- p .StatsHandler .HandleProcess (m . Context () , & End {
132
+ p .StatsHandler .HandleProcess (ctx , & End {
136
133
MsgCount : m .Count (),
137
- BeginTime : m . GetEnqueuedAt () ,
134
+ BeginTime : enqueuedAt ,
138
135
EndTime : time .Now (),
139
136
})
140
137
}()
0 commit comments