mirror of
https://github.com/eventials/goevents.git
synced 2025-04-24 13:48:53 +08:00
Use Context as in http.Request.
This commit is contained in:
parent
aa1680aedd
commit
0049262e0a
@ -1,7 +1,6 @@
|
|||||||
package amqp
|
package amqp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -226,9 +225,8 @@ func (c *Consumer) dispatch(msg amqplib.Delivery) {
|
|||||||
|
|
||||||
func (c *Consumer) doDispatch(msg amqplib.Delivery, h *handler, retryCount int32, delay time.Duration) {
|
func (c *Consumer) doDispatch(msg amqplib.Delivery, h *handler, retryCount int32, delay time.Duration) {
|
||||||
err := h.fn(messaging.Event{
|
err := h.fn(messaging.Event{
|
||||||
Action: h.action,
|
Action: h.action,
|
||||||
Body: msg.Body,
|
Body: msg.Body,
|
||||||
Context: context.Background(),
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package messaging
|
package messaging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,12 +20,6 @@ type SubscribeOptions struct {
|
|||||||
MaxRetries int32
|
MaxRetries int32
|
||||||
}
|
}
|
||||||
|
|
||||||
type Event struct {
|
|
||||||
Action string
|
|
||||||
Body []byte
|
|
||||||
Context context.Context
|
|
||||||
}
|
|
||||||
|
|
||||||
type EventHandler func(Event) error
|
type EventHandler func(Event) error
|
||||||
|
|
||||||
type Consumer interface {
|
type Consumer interface {
|
||||||
|
35
messaging/event.go
Normal file
35
messaging/event.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package messaging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Event struct {
|
||||||
|
Action string
|
||||||
|
Body []byte
|
||||||
|
ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithContext returns a shallow copy of Event with its context changed to ctx.
|
||||||
|
// The provided ctx must be non-nil.
|
||||||
|
func (e *Event) WithContext(ctx context.Context) *Event {
|
||||||
|
if ctx == nil {
|
||||||
|
panic("nil context")
|
||||||
|
}
|
||||||
|
|
||||||
|
e2 := new(Event)
|
||||||
|
*e2 = *e
|
||||||
|
e2.ctx = ctx
|
||||||
|
|
||||||
|
return e2
|
||||||
|
}
|
||||||
|
|
||||||
|
// The returned context is always non-nil; it defaults to the background context.
|
||||||
|
// To change the context, use WithContext.
|
||||||
|
func (e *Event) Context() context.Context {
|
||||||
|
if e.ctx != nil {
|
||||||
|
return e.ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
return context.Background()
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user