-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterceptor.go
51 lines (39 loc) · 1.48 KB
/
interceptor.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
package subee
import (
"context"
"github.com/pkg/errors"
)
// BatchConsumerFunc type is an adapter to allow the use of ordinary functions as BatchConsumer.
type BatchConsumerFunc func(context.Context, []Message) error
// BatchConsume call f(ctx, msgs)
func (f BatchConsumerFunc) BatchConsume(ctx context.Context, msgs []Message) error {
return errors.WithStack(f(ctx, msgs))
}
// BatchConsumerInterceptor provides a hook to intercept the execution of a multiple messages consumption.
type BatchConsumerInterceptor func(BatchConsumer) BatchConsumer
// ConsumerFunc type is an adapter to allow the use of ordinary functions as Consumer.
type ConsumerFunc func(context.Context, Message) error
// Consume call f(ctx, msgs)
func (f ConsumerFunc) Consume(ctx context.Context, msg Message) error {
return errors.WithStack(f(ctx, msg))
}
// ConsumerInterceptor provides a hook to intercept the execution of a message consumption.
type ConsumerInterceptor func(Consumer) Consumer
func chainConsumerInterceptors(consumer Consumer, interceptors ...ConsumerInterceptor) Consumer {
if len(interceptors) == 0 {
return consumer
}
for i := len(interceptors) - 1; i >= 0; i-- {
consumer = interceptors[i](consumer)
}
return consumer
}
func chainBatchConsumerInterceptors(consumer BatchConsumer, interceptors ...BatchConsumerInterceptor) BatchConsumer {
if len(interceptors) == 0 {
return consumer
}
for i := len(interceptors) - 1; i >= 0; i-- {
consumer = interceptors[i](consumer)
}
return consumer
}