refactor: standardization of code structure (#7)

This commit is contained in:
Li4n0
2021-04-25 14:15:42 +08:00
committed by GitHub
parent 26755351f7
commit 50a6749070
17 changed files with 88 additions and 100 deletions
+11 -14
View File
@@ -47,10 +47,9 @@ func (s *Server) getRules() []*Rule {
func (s *Server) updateRules() error {
db := database.DB.Model(new(Rule))
defer s.rulesLock.Unlock()
s.rulesLock.Lock()
db.Order("rank desc").Find(&s.rules)
s.rulesLock.Unlock()
return nil
return db.Order("rank desc").Find(&s.rules).Error
}
// NewConnection is part of the mysql.Handler interface.
@@ -90,28 +89,26 @@ func (s *Server) NewConnection(c *vmysql.Conn) {
func (s *Server) ConnectionClosed(c *vmysql.Conn) {
log.Trace("MySQL Client leaved, ID [%d]", c.ConnectionID)
var (
user = c.User
schema = c.SchemaName
supportLoadLocalData = c.SupportLoadDataLocal
cr, ok = s.connRulePool.Load(c.ConnectionID)
clientName, clientOS, flag, flagGroup string
)
var clientName, clientOS, flag, flagGroup string
user := c.User
schema := c.SchemaName
supportLoadLocalData := c.SupportLoadDataLocal
cr, ok := s.connRulePool.Load(c.ConnectionID)
if !ok {
log.Error("MySQL Connection rule(%d) not match flag", c.ConnectionID)
return
}
_rule := cr.(*Rule)
// flag must not be empty
for _, s := range []string{user, schema} {
flag, flagGroup, _ = _rule.Match(s)
if flag != "" {
break
}
}
if flag == "" {
log.Error("MySQL Connection rule(%d) not match flag", c.ConnectionID)
}
if c.ConnAttrs != nil {
clientName = c.ConnAttrs["_client_name"] + " " + c.ConnAttrs["_client_version"]
@@ -130,7 +127,7 @@ func (s *Server) ConnectionClosed(c *vmysql.Conn) {
r, err := newRecord(_rule, flag, user, clientName, clientOS, ip, qqwry.Area(ip), supportLoadLocalData, files)
if err != nil {
log.Error("MySQL record(rule_id:%s) created failed :%s", _rule.Name, err.Error())
log.Error("MySQL record(rule_id:%s) created failed :%s", _rule.Name, err)
return
}
log.Info("MySQL record[id:%d rule:%s remote_ip:%s] has been created", r.ID, _rule.Name, ip)