-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
111 lines (86 loc) · 1.74 KB
/
context.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
102
103
104
105
106
107
108
109
110
111
package equeue
import (
"context"
"errors"
"math"
"time"
"github.com/cloudevents/sdk-go/v2/event"
)
const abortIndex int8 = math.MaxInt8 >> 1
type ResultNackWithRedeliveryDelay struct {
delay time.Duration
}
func (r ResultNackWithRedeliveryDelay) Error() string {
return "nack with delay"
}
func (r ResultNackWithRedeliveryDelay) Delay() time.Duration {
return r.delay
}
type Context struct {
engine *Engine
Request *Request
handlers HandlersChain
index int8
nack bool
nackRedeliveryDelay time.Duration
Errors equeueErrors
}
func (c *Context) reset() {
c.handlers = nil
c.index = -1
c.nack = false
c.nackRedeliveryDelay = 0
c.Errors = c.Errors[:0]
}
func (c *Context) Publish(ctx context.Context, topic string, event event.Event) error {
return c.engine.Publish(ctx, topic, event)
}
func (c *Context) Next() {
c.index++
for c.index < int8(len(c.handlers)) {
c.handlers[c.index](c)
c.index++
}
}
func (c *Context) Abort() {
c.index = abortIndex
}
func (c *Context) IsAborted() bool {
return c.index >= abortIndex
}
func (c *Context) AbortWithNack() {
c.Nack()
c.Abort()
}
func (c *Context) AbortWithError(err error) {
c.Error(err)
c.Abort()
}
func (c *Context) Nack() {
c.nack = true
}
func (c *Context) NackWithRedeliveryDelay(delay time.Duration) {
c.nack = true
c.nackRedeliveryDelay = delay
}
func (c *Context) IsNack() bool {
return c.nack
}
func (c *Context) Error(err error) *Error {
if err == nil {
panic("err is nil")
}
var perr *Error
ok := errors.As(err, &perr)
if !ok {
perr = &Error{
Err: err,
Type: ErrorTypePrivate,
}
}
c.Errors = append(c.Errors, perr)
return perr
}
func (c *Context) Done() <-chan struct{} {
return c.Request.Context().Done()
}