Skip to content

Commit d5199b5

Browse files
committed
feat: add Request
1 parent 74960ce commit d5199b5

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

context.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ const abortIndex int8 = math.MaxInt8 >> 1
1313
type Context struct {
1414
engine *Engine
1515

16-
Event *event.Event
17-
ctx context.Context
16+
Request *Request
1817

1918
handlers HandlersChain
2019
index int8
@@ -88,5 +87,5 @@ func (c *Context) Error(err error) *Error {
8887
}
8988

9089
func (c *Context) Done() <-chan struct{} {
91-
return c.ctx.Done()
90+
return c.Request.Context().Done()
9291
}

equeue.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,20 @@ func (w *worker) run() {
297297
c := w.engine.pool.Get().(*Context)
298298
c.reset()
299299

300+
var ctx context.Context
301+
ctx, w.cancelCtx = context.WithCancel(context.Background())
302+
300303
var err error
301-
c.Event, err = binding.ToEvent(context.Background(), w.message)
304+
e, err := binding.ToEvent(context.Background(), w.message)
302305
if err != nil {
303306
_, filename, line, _ := runtime.Caller(0)
304307
c.Error(err).SetType(ErrorTypeBind).SetMeta(H{
305308
"filename": filename,
306309
"line": line + 1})
307310
}
311+
c.Request = &Request{Event: e, ctx: ctx}
312+
308313
c.handlers = w.subscription.handlers
309-
c.ctx, w.cancelCtx = context.WithCancel(context.Background())
310314
c.Next()
311315

312316
if c.IsNack() {

request.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package equeue
2+
3+
import (
4+
"context"
5+
6+
"github.com/cloudevents/sdk-go/v2/event"
7+
)
8+
9+
// http.Request
10+
type Request struct {
11+
Event *event.Event
12+
13+
ctx context.Context
14+
}
15+
16+
// Context returns the request's context. To change the context, use
17+
// Clone or WithContext.
18+
//
19+
// The returned context is always non-nil; it defaults to the
20+
// background context.
21+
//
22+
// For outgoing client requests, the context controls cancellation.
23+
//
24+
// For incoming server requests, the context is canceled when the
25+
// client's connection closes, the request is canceled (with HTTP/2),
26+
// or when the ServeHTTP method returns.
27+
func (r *Request) Context() context.Context {
28+
if r.ctx != nil {
29+
return r.ctx
30+
}
31+
return context.Background()
32+
}
33+
34+
// WithContext returns a shallow copy of r with its context changed
35+
// to ctx. The provided ctx must be non-nil.
36+
//
37+
// For outgoing client request, the context controls the entire
38+
// lifetime of a request and its response: obtaining a connection,
39+
// sending the request, and reading the response headers and body.
40+
//
41+
// To create a new request with a context, use NewRequestWithContext.
42+
// To make a deep copy of a request with a new context, use Request.Clone.
43+
func (r *Request) WithContext(ctx context.Context) *Request {
44+
if ctx == nil {
45+
panic("nil context")
46+
}
47+
r2 := new(Request)
48+
*r2 = *r
49+
r2.ctx = ctx
50+
return r2
51+
}

0 commit comments

Comments
 (0)