浏览代码

调整context的new方法

dcsunny 4 年之前
父节点
当前提交
e85295cb6f
共有 1 个文件被更改,包括 30 次插入4 次删除
  1. 30 4
      common/context.go

+ 30 - 4
common/context.go

@@ -2,13 +2,39 @@ package common
 
 import (
 	"context"
+	"time"
 
 	context2 "git.ikuban.com/server/kratos-utils/http/context"
 )
 
-func NewContext(accountID int64) context.Context {
-	ctx := context.Background()
-	ctx = context2.AppendToContext(ctx, "user_id", accountID)
-	ctx = context2.AppendToContext(ctx, "account_id", accountID)
+func NewContext(parentCtx context.Context) context.Context {
+	ctx := &AccountContext{ctx: parentCtx}
 	return ctx
 }
+
+func NewContextWithAccountID(parentCtx context.Context, accountID int64) context.Context {
+	ctx := &AccountContext{ctx: parentCtx}
+	ctx = context2.AppendToContext(ctx, "user_id", accountID).(*AccountContext)
+	ctx = context2.AppendToContext(ctx, "account_id", accountID).(*AccountContext)
+	return ctx
+}
+
+type AccountContext struct {
+	ctx context.Context
+}
+
+func (this *AccountContext) Deadline() (deadline time.Time, ok bool) {
+	return this.ctx.Deadline()
+}
+
+func (*AccountContext) Done() <-chan struct{} {
+	return nil
+}
+
+func (this *AccountContext) Err() error {
+	return this.ctx.Err()
+}
+
+func (this *AccountContext) Value(key interface{}) interface{} {
+	return this.ctx.Value(key)
+}