Skip to content

Commit

Permalink
use atomic variables (matrixorigin#20995)
Browse files Browse the repository at this point in the history
问题:可能的解释是响应client时间耗时,持有了锁。同时rpc 等待锁,而没响应。
修改:dbname,username,改为 原子变量。
相关:其他变量没有与网络锁关联,无需修改。

Approved by: @qingxinhome, @sukki37
  • Loading branch information
daviszhen committed Jan 2, 2025
1 parent 130c4eb commit dc6ea09
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions pkg/frontend/mysql_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ type MysqlProtocolImpl struct {
maxClientPacketSize uint32

//the user of the client
username string
username atomic.Value

// opaque authentication response data generated by Authentication Method
// indicated by the plugin name field.
authResponse []byte

//the default database for the client
database string
database atomic.Value

// Connection attributes are key-value pairs that application programs
// can pass to the server at connect time.
Expand Down Expand Up @@ -523,15 +523,15 @@ func (mp *MysqlProtocolImpl) SetSequenceID(value uint8) {
}

func (mp *MysqlProtocolImpl) GetDatabaseName() string {
mp.m.Lock()
defer mp.m.Unlock()
return mp.database
val := mp.database.Load()
if val == nil {
return ""
}
return val.(string)
}

func (mp *MysqlProtocolImpl) SetDatabaseName(s string) {
mp.m.Lock()
defer mp.m.Unlock()
mp.database = s
mp.database.Store(s)
}

func (mp *MysqlProtocolImpl) GetAuthString() []byte {
Expand All @@ -547,15 +547,15 @@ func (mp *MysqlProtocolImpl) GetAuthResponse() []byte {
}

func (mp *MysqlProtocolImpl) GetUserName() string {
mp.m.Lock()
defer mp.m.Unlock()
return mp.username
val := mp.username.Load()
if val == nil {
return ""
}
return val.(string)
}

func (mp *MysqlProtocolImpl) SetUserName(s string) {
mp.m.Lock()
defer mp.m.Unlock()
mp.username = s
mp.username.Store(s)
}

const defaultTcp4PackageSize = 1<<14 - 66
Expand Down Expand Up @@ -1521,8 +1521,8 @@ func (mp *MysqlProtocolImpl) HandleHandshake(ctx context.Context, payload []byte
}

mp.maxClientPacketSize = resp41.maxPacketSize
mp.username = resp41.username
mp.database = resp41.database
mp.username.Store(resp41.username)
mp.database.Store(resp41.database)
mp.connectAttrs = resp41.connectAttrs
} else {
var resp320 response320
Expand All @@ -1543,8 +1543,8 @@ func (mp *MysqlProtocolImpl) HandleHandshake(ctx context.Context, payload []byte
mp.charset = "utf8mb4"

mp.maxClientPacketSize = resp320.maxPacketSize
mp.username = resp320.username
mp.database = resp320.database
mp.username.Store(resp320.username)
mp.database.Store(resp320.database)
}
return false, nil
}
Expand All @@ -1560,7 +1560,7 @@ func (mp *MysqlProtocolImpl) Authenticate(ctx context.Context) error {
ses.Debugf(ctx, "authenticate user")
if err := mp.authenticateUser(ctx, mp.authResponse); err != nil {
ses.Errorf(ctx, "authenticate user failed.error:%v", err)
errorCode, sqlState, msg := RewriteError(err, mp.username)
errorCode, sqlState, msg := RewriteError(err, mp.GetUserName())
ses.timestampMap[TSSendErrPacketStart] = time.Now()
err2 := mp.sendErrPacket(errorCode, sqlState, msg)
ses.timestampMap[TSSendErrPacketEnd] = time.Now()
Expand Down

0 comments on commit dc6ea09

Please sign in to comment.