-
Notifications
You must be signed in to change notification settings - Fork 18
/
expire.go
44 lines (32 loc) · 1.04 KB
/
expire.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
package middleware
import (
"context"
"strconv"
"time"
"github.com/makasim/amqpextra/consumer"
amqp "github.com/rabbitmq/amqp091-go"
)
func ExpireToTimeout(defaultTimeout time.Duration) func(next consumer.Handler) consumer.Handler {
return wrap(func(ctx context.Context, msg amqp.Delivery, next consumer.Handler) (result interface{}) {
if msg.Expiration == "" {
if defaultTimeout.Nanoseconds() == 0 {
return next.Handle(ctx, msg)
}
nextCtx, cancelFunc := context.WithTimeout(ctx, defaultTimeout)
defer cancelFunc()
return next.Handle(nextCtx, msg)
}
expiration, err := strconv.ParseInt(msg.Expiration, 10, 0)
if err != nil {
log(ctx, "[WARN] got invalid expiration: %s", msg.Expiration)
if defaultTimeout.Nanoseconds() != 0 {
nextCtx, cancelFunc := context.WithTimeout(ctx, defaultTimeout)
defer cancelFunc()
return next.Handle(nextCtx, msg)
}
}
nextCtx, cancelFunc := context.WithTimeout(ctx, time.Duration(expiration)*time.Millisecond)
defer cancelFunc()
return next.Handle(nextCtx, msg)
})
}