使用session.context(ctx)方法后会覆盖原有的ctx,拿不到session_id #2052

Closed
opened 2021-09-20 13:51:05 +00:00 by undefined_ss · 2 comments
Contributor

初始化engine的时候可以设置EnableSessionID

// EnableSessionID if enable session id
func (engine *Engine) EnableSessionID(enable bool) {
	engine.logSessionID = enable
}

这个开关会在后面newSession的使用来初始化session里面的ctx,并放入log.SessionIDKeylog.SessionKey。这里本意应该是在后面的时候可以使用ctx来获取SessionIDSessionKey(比如LoggerAdapter)

func newSession(engine *Engine) *Session {
	var ctx context.Context
	if engine.logSessionID {
		ctx = context.WithValue(engine.defaultContext, log.SessionIDKey, newSessionID())
	} else {
		ctx = engine.defaultContext
	}
    
    ...
    
    	if engine.logSessionID {
		session.ctx = context.WithValue(session.ctx, log.SessionKey, session)
	}
	return session
}

但是如果使用session的时候,使用了sessionContext(ctx)方法传入了上下文的context,就会直接覆盖,导致需要取SessionKey的地方取不到。下面的是session.Context()方法

// Context sets the context on this session
func (session *Session) Context(ctx context.Context) *Session {
	session.ctx = ctx
	return session
}

可以在使用了sessionContext(ctx)方法的时候,将原有的值拷贝一份到传入的ctx中去,而不是覆盖可以解决此问题

// Context sets the context on this session
func (session *Session) Context(ctx context.Context) *Session {
	if session.ctx != nil {
		ctx = context.WithValue(ctx, log.SessionIDKey, session.ctx.Value(log.SessionIDKey))
		ctx = context.WithValue(ctx, log.SessionKey, session.ctx.Value(log.SessionKey))
		ctx = context.WithValue(ctx, log.SessionShowSQLKey, session.ctx.Value(log.SessionShowSQLKey))
	}
	
	session.ctx = ctx
	return session
}
初始化`engine`的时候可以设置`EnableSessionID` ```golang // EnableSessionID if enable session id func (engine *Engine) EnableSessionID(enable bool) { engine.logSessionID = enable } ``` 这个开关会在后面`newSession`的使用来初始化`session`里面的`ctx`,并放入`log.SessionIDKey`和`log.SessionKey`。这里本意应该是在后面的时候可以使用ctx来获取`SessionID`和`SessionKey`(比如`LoggerAdapter`) ``` func newSession(engine *Engine) *Session { var ctx context.Context if engine.logSessionID { ctx = context.WithValue(engine.defaultContext, log.SessionIDKey, newSessionID()) } else { ctx = engine.defaultContext } ... if engine.logSessionID { session.ctx = context.WithValue(session.ctx, log.SessionKey, session) } return session } ``` 但是如果使用`session`的时候,使用了`session`的`Context(ctx)`方法传入了上下文的context,就会**直接覆盖**,导致需要取`SessionKey`的地方取不到。下面的是`session.Context()`方法 ``` golang // Context sets the context on this session func (session *Session) Context(ctx context.Context) *Session { session.ctx = ctx return session } ``` 可以在使用了`session`的`Context(ctx)`方法的时候,将原有的值拷贝一份到传入的ctx中去,而不是覆盖可以解决此问题 ``` // Context sets the context on this session func (session *Session) Context(ctx context.Context) *Session { if session.ctx != nil { ctx = context.WithValue(ctx, log.SessionIDKey, session.ctx.Value(log.SessionIDKey)) ctx = context.WithValue(ctx, log.SessionKey, session.ctx.Value(log.SessionKey)) ctx = context.WithValue(ctx, log.SessionShowSQLKey, session.ctx.Value(log.SessionShowSQLKey)) } session.ctx = ctx return session } ```
undefined_ss started working 2021-09-20 13:54:56 +00:00
lunny added the
kind
bug
label 2021-09-21 12:23:36 +00:00
Owner

Could you send a PR to fix this?

Could you send a PR to fix this?
Author
Contributor

Could you send a PR to fix this?

ok, pr created, ref: #2053

> Could you send a PR to fix this? ok, pr created, ref: https://gitea.com/xorm/xorm/pulls/2053
lunny closed this issue 2021-09-23 12:22:15 +00:00
undefined_ss stopped working 2021-09-24 10:24:40 +00:00
92h 29min 44s
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#2052
No description provided.