Skip to content

Commit

Permalink
feat: make context error
Browse files Browse the repository at this point in the history
  • Loading branch information
zenghur committed Feb 28, 2020
1 parent 9a6ddda commit e28d6bf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (c *Client) SetMakeContext(makeContext MakeContext) {
// DefaultMakeContextIfNone 保证makeContext不为空
func (c *Client) defaultMakeContext() {
if c.makeContext == nil {
c.makeContext = func(m *M) context.Context {
return context.TODO()
c.makeContext = func(m *M) (context.Context, error) {
return context.TODO(), nil
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,9 @@ func (c *Consumer) OnReceive(queue *Queue, receiveMsg *ReceiveMessage) {
m.EnqueueTime = receiveMsg.EnqueueTime
m.codec = queue.codec
m.ReceiptHandle = receiveMsg.ReceiptHandle
ctx := queue.makeContext(m)
ctx, err := queue.makeContext(m)
ctx = context.WithValue(ctx, aliyunMnsM, m)
ctx = context.WithValue(ctx, aliyunMnsContextErr, err)
errChan <- queue.OnReceive(ctx)
}()

Expand Down
11 changes: 9 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ import (
type contextKey int

const (
aliyunMnsM contextKey = 0
aliyunMnsM contextKey = 0
aliyunMnsContextErr contextKey = 1
)

// MakeContext 生成一个context
type MakeContext func(m *M) context.Context
type MakeContext func(m *M) (context.Context, error)

// MFrom 拿出message
func MFrom(ctx context.Context) (*M, bool) {
m, ok := ctx.Value(aliyunMnsM).(*M)
return m, ok
}

// ErrFrom 拿出context error
func ErrFrom(ctx context.Context) (*M, bool) {
m, ok := ctx.Value(aliyunMnsM).(*M)
return m, ok
}

0 comments on commit e28d6bf

Please sign in to comment.