refactor(handler): use logger package log func replace zap log func

This commit is contained in:
douxu 2025-06-06 16:41:52 +08:00
parent 9aa5b0dcc6
commit f6cee44f84
20 changed files with 145 additions and 207 deletions

View File

@ -6,17 +6,17 @@ import (
"time" "time"
"modelRT/config" "modelRT/config"
"modelRT/logger"
"modelRT/orm" "modelRT/orm"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"github.com/panjf2000/ants/v2" "github.com/panjf2000/ants/v2"
"go.uber.org/zap"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
) )
// QueryCircuitDiagramComponentFromDB return the result of query circuit diagram component info order by page id from postgresDB // QueryCircuitDiagramComponentFromDB return the result of query circuit diagram component info order by page id from postgresDB
func QueryCircuitDiagramComponentFromDB(ctx context.Context, tx *gorm.DB, pool *ants.PoolWithFunc, logger *zap.Logger) (map[uuid.UUID]int, error) { func QueryCircuitDiagramComponentFromDB(ctx context.Context, tx *gorm.DB, pool *ants.PoolWithFunc) (map[uuid.UUID]int, error) {
var components []orm.Component var components []orm.Component
// ctx超时判断 // ctx超时判断
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
@ -24,7 +24,7 @@ func QueryCircuitDiagramComponentFromDB(ctx context.Context, tx *gorm.DB, pool *
result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&components) result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&components)
if result.Error != nil { if result.Error != nil {
logger.Error("query circuit diagram component info failed", zap.Error(result.Error)) logger.Error(ctx, "query circuit diagram component info failed", "error", result.Error)
return nil, result.Error return nil, result.Error
} }
@ -34,7 +34,7 @@ func QueryCircuitDiagramComponentFromDB(ctx context.Context, tx *gorm.DB, pool *
for _, component := range components { for _, component := range components {
pool.Invoke(config.ModelParseConfig{ pool.Invoke(config.ModelParseConfig{
ComponentInfo: component, ComponentInfo: component,
Context: ctx, Ctx: ctx,
}) })
componentTypeMap[component.GlobalUUID] = component.ComponentType componentTypeMap[component.GlobalUUID] = component.ComponentType

View File

@ -5,26 +5,25 @@ import (
"context" "context"
"time" "time"
"modelRT/logger"
"modelRT/orm" "modelRT/orm"
"go.uber.org/zap"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
) )
// QueryAllPages return the all page info of the circuit diagram query by grid_id and zone_id and station_id // QueryAllPages return the all page info of the circuit diagram query by grid_id and zone_id and station_id
func QueryAllPages(ctx context.Context, tx *gorm.DB, logger *zap.Logger, gridID, zoneID, stationID int64) ([]orm.Page, error) { func QueryAllPages(ctx context.Context, tx *gorm.DB, gridID, zoneID, stationID int64) ([]orm.Page, error) {
var pages []orm.Page var pages []orm.Page
// ctx超时判断 // ctx timeout judgment
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel() defer cancel()
result := tx.Model(&orm.Page{}).WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Select(`"page".id, "page".Name, "page".status,"page".context`).Joins(`inner join "station" on "station".id = "page".station_id`).Joins(`inner join "zone" on "zone".id = "station".zone_id`).Joins(`inner join "grid" on "grid".id = "zone".grid_id`).Where(`"grid".id = ? and "zone".id = ? and "station".id = ?`, gridID, zoneID, stationID).Scan(&pages) result := tx.Model(&orm.Page{}).WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Select(`"page".id, "page".Name, "page".status,"page".context`).Joins(`inner join "station" on "station".id = "page".station_id`).Joins(`inner join "zone" on "zone".id = "station".zone_id`).Joins(`inner join "grid" on "grid".id = "zone".grid_id`).Where(`"grid".id = ? and "zone".id = ? and "station".id = ?`, gridID, zoneID, stationID).Scan(&pages)
if result.Error != nil { if result.Error != nil {
logger.Error("query circuit diagram pages by gridID and zoneID and stationID failed", zap.Int64("grid_id", gridID), zap.Int64("zone_id", zoneID), zap.Int64("station_id", stationID), zap.Error(result.Error)) logger.Error(ctx, "query circuit diagram pages by gridID and zoneID and stationID failed", "grid_id", gridID, "zone_id", zoneID, "station_id", stationID, "error", result.Error)
return nil, result.Error return nil, result.Error
} }
return pages, nil return pages, nil
} }

View File

@ -8,17 +8,17 @@ import (
"modelRT/constant" "modelRT/constant"
"modelRT/diagram" "modelRT/diagram"
"modelRT/logger"
"modelRT/orm" "modelRT/orm"
"modelRT/sql" "modelRT/sql"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"go.uber.org/zap"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
) )
// QueryTopologic return the topologic info of the circuit diagram // QueryTopologic return the topologic info of the circuit diagram
func QueryTopologic(ctx context.Context, tx *gorm.DB, logger *zap.Logger) ([]orm.Topologic, error) { func QueryTopologic(ctx context.Context, tx *gorm.DB) ([]orm.Topologic, error) {
var topologics []orm.Topologic var topologics []orm.Topologic
// ctx超时判断 // ctx超时判断
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
@ -26,23 +26,23 @@ func QueryTopologic(ctx context.Context, tx *gorm.DB, logger *zap.Logger) ([]orm
result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Raw(sql.RecursiveSQL, constant.UUIDNilStr).Scan(&topologics) result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Raw(sql.RecursiveSQL, constant.UUIDNilStr).Scan(&topologics)
if result.Error != nil { if result.Error != nil {
logger.Error("query circuit diagram topologic info by start node uuid failed", zap.String("start_node_uuid", constant.UUIDNilStr), zap.Error(result.Error)) logger.Error(ctx, "query circuit diagram topologic info by start node uuid failed", "start_node_uuid", constant.UUIDNilStr, "error", result.Error)
return nil, result.Error return nil, result.Error
} }
return topologics, nil return topologics, nil
} }
// QueryTopologicFromDB return the result of query topologic info from DB // QueryTopologicFromDB return the result of query topologic info from DB
func QueryTopologicFromDB(ctx context.Context, tx *gorm.DB, logger *zap.Logger, componentTypeMap map[uuid.UUID]int) (*diagram.MultiBranchTreeNode, error) { func QueryTopologicFromDB(ctx context.Context, tx *gorm.DB, componentTypeMap map[uuid.UUID]int) (*diagram.MultiBranchTreeNode, error) {
topologicInfos, err := QueryTopologic(ctx, tx, logger) topologicInfos, err := QueryTopologic(ctx, tx)
if err != nil { if err != nil {
logger.Error("query topologic info failed", zap.Error(err)) logger.Error(ctx, "query topologic info failed", "error", err)
return nil, err return nil, err
} }
tree, err := BuildMultiBranchTree(topologicInfos, componentTypeMap) tree, err := BuildMultiBranchTree(topologicInfos, componentTypeMap)
if err != nil { if err != nil {
logger.Error("init topologic failed", zap.Error(err)) logger.Error(ctx, "init topologic failed", "error", err)
return nil, err return nil, err
} }
return tree, nil return tree, nil

View File

@ -7,8 +7,6 @@ import (
"modelRT/logger" "modelRT/logger"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
"go.uber.org/zap"
) )
// RedisHash defines the encapsulation struct of redis hash type // RedisHash defines the encapsulation struct of redis hash type
@ -16,7 +14,6 @@ type RedisHash struct {
ctx context.Context ctx context.Context
rwLocker *locker.RedissionRWLocker rwLocker *locker.RedissionRWLocker
storageClient *redis.Client storageClient *redis.Client
logger *zap.Logger
} }
// NewRedisHash define func of new redis hash instance // NewRedisHash define func of new redis hash instance
@ -25,7 +22,6 @@ func NewRedisHash(ctx context.Context, hashKey string, token string, lockLeaseTi
ctx: ctx, ctx: ctx,
rwLocker: locker.InitRWLocker(hashKey, token, lockLeaseTime, needRefresh), rwLocker: locker.InitRWLocker(hashKey, token, lockLeaseTime, needRefresh),
storageClient: GetRedisClientInstance(), storageClient: GetRedisClientInstance(),
logger: logger.GetLoggerInstance(),
} }
} }
@ -33,14 +29,14 @@ func NewRedisHash(ctx context.Context, hashKey string, token string, lockLeaseTi
func (rh *RedisHash) SetRedisHashByMap(hashKey string, fields map[string]interface{}) error { func (rh *RedisHash) SetRedisHashByMap(hashKey string, fields map[string]interface{}) error {
err := rh.rwLocker.WLock(rh.ctx) err := rh.rwLocker.WLock(rh.ctx)
if err != nil { if err != nil {
rh.logger.Error("lock wLock by hashKey failed", zap.String("hashKey", hashKey), zap.Error(err)) logger.Error(rh.ctx, "lock wLock by hash_key failed", "hash_key", hashKey, "error", err)
return err return err
} }
defer rh.rwLocker.UnWLock(rh.ctx) defer rh.rwLocker.UnWLock(rh.ctx)
err = rh.storageClient.HSet(rh.ctx, hashKey, fields).Err() err = rh.storageClient.HSet(rh.ctx, hashKey, fields).Err()
if err != nil { if err != nil {
rh.logger.Error("set hash by map failed", zap.String("hashKey", hashKey), zap.Any("fields", fields), zap.Error(err)) logger.Error(rh.ctx, "set hash by map failed", "hash_key", hashKey, "fields", fields, "error", err)
return err return err
} }
return nil return nil
@ -50,14 +46,14 @@ func (rh *RedisHash) SetRedisHashByMap(hashKey string, fields map[string]interfa
func (rh *RedisHash) SetRedisHashByKV(hashKey string, field string, value interface{}) error { func (rh *RedisHash) SetRedisHashByKV(hashKey string, field string, value interface{}) error {
err := rh.rwLocker.WLock(rh.ctx) err := rh.rwLocker.WLock(rh.ctx)
if err != nil { if err != nil {
rh.logger.Error("lock wLock by hashKey failed", zap.String("hashKey", hashKey), zap.Error(err)) logger.Error(rh.ctx, "lock wLock by hash_key failed", "hash_key", hashKey, "error", err)
return err return err
} }
defer rh.rwLocker.UnWLock(rh.ctx) defer rh.rwLocker.UnWLock(rh.ctx)
err = rh.storageClient.HSet(rh.ctx, hashKey, field, value).Err() err = rh.storageClient.HSet(rh.ctx, hashKey, field, value).Err()
if err != nil { if err != nil {
rh.logger.Error("set hash by kv failed", zap.String("hashKey", hashKey), zap.String("field", field), zap.Any("value", value), zap.Error(err)) logger.Error(rh.ctx, "set hash by kv failed", "hash_key", hashKey, "field", field, "value", value, "error", err)
return err return err
} }
return nil return nil
@ -67,14 +63,14 @@ func (rh *RedisHash) SetRedisHashByKV(hashKey string, field string, value interf
func (rh *RedisHash) HGet(hashKey string, field string) (string, error) { func (rh *RedisHash) HGet(hashKey string, field string) (string, error) {
err := rh.rwLocker.RLock(rh.ctx) err := rh.rwLocker.RLock(rh.ctx)
if err != nil { if err != nil {
rh.logger.Error("lock rLock by hashKey failed", zap.String("hashKey", hashKey), zap.Error(err)) logger.Error(rh.ctx, "lock rLock by hash_key failed", "hash_key", hashKey, "error", err)
return "", err return "", err
} }
defer rh.rwLocker.UnRLock(rh.ctx) defer rh.rwLocker.UnRLock(rh.ctx)
result, err := rh.storageClient.HGet(rh.ctx, hashKey, field).Result() result, err := rh.storageClient.HGet(rh.ctx, hashKey, field).Result()
if err != nil { if err != nil {
rh.logger.Error("set hash by kv failed", zap.String("hashKey", hashKey), zap.String("field", field), zap.Error(err)) logger.Error(rh.ctx, "set hash by kv failed", "hash_key", hashKey, "field", field, "error", err)
return "", err return "", err
} }
return result, nil return result, nil
@ -84,14 +80,14 @@ func (rh *RedisHash) HGet(hashKey string, field string) (string, error) {
func (rh *RedisHash) HGetAll(hashKey string) (map[string]string, error) { func (rh *RedisHash) HGetAll(hashKey string) (map[string]string, error) {
err := rh.rwLocker.RLock(rh.ctx) err := rh.rwLocker.RLock(rh.ctx)
if err != nil { if err != nil {
rh.logger.Error("lock rLock by hashKey failed", zap.String("hashKey", hashKey), zap.Error(err)) logger.Error(rh.ctx, "lock rLock by hash_key failed", "hash_key", hashKey, "error", err)
return nil, err return nil, err
} }
defer rh.rwLocker.UnRLock(rh.ctx) defer rh.rwLocker.UnRLock(rh.ctx)
result, err := rh.storageClient.HGetAll(rh.ctx, hashKey).Result() result, err := rh.storageClient.HGetAll(rh.ctx, hashKey).Result()
if err != nil { if err != nil {
rh.logger.Error("get all hash field by hash key failed", zap.String("hashKey", hashKey), zap.Error(err)) logger.Error(rh.ctx, "get all hash field by hash key failed", "hash_key", hashKey, "error", err)
return nil, err return nil, err
} }
return result, nil return result, nil

View File

@ -33,14 +33,14 @@ func NewRedisSet(ctx context.Context, hashKey string, token string, lockLeaseTim
func (rs *RedisSet) SADD(setKey string, members ...interface{}) error { func (rs *RedisSet) SADD(setKey string, members ...interface{}) error {
err := rs.rwLocker.WLock(rs.ctx) err := rs.rwLocker.WLock(rs.ctx)
if err != nil { if err != nil {
rs.logger.Error("lock wLock by setKey failed", zap.String("setKey", setKey), zap.Error(err)) logger.Error(rs.ctx, "lock wLock by setKey failed", "set_key", setKey, "error", err)
return err return err
} }
defer rs.rwLocker.UnWLock(rs.ctx) defer rs.rwLocker.UnWLock(rs.ctx)
err = rs.storageClient.SAdd(rs.ctx, setKey, members).Err() err = rs.storageClient.SAdd(rs.ctx, setKey, members).Err()
if err != nil { if err != nil {
rs.logger.Error("add set by memebers failed", zap.String("setKey", setKey), zap.Any("members", members), zap.Error(err)) logger.Error(rs.ctx, "add set by memebers failed", "set_key", setKey, "members", members, "error", err)
return err return err
} }
return nil return nil
@ -50,14 +50,14 @@ func (rs *RedisSet) SADD(setKey string, members ...interface{}) error {
func (rs *RedisSet) SREM(setKey string, members ...interface{}) error { func (rs *RedisSet) SREM(setKey string, members ...interface{}) error {
err := rs.rwLocker.WLock(rs.ctx) err := rs.rwLocker.WLock(rs.ctx)
if err != nil { if err != nil {
rs.logger.Error("lock wLock by setKey failed", zap.String("setKey", setKey), zap.Error(err)) logger.Error(rs.ctx, "lock wLock by setKey failed", "set_key", setKey, "error", err)
return err return err
} }
defer rs.rwLocker.UnWLock(rs.ctx) defer rs.rwLocker.UnWLock(rs.ctx)
count, err := rs.storageClient.SRem(rs.ctx, setKey, members).Result() count, err := rs.storageClient.SRem(rs.ctx, setKey, members).Result()
if err != nil || count != int64(len(members)) { if err != nil || count != int64(len(members)) {
rs.logger.Error("rem members from set failed", zap.String("setKey", setKey), zap.Any("members", members), zap.Error(err)) logger.Error(rs.ctx, "rem members from set failed", "set_key", setKey, "members", members, "error", err)
return fmt.Errorf("rem members from set failed:%w", err) return fmt.Errorf("rem members from set failed:%w", err)
} }
@ -68,24 +68,24 @@ func (rs *RedisSet) SREM(setKey string, members ...interface{}) error {
func (rs *RedisSet) SMembers(setKey string) ([]string, error) { func (rs *RedisSet) SMembers(setKey string) ([]string, error) {
err := rs.rwLocker.RLock(rs.ctx) err := rs.rwLocker.RLock(rs.ctx)
if err != nil { if err != nil {
rs.logger.Error("lock rLock by setKey failed", zap.String("setKey", setKey), zap.Error(err)) logger.Error(rs.ctx, "lock rLock by setKey failed", "set_key", setKey, "error", err)
return nil, err return nil, err
} }
defer rs.rwLocker.UnRLock(rs.ctx) defer rs.rwLocker.UnRLock(rs.ctx)
result, err := rs.storageClient.SMembers(rs.ctx, setKey).Result() result, err := rs.storageClient.SMembers(rs.ctx, setKey).Result()
if err != nil { if err != nil {
rs.logger.Error("get all hash field by hash key failed", zap.String("setKey", setKey), zap.Error(err)) logger.Error(rs.ctx, "get all set field by hash key failed", "set_key", setKey, "error", err)
return nil, err return nil, err
} }
return result, nil return result, nil
} }
// SIsMember define func of determine whether an member is in set by key // SIsMember define func of determine whether an member is in set by key
func (rh *RedisHash) SIsMember(setKey string, member interface{}) (bool, error) { func (rs *RedisSet) SIsMember(setKey string, member interface{}) (bool, error) {
result, err := rh.storageClient.SIsMember(rh.ctx, setKey, member).Result() result, err := rs.storageClient.SIsMember(rs.ctx, setKey, member).Result()
if err != nil { if err != nil {
rh.logger.Error("get all hash field by hash key failed", zap.String("setKey", setKey), zap.Error(err)) logger.Error(rs.ctx, "get all set field by hash key failed", "set_key", setKey, "error", err)
return false, err return false, err
} }
return result, nil return result, nil

View File

@ -43,7 +43,6 @@ type redissionLocker struct {
subExitChan chan struct{} subExitChan chan struct{}
client *redis.Client client *redis.Client
refreshOnce *sync.Once refreshOnce *sync.Once
Logger *zap.Logger
} }
func (rl *redissionLocker) Lock(ctx context.Context, timeout ...time.Duration) error { func (rl *redissionLocker) Lock(ctx context.Context, timeout ...time.Duration) error {
@ -52,7 +51,7 @@ func (rl *redissionLocker) Lock(ctx context.Context, timeout ...time.Duration) e
} }
result := rl.tryLock(ctx).(*constant.RedisResult) result := rl.tryLock(ctx).(*constant.RedisResult)
if result.Code == constant.UnknownInternalError { if result.Code == constant.UnknownInternalError {
rl.Logger.Error(result.OutputResultMessage()) logger.Error(ctx, result.OutputResultMessage())
return fmt.Errorf("get lock failed:%w", result) return fmt.Errorf("get lock failed:%w", result)
} }
@ -68,7 +67,7 @@ func (rl *redissionLocker) Lock(ctx context.Context, timeout ...time.Duration) e
defer close(subMsg) defer close(subMsg)
sub := rl.client.Subscribe(ctx, rl.waitChanKey) sub := rl.client.Subscribe(ctx, rl.waitChanKey)
defer sub.Close() defer sub.Close()
go rl.subscribeLock(sub, subMsg) go rl.subscribeLock(ctx, sub, subMsg)
if len(timeout) > 0 && timeout[0] > 0 { if len(timeout) > 0 && timeout[0] > 0 {
acquireTimer := time.NewTimer(timeout[0]) acquireTimer := time.NewTimer(timeout[0])
@ -77,23 +76,23 @@ func (rl *redissionLocker) Lock(ctx context.Context, timeout ...time.Duration) e
case _, ok := <-subMsg: case _, ok := <-subMsg:
if !ok { if !ok {
err := errors.New("failed to read the lock waiting for for the channel message") err := errors.New("failed to read the lock waiting for for the channel message")
rl.Logger.Error("failed to read the lock waiting for for the channel message") logger.Error(ctx, "failed to read the lock waiting for for the channel message")
return err return err
} }
resultErr := rl.tryLock(ctx).(*constant.RedisResult) resultErr := rl.tryLock(ctx).(*constant.RedisResult)
if (resultErr.Code == constant.LockFailure) || (resultErr.Code == constant.UnknownInternalError) { if (resultErr.Code == constant.LockFailure) || (resultErr.Code == constant.UnknownInternalError) {
rl.Logger.Info(resultErr.OutputResultMessage()) logger.Info(ctx, resultErr.OutputResultMessage())
continue continue
} }
if resultErr.Code == constant.LockSuccess { if resultErr.Code == constant.LockSuccess {
rl.Logger.Info(resultErr.OutputResultMessage()) logger.Info(ctx, resultErr.OutputResultMessage())
return nil return nil
} }
case <-acquireTimer.C: case <-acquireTimer.C:
err := errors.New("the waiting time for obtaining the lock operation has timed out") err := errors.New("the waiting time for obtaining the lock operation has timed out")
rl.Logger.Info("the waiting time for obtaining the lock operation has timed out") logger.Info(ctx, "the waiting time for obtaining the lock operation has timed out")
return err return err
} }
} }
@ -101,11 +100,11 @@ func (rl *redissionLocker) Lock(ctx context.Context, timeout ...time.Duration) e
return fmt.Errorf("lock the redis lock failed:%w", result) return fmt.Errorf("lock the redis lock failed:%w", result)
} }
func (rl *redissionLocker) subscribeLock(sub *redis.PubSub, subMsgChan chan struct{}) { func (rl *redissionLocker) subscribeLock(ctx context.Context, sub *redis.PubSub, subMsgChan chan struct{}) {
if sub == nil || subMsgChan == nil { if sub == nil || subMsgChan == nil {
return return
} }
rl.Logger.Info("lock: enter sub routine", zap.String("token", rl.Token)) logger.Info(ctx, "lock: enter sub routine", zap.String("token", rl.Token))
for { for {
select { select {
@ -126,7 +125,7 @@ ARGV[1]:锁的过期时间lockLeaseTime,单位为秒。
ARGV[2]:当前客户端的唯一标识token,用于区分不同的客户端 ARGV[2]:当前客户端的唯一标识token,用于区分不同的客户端
*/ */
func (rl *redissionLocker) refreshLockTimeout(ctx context.Context) { func (rl *redissionLocker) refreshLockTimeout(ctx context.Context) {
rl.Logger.Info("lock refresh by key and token", zap.String("token", rl.Token), zap.String("key", rl.Key)) logger.Info(ctx, "lock refresh by key and token", zap.String("token", rl.Token), zap.String("key", rl.Key))
lockTime := time.Duration(rl.lockLeaseTime/3) * time.Millisecond lockTime := time.Duration(rl.lockLeaseTime/3) * time.Millisecond
timer := time.NewTimer(lockTime) timer := time.NewTimer(lockTime)
@ -139,17 +138,17 @@ func (rl *redissionLocker) refreshLockTimeout(ctx context.Context) {
res := rl.client.Eval(ctx, luascript.RefreshLockScript, []string{rl.Key}, rl.lockLeaseTime, rl.Token) res := rl.client.Eval(ctx, luascript.RefreshLockScript, []string{rl.Key}, rl.lockLeaseTime, rl.Token)
val, err := res.Int() val, err := res.Int()
if err != redis.Nil && err != nil { if err != redis.Nil && err != nil {
rl.Logger.Info("lock refresh failed", zap.String("token", rl.Token), zap.String("key", rl.Key), zap.Error(err)) logger.Info(ctx, "lock refresh failed", "token", rl.Token, "key", rl.Key, "error", err)
return return
} }
if constant.RedisCode(val) == constant.RefreshLockFailure { if constant.RedisCode(val) == constant.RefreshLockFailure {
rl.Logger.Error("lock refreash failed,can not find the lock by key and token", zap.String("token", rl.Token), zap.String("key", rl.Key)) logger.Error(ctx, "lock refreash failed,can not find the lock by key and token", "token", rl.Token, "key", rl.Key)
break break
} }
if constant.RedisCode(val) == constant.RefreshLockSuccess { if constant.RedisCode(val) == constant.RefreshLockSuccess {
rl.Logger.Info("lock refresh success by key and token", zap.String("token", rl.Token), zap.String("key", rl.Key)) logger.Info(ctx, "lock refresh success by key and token", "token", rl.Token, "key", rl.Key)
} }
timer.Reset(lockTime) timer.Reset(lockTime)
case <-rl.refreshExitChan: case <-rl.refreshExitChan:
@ -165,11 +164,11 @@ func (rl *redissionLocker) cancelRefreshLockTime() {
} }
} }
func (rl *redissionLocker) closeSub(sub *redis.PubSub, noticeChan chan struct{}) { func (rl *redissionLocker) closeSub(ctx context.Context, sub *redis.PubSub, noticeChan chan struct{}) {
if sub != nil { if sub != nil {
err := sub.Close() err := sub.Close()
if err != nil { if err != nil {
rl.Logger.Error("close sub failed", zap.String("token", rl.Token), zap.String("key", rl.Key), zap.Error(err)) logger.Error(ctx, "close sub failed", "token", rl.Token, "key", rl.Key, "error", err)
} }
} }
@ -203,7 +202,7 @@ func (rl *redissionLocker) UnLock(ctx context.Context) error {
res := rl.client.Eval(ctx, luascript.UnLockScript, []string{rl.Key, rl.waitChanKey}, unlockMessage, rl.Token) res := rl.client.Eval(ctx, luascript.UnLockScript, []string{rl.Key, rl.waitChanKey}, unlockMessage, rl.Token)
val, err := res.Int() val, err := res.Int()
if err != redis.Nil && err != nil { if err != redis.Nil && err != nil {
rl.Logger.Info("unlock lock failed", zap.String("token", rl.Token), zap.String("key", rl.Key), zap.Error(err)) logger.Info(ctx, "unlock lock failed", zap.String("token", rl.Token), zap.String("key", rl.Key), zap.Error(err))
return fmt.Errorf("unlock lock failed:%w", constant.NewRedisResult(constant.UnknownInternalError, constant.UnLockType, err.Error())) return fmt.Errorf("unlock lock failed:%w", constant.NewRedisResult(constant.UnknownInternalError, constant.UnLockType, err.Error()))
} }
@ -212,12 +211,12 @@ func (rl *redissionLocker) UnLock(ctx context.Context) error {
rl.cancelRefreshLockTime() rl.cancelRefreshLockTime()
} }
rl.Logger.Info("unlock lock success", zap.String("token", rl.Token), zap.String("key", rl.Key)) logger.Info(ctx, "unlock lock success", zap.String("token", rl.Token), zap.String("key", rl.Key))
return nil return nil
} }
if constant.RedisCode(val) == constant.UnLocakFailureWithLockOccupancy { if constant.RedisCode(val) == constant.UnLocakFailureWithLockOccupancy {
rl.Logger.Info("unlock lock failed", zap.String("token", rl.Token), zap.String("key", rl.Key)) logger.Info(ctx, "unlock lock failed", zap.String("token", rl.Token), zap.String("key", rl.Key))
return fmt.Errorf("unlock lock failed:%w", constant.NewRedisResult(constant.UnLocakFailureWithLockOccupancy, constant.UnLockType, "")) return fmt.Errorf("unlock lock failed:%w", constant.NewRedisResult(constant.UnLocakFailureWithLockOccupancy, constant.UnLockType, ""))
} }
return nil return nil
@ -252,7 +251,6 @@ func GetLocker(client *redis.Client, ops *RedissionLockConfig) *redissionLocker
needRefresh: ops.NeedRefresh, needRefresh: ops.NeedRefresh,
client: client, client: client,
refreshExitChan: make(chan struct{}), refreshExitChan: make(chan struct{}),
Logger: logger.GetLoggerInstance(),
} }
return r return r
} }

View File

@ -14,7 +14,6 @@ import (
uuid "github.com/gofrs/uuid" uuid "github.com/gofrs/uuid"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
"go.uber.org/zap"
) )
type RedissionRWLocker struct { type RedissionRWLocker struct {
@ -27,7 +26,7 @@ type RedissionRWLocker struct {
func (rl *RedissionRWLocker) RLock(ctx context.Context, timeout ...time.Duration) error { func (rl *RedissionRWLocker) RLock(ctx context.Context, timeout ...time.Duration) error {
result := rl.tryRLock(ctx).(*constant.RedisResult) result := rl.tryRLock(ctx).(*constant.RedisResult)
if result.Code == constant.UnknownInternalError { if result.Code == constant.UnknownInternalError {
rl.Logger.Error(result.OutputResultMessage()) logger.Error(ctx, result.OutputResultMessage())
return fmt.Errorf("get read lock failed:%w", result) return fmt.Errorf("get read lock failed:%w", result)
} }
@ -42,7 +41,7 @@ func (rl *RedissionRWLocker) RLock(ctx context.Context, timeout ...time.Duration
go rl.refreshLockTimeout(ctx) go rl.refreshLockTimeout(ctx)
}) })
} }
rl.Logger.Info("success get the read lock by key and token", zap.String("key", rl.Key), zap.String("token", rl.Token)) logger.Info(ctx, "success get the read lock by key and token", "key", rl.Key, "token", rl.Token)
return nil return nil
} }
@ -53,7 +52,7 @@ func (rl *RedissionRWLocker) RLock(ctx context.Context, timeout ...time.Duration
subMsgChan := make(chan struct{}, 1) subMsgChan := make(chan struct{}, 1)
sub := rl.client.Subscribe(ctx, rl.readWaitChanKey) sub := rl.client.Subscribe(ctx, rl.readWaitChanKey)
go rl.subscribeLock(sub, subMsgChan) go rl.subscribeLock(ctx, sub, subMsgChan)
acquireTimer := time.NewTimer(timeout[0]) acquireTimer := time.NewTimer(timeout[0])
for { for {
@ -61,19 +60,19 @@ func (rl *RedissionRWLocker) RLock(ctx context.Context, timeout ...time.Duration
case _, ok := <-subMsgChan: case _, ok := <-subMsgChan:
if !ok { if !ok {
err := errors.New("failed to read the read lock waiting for for the channel message") err := errors.New("failed to read the read lock waiting for for the channel message")
rl.Logger.Error("failed to read the read lock waiting for for the channel message") logger.Error(ctx, "failed to read the read lock waiting for for the channel message")
return err return err
} }
result := rl.tryRLock(ctx).(*constant.RedisResult) result := rl.tryRLock(ctx).(*constant.RedisResult)
if (result.Code == constant.RLockFailureWithWLockOccupancy) || (result.Code == constant.UnknownInternalError) { if (result.Code == constant.RLockFailureWithWLockOccupancy) || (result.Code == constant.UnknownInternalError) {
rl.Logger.Info(result.OutputResultMessage()) logger.Info(ctx, result.OutputResultMessage())
continue continue
} }
if result.Code == constant.LockSuccess { if result.Code == constant.LockSuccess {
rl.Logger.Info(result.OutputResultMessage()) logger.Info(ctx, result.OutputResultMessage())
rl.closeSub(sub, rl.subExitChan) rl.closeSub(ctx, sub, rl.subExitChan)
if rl.needRefresh { if rl.needRefresh {
rl.refreshOnce.Do(func() { rl.refreshOnce.Do(func() {
@ -88,8 +87,8 @@ func (rl *RedissionRWLocker) RLock(ctx context.Context, timeout ...time.Duration
return nil return nil
} }
case <-acquireTimer.C: case <-acquireTimer.C:
rl.Logger.Info("the waiting time for obtaining the read lock operation has timed out") logger.Info(ctx, "the waiting time for obtaining the read lock operation has timed out")
rl.closeSub(sub, rl.subExitChan) rl.closeSub(ctx, sub, rl.subExitChan)
// after acquire lock timeout,notice the sub channel to close // after acquire lock timeout,notice the sub channel to close
return constant.AcquireTimeoutErr return constant.AcquireTimeoutErr
} }
@ -110,7 +109,7 @@ func (rl *RedissionRWLocker) tryRLock(ctx context.Context) error {
} }
func (rl *RedissionRWLocker) refreshLockTimeout(ctx context.Context) { func (rl *RedissionRWLocker) refreshLockTimeout(ctx context.Context) {
rl.Logger.Info("lock refresh by key and token", zap.String("token", rl.Token), zap.String("key", rl.Key)) logger.Info(ctx, "lock refresh by key and token", "token", rl.Token, "key", rl.Key)
lockTime := time.Duration(rl.lockLeaseTime/3) * time.Millisecond lockTime := time.Duration(rl.lockLeaseTime/3) * time.Millisecond
timer := time.NewTimer(lockTime) timer := time.NewTimer(lockTime)
@ -123,17 +122,17 @@ func (rl *RedissionRWLocker) refreshLockTimeout(ctx context.Context) {
res := rl.client.Eval(ctx, luascript.RefreshRWLockScript, []string{rl.Key, rl.RWTokenTimeoutPrefix}, rl.lockLeaseTime, rl.Token) res := rl.client.Eval(ctx, luascript.RefreshRWLockScript, []string{rl.Key, rl.RWTokenTimeoutPrefix}, rl.lockLeaseTime, rl.Token)
val, err := res.Int() val, err := res.Int()
if err != redis.Nil && err != nil { if err != redis.Nil && err != nil {
rl.Logger.Info("lock refresh failed", zap.String("token", rl.Token), zap.String("key", rl.Key), zap.Error(err)) logger.Info(ctx, "lock refresh failed", "token", rl.Token, "key", rl.Key, "error", err)
return return
} }
if constant.RedisCode(val) == constant.RefreshLockFailure { if constant.RedisCode(val) == constant.RefreshLockFailure {
rl.Logger.Error("lock refreash failed,can not find the read lock by key and token", zap.String("rwTokenPrefix", rl.RWTokenTimeoutPrefix), zap.String("token", rl.Token), zap.String("key", rl.Key)) logger.Error(ctx, "lock refreash failed,can not find the read lock by key and token", "rwTokenPrefix", rl.RWTokenTimeoutPrefix, "token", rl.Token, "key", rl.Key)
return return
} }
if constant.RedisCode(val) == constant.RefreshLockSuccess { if constant.RedisCode(val) == constant.RefreshLockSuccess {
rl.Logger.Info("lock refresh success by key and token", zap.String("token", rl.Token), zap.String("key", rl.Key)) logger.Info(ctx, "lock refresh success by key and token", "token", rl.Token, "key", rl.Key)
} }
timer.Reset(lockTime) timer.Reset(lockTime)
case <-rl.refreshExitChan: case <-rl.refreshExitChan:
@ -143,11 +142,11 @@ func (rl *RedissionRWLocker) refreshLockTimeout(ctx context.Context) {
} }
func (rl *RedissionRWLocker) UnRLock(ctx context.Context) error { func (rl *RedissionRWLocker) UnRLock(ctx context.Context) error {
rl.Logger.Info("unlock RLock by key and token", zap.String("key", rl.Key), zap.String("token", rl.Token)) logger.Info(ctx, "unlock RLock by key and token", "key", rl.Key, "token", rl.Token)
res := rl.client.Eval(ctx, luascript.UnRLockScript, []string{rl.Key, rl.RWTokenTimeoutPrefix, rl.writeWaitChanKey}, unlockMessage, rl.Token) res := rl.client.Eval(ctx, luascript.UnRLockScript, []string{rl.Key, rl.RWTokenTimeoutPrefix, rl.writeWaitChanKey}, unlockMessage, rl.Token)
val, err := res.Int() val, err := res.Int()
if err != redis.Nil && err != nil { if err != redis.Nil && err != nil {
rl.Logger.Info("unlock read lock failed", zap.String("token", rl.Token), zap.String("key", rl.Key), zap.Error(err)) logger.Info(ctx, "unlock read lock failed", "token", rl.Token, "key", rl.Key, "error", err)
return fmt.Errorf("unlock read lock failed:%w", constant.NewRedisResult(constant.UnknownInternalError, constant.UnRLockType, err.Error())) return fmt.Errorf("unlock read lock failed:%w", constant.NewRedisResult(constant.UnknownInternalError, constant.UnRLockType, err.Error()))
} }
@ -156,12 +155,12 @@ func (rl *RedissionRWLocker) UnRLock(ctx context.Context) error {
rl.cancelRefreshLockTime() rl.cancelRefreshLockTime()
} }
rl.Logger.Info("unlock read lock success", zap.String("token", rl.Token), zap.String("key", rl.Key)) logger.Info(ctx, "unlock read lock success", "token", rl.Token, "key", rl.Key)
return nil return nil
} }
if constant.RedisCode(val) == constant.UnRLockFailureWithWLockOccupancy { if constant.RedisCode(val) == constant.UnRLockFailureWithWLockOccupancy {
rl.Logger.Info("unlock read lock failed", zap.String("token", rl.Token), zap.String("key", rl.Key)) logger.Info(ctx, "unlock read lock failed", "token", rl.Token, "key", rl.Key)
return fmt.Errorf("unlock read lock failed:%w", constant.NewRedisResult(constant.UnRLockFailureWithWLockOccupancy, constant.UnRLockType, "")) return fmt.Errorf("unlock read lock failed:%w", constant.NewRedisResult(constant.UnRLockFailureWithWLockOccupancy, constant.UnRLockType, ""))
} }
return nil return nil
@ -170,7 +169,7 @@ func (rl *RedissionRWLocker) UnRLock(ctx context.Context) error {
func (rl *RedissionRWLocker) WLock(ctx context.Context, timeout ...time.Duration) error { func (rl *RedissionRWLocker) WLock(ctx context.Context, timeout ...time.Duration) error {
result := rl.tryWLock(ctx).(*constant.RedisResult) result := rl.tryWLock(ctx).(*constant.RedisResult)
if result.Code == constant.UnknownInternalError { if result.Code == constant.UnknownInternalError {
rl.Logger.Error(result.OutputResultMessage()) logger.Error(ctx, result.OutputResultMessage())
return fmt.Errorf("get write lock failed:%w", result) return fmt.Errorf("get write lock failed:%w", result)
} }
@ -185,7 +184,7 @@ func (rl *RedissionRWLocker) WLock(ctx context.Context, timeout ...time.Duration
go rl.refreshLockTimeout(ctx) go rl.refreshLockTimeout(ctx)
}) })
} }
rl.Logger.Info("success get the write lock by key and token", zap.String("key", rl.Key), zap.String("token", rl.Token)) logger.Info(ctx, "success get the write lock by key and token", "key", rl.Key, "token", rl.Token)
return nil return nil
} }
@ -196,7 +195,7 @@ func (rl *RedissionRWLocker) WLock(ctx context.Context, timeout ...time.Duration
subMsgChan := make(chan struct{}, 1) subMsgChan := make(chan struct{}, 1)
sub := rl.client.Subscribe(ctx, rl.writeWaitChanKey) sub := rl.client.Subscribe(ctx, rl.writeWaitChanKey)
go rl.subscribeLock(sub, subMsgChan) go rl.subscribeLock(ctx, sub, subMsgChan)
acquireTimer := time.NewTimer(timeout[0]) acquireTimer := time.NewTimer(timeout[0])
for { for {
@ -204,19 +203,19 @@ func (rl *RedissionRWLocker) WLock(ctx context.Context, timeout ...time.Duration
case _, ok := <-subMsgChan: case _, ok := <-subMsgChan:
if !ok { if !ok {
err := errors.New("failed to read the write lock waiting for for the channel message") err := errors.New("failed to read the write lock waiting for for the channel message")
rl.Logger.Error("failed to read the read lock waiting for for the channel message") logger.Error(ctx, "failed to read the read lock waiting for for the channel message")
return err return err
} }
result := rl.tryWLock(ctx).(*constant.RedisResult) result := rl.tryWLock(ctx).(*constant.RedisResult)
if (result.Code == constant.UnknownInternalError) || (result.Code == constant.WLockFailureWithRLockOccupancy) || (result.Code == constant.WLockFailureWithWLockOccupancy) || (result.Code == constant.WLockFailureWithNotFirstPriority) { if (result.Code == constant.UnknownInternalError) || (result.Code == constant.WLockFailureWithRLockOccupancy) || (result.Code == constant.WLockFailureWithWLockOccupancy) || (result.Code == constant.WLockFailureWithNotFirstPriority) {
rl.Logger.Info(result.OutputResultMessage()) logger.Info(ctx, result.OutputResultMessage())
continue continue
} }
if result.Code == constant.LockSuccess { if result.Code == constant.LockSuccess {
rl.Logger.Info(result.OutputResultMessage()) logger.Info(ctx, result.OutputResultMessage())
rl.closeSub(sub, rl.subExitChan) rl.closeSub(ctx, sub, rl.subExitChan)
if rl.needRefresh { if rl.needRefresh {
rl.refreshOnce.Do(func() { rl.refreshOnce.Do(func() {
@ -231,8 +230,8 @@ func (rl *RedissionRWLocker) WLock(ctx context.Context, timeout ...time.Duration
return nil return nil
} }
case <-acquireTimer.C: case <-acquireTimer.C:
rl.Logger.Info("the waiting time for obtaining the write lock operation has timed out") logger.Info(ctx, "the waiting time for obtaining the write lock operation has timed out")
rl.closeSub(sub, rl.subExitChan) rl.closeSub(ctx, sub, rl.subExitChan)
// after acquire lock timeout,notice the sub channel to close // after acquire lock timeout,notice the sub channel to close
return constant.AcquireTimeoutErr return constant.AcquireTimeoutErr
} }
@ -256,7 +255,7 @@ func (rl *RedissionRWLocker) UnWLock(ctx context.Context) error {
res := rl.client.Eval(ctx, luascript.UnWLockScript, []string{rl.Key, rl.RWTokenTimeoutPrefix, rl.writeWaitChanKey, rl.readWaitChanKey}, unlockMessage, rl.Token) res := rl.client.Eval(ctx, luascript.UnWLockScript, []string{rl.Key, rl.RWTokenTimeoutPrefix, rl.writeWaitChanKey, rl.readWaitChanKey}, unlockMessage, rl.Token)
val, err := res.Int() val, err := res.Int()
if err != redis.Nil && err != nil { if err != redis.Nil && err != nil {
rl.Logger.Error("unlock write lock failed", zap.String("token", rl.Token), zap.String("key", rl.Key), zap.Error(err)) logger.Error(ctx, "unlock write lock failed", "token", rl.Token, "key", rl.Key, "error", err)
return fmt.Errorf("unlock write lock failed:%w", constant.NewRedisResult(constant.UnknownInternalError, constant.UnWLockType, err.Error())) return fmt.Errorf("unlock write lock failed:%w", constant.NewRedisResult(constant.UnknownInternalError, constant.UnWLockType, err.Error()))
} }
@ -264,12 +263,12 @@ func (rl *RedissionRWLocker) UnWLock(ctx context.Context) error {
if rl.needRefresh && (constant.RedisCode(val) == constant.UnLockSuccess) { if rl.needRefresh && (constant.RedisCode(val) == constant.UnLockSuccess) {
rl.cancelRefreshLockTime() rl.cancelRefreshLockTime()
} }
rl.Logger.Info("unlock write lock success", zap.String("token", rl.Token), zap.String("key", rl.Key)) logger.Info(ctx, "unlock write lock success", "token", rl.Token, "key", rl.Key)
return nil return nil
} }
if (constant.RedisCode(val) == constant.UnWLockFailureWithRLockOccupancy) || (constant.RedisCode(val) == constant.UnWLockFailureWithWLockOccupancy) { if (constant.RedisCode(val) == constant.UnWLockFailureWithRLockOccupancy) || (constant.RedisCode(val) == constant.UnWLockFailureWithWLockOccupancy) {
rl.Logger.Info("unlock write lock failed", zap.String("token", rl.Token), zap.String("key", rl.Key)) logger.Info(ctx, "unlock write lock failed", "token", rl.Token, "key", rl.Key)
return fmt.Errorf("unlock write lock failed:%w", constant.NewRedisResult(constant.RedisCode(val), constant.UnWLockType, "")) return fmt.Errorf("unlock write lock failed:%w", constant.NewRedisResult(constant.RedisCode(val), constant.UnWLockType, ""))
} }
return nil return nil
@ -308,7 +307,6 @@ func GetRWLocker(client *redis.Client, conf *RedissionLockConfig) *RedissionRWLo
lockLeaseTime: conf.LockLeaseTime, lockLeaseTime: conf.LockLeaseTime,
client: client, client: client,
refreshOnce: &sync.Once{}, refreshOnce: &sync.Once{},
Logger: logger.GetLoggerInstance(),
} }
rwLocker := &RedissionRWLocker{ rwLocker := &RedissionRWLocker{

View File

@ -11,20 +11,17 @@ import (
"modelRT/network" "modelRT/network"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.uber.org/zap"
) )
// QueryAlertEventHandler define query alert event process API // QueryAlertEventHandler define query alert event process API
func QueryAlertEventHandler(c *gin.Context) { func QueryAlertEventHandler(c *gin.Context) {
var targetLevel constant.AlertLevel var targetLevel constant.AlertLevel
logger := logger.GetLoggerInstance()
alertManger := alert.GetAlertMangerInstance() alertManger := alert.GetAlertMangerInstance()
levelStr := c.Query("level") levelStr := c.Query("level")
level, err := strconv.Atoi(levelStr) level, err := strconv.Atoi(levelStr)
if err != nil { if err != nil {
logger.Error("convert alert level string to int failed", zap.Error(err)) logger.Error(c, "convert alert level string to int failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: -1, Code: -1,

View File

@ -16,21 +16,19 @@ import (
"modelRT/orm" "modelRT/orm"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.uber.org/zap"
) )
// ComponentAnchorReplaceHandler define component anchor point replace process API // ComponentAnchorReplaceHandler define component anchor point replace process API
func ComponentAnchorReplaceHandler(c *gin.Context) { func ComponentAnchorReplaceHandler(c *gin.Context) {
var uuid, anchorName string var uuid, anchorName string
logger := logger.GetLoggerInstance()
pgClient := database.GetPostgresDBClient()
pgClient := database.GetPostgresDBClient()
cancelCtx, cancel := context.WithTimeout(c, 5*time.Second) cancelCtx, cancel := context.WithTimeout(c, 5*time.Second)
defer cancel() defer cancel()
var request network.ComponetAnchorReplaceRequest var request network.ComponetAnchorReplaceRequest
if err := c.ShouldBindJSON(&request); err != nil { if err := c.ShouldBindJSON(&request); err != nil {
logger.Error("unmarshal component anchor point replace info failed", zap.Error(err)) logger.Error(c, "unmarshal component anchor point replace info failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -45,7 +43,7 @@ func ComponentAnchorReplaceHandler(c *gin.Context) {
var componentInfo orm.Component var componentInfo orm.Component
result := pgClient.WithContext(cancelCtx).Model(&orm.Component{}).Where("global_uuid = ?", uuid).Find(&componentInfo) result := pgClient.WithContext(cancelCtx).Model(&orm.Component{}).Where("global_uuid = ?", uuid).Find(&componentInfo)
if result.Error != nil { if result.Error != nil {
logger.Error("query component detail info failed", zap.Error(result.Error)) logger.Error(c, "query component detail info failed", "error", result.Error)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -57,7 +55,7 @@ func ComponentAnchorReplaceHandler(c *gin.Context) {
if result.RowsAffected == 0 { if result.RowsAffected == 0 {
err := fmt.Errorf("query component detail info by uuid failed:%w", constant.ErrQueryRowZero) err := fmt.Errorf("query component detail info by uuid failed:%w", constant.ErrQueryRowZero)
logger.Error("query component detail info from table is empty", zap.String("table_name", "component")) logger.Error(c, "query component detail info from table is empty", "table_name", "component")
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -73,7 +71,7 @@ func ComponentAnchorReplaceHandler(c *gin.Context) {
tableName := model.SelectModelNameByType(componentInfo.ComponentType) tableName := model.SelectModelNameByType(componentInfo.ComponentType)
result = pgClient.WithContext(cancelCtx).Table(tableName).Where("global_uuid = ?", uuid).Find(&unmarshalMap) result = pgClient.WithContext(cancelCtx).Table(tableName).Where("global_uuid = ?", uuid).Find(&unmarshalMap)
if result.Error != nil { if result.Error != nil {
logger.Error("query model detail info failed", zap.Error(result.Error)) logger.Error(c, "query model detail info failed", "error", result.Error)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -85,7 +83,7 @@ func ComponentAnchorReplaceHandler(c *gin.Context) {
if unmarshalMap == nil { if unmarshalMap == nil {
err := fmt.Errorf("query model detail info by uuid failed:%w", constant.ErrQueryRowZero) err := fmt.Errorf("query model detail info by uuid failed:%w", constant.ErrQueryRowZero)
logger.Error("query model detail info from table is empty", zap.String("table_name", tableName)) logger.Error(c, "query model detail info from table is empty", "table_name", tableName)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -97,7 +95,7 @@ func ComponentAnchorReplaceHandler(c *gin.Context) {
componentType := unmarshalMap["component_type"].(int) componentType := unmarshalMap["component_type"].(int)
if componentType != constant.DemoType { if componentType != constant.DemoType {
logger.Error("can not process real time data of component type not equal DemoType", zap.Int64("component_id", componentInfo.ID)) logger.Error(c, "can not process real time data of component type not equal DemoType", "component_id", componentInfo.ID)
} }
diagram.UpdateAnchorValue(componentInfo.ID, anchorName) diagram.UpdateAnchorValue(componentInfo.ID, anchorName)

View File

@ -13,17 +13,15 @@ import (
"github.com/bitly/go-simplejson" "github.com/bitly/go-simplejson"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"go.uber.org/zap"
) )
// CircuitDiagramCreateHandler define circuit diagram create process API // CircuitDiagramCreateHandler define circuit diagram create process API
func CircuitDiagramCreateHandler(c *gin.Context) { func CircuitDiagramCreateHandler(c *gin.Context) {
logger := logger.GetLoggerInstance()
pgClient := database.GetPostgresDBClient() pgClient := database.GetPostgresDBClient()
var request network.CircuitDiagramCreateRequest var request network.CircuitDiagramCreateRequest
if err := c.ShouldBindJSON(&request); err != nil { if err := c.ShouldBindJSON(&request); err != nil {
logger.Error("unmarshal circuit diagram create info failed", zap.Error(err)) logger.Error(c, "unmarshal circuit diagram create info failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -35,7 +33,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
graph, err := diagram.GetGraphMap(request.PageID) graph, err := diagram.GetGraphMap(request.PageID)
if err != nil { if err != nil {
logger.Error("get topologic data from set by pageID failed", zap.Error(err)) logger.Error(c, "get topologic data from set by pageID failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -63,7 +61,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
err = fmt.Errorf("convert uuid from string failed:%w:%w", err1, err2) err = fmt.Errorf("convert uuid from string failed:%w:%w", err1, err2)
} }
logger.Error("format uuid from string failed", zap.Error(err)) logger.Error(c, "format uuid from string failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -87,7 +85,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
logger.Error("create topologic info into DB failed", zap.Any("topologic_info", topologicCreateInfos), zap.Error(err)) logger.Error(c, "create topologic info into DB failed", "topologic_info", topologicCreateInfos, "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -109,7 +107,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
logger.Error("insert component info into DB failed", zap.Error(err)) logger.Error(c, "insert component info into DB failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -127,7 +125,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
logger.Error("create component model into DB failed", zap.Any("component_infos", request.ComponentInfos), zap.Error(err)) logger.Error(c, "create component model into DB failed", "component_infos", request.ComponentInfos, "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -147,7 +145,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
logger.Error("unmarshal component params info failed", zap.String("component_params", componentInfo.Params), zap.Error(err)) logger.Error(c, "unmarshal component params info failed", "component_params", componentInfo.Params, "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -165,7 +163,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
logger.Error("format params json info to map failed", zap.Error(err)) logger.Error(c, "format params json info to map failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,

View File

@ -17,18 +17,16 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"go.uber.org/zap"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
) )
// CircuitDiagramDeleteHandler define circuit diagram delete process API // CircuitDiagramDeleteHandler define circuit diagram delete process API
func CircuitDiagramDeleteHandler(c *gin.Context) { func CircuitDiagramDeleteHandler(c *gin.Context) {
logger := logger.GetLoggerInstance()
pgClient := database.GetPostgresDBClient() pgClient := database.GetPostgresDBClient()
var request network.CircuitDiagramDeleteRequest var request network.CircuitDiagramDeleteRequest
if err := c.ShouldBindJSON(&request); err != nil { if err := c.ShouldBindJSON(&request); err != nil {
logger.Error("unmarshal circuit diagram del info failed", zap.Error(err)) logger.Error(c, "unmarshal circuit diagram del info failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -40,7 +38,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
graph, err := diagram.GetGraphMap(request.PageID) graph, err := diagram.GetGraphMap(request.PageID)
if err != nil { if err != nil {
logger.Error("get topologic data from set by pageID failed", zap.Error(err)) logger.Error(c, "get topologic data from set by pageID failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -68,7 +66,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
err = fmt.Errorf("convert uuid from string failed:%w:%w", err1, err2) err = fmt.Errorf("convert uuid from string failed:%w:%w", err1, err2)
} }
logger.Error("format uuid from string failed", zap.Error(err)) logger.Error(c, "format uuid from string failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -93,7 +91,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
logger.Error("delete topologic info into DB failed", zap.Any("topologic_info", topologicDelInfo), zap.Error(err)) logger.Error(c, "delete topologic info into DB failed", "topologic_info", topologicDelInfo, "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -110,7 +108,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
logger.Error("delete topologic info failed", zap.Any("topologic_info", topologicDelInfo), zap.Error(err)) logger.Error(c, "delete topologic info failed", "topologic_info", topologicDelInfo, "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -136,7 +134,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
logger.Error("format uuid from string failed", zap.Error(err)) logger.Error(c, "format uuid from string failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -160,7 +158,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
err = fmt.Errorf("%w:please check uuid conditions", constant.ErrDeleteRowZero) err = fmt.Errorf("%w:please check uuid conditions", constant.ErrDeleteRowZero)
} }
logger.Error("query component info into postgresDB failed", zap.String("component_global_uuid", componentInfo.UUID), zap.Error(err)) logger.Error(c, "query component info into postgresDB failed", "component_global_uuid", componentInfo.UUID, "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -182,7 +180,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
err = fmt.Errorf("%w:please check uuid conditions", constant.ErrDeleteRowZero) err = fmt.Errorf("%w:please check uuid conditions", constant.ErrDeleteRowZero)
} }
logger.Error("delete component info into postgresDB failed", zap.String("component_global_uuid", componentInfo.UUID), zap.Error(err)) logger.Error(c, "delete component info into postgresDB failed", "component_global_uuid", componentInfo.UUID, "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -207,7 +205,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
} }
msg := fmt.Sprintf("delete component info from table %s failed", modelStruct.ReturnTableName()) msg := fmt.Sprintf("delete component info from table %s failed", modelStruct.ReturnTableName())
logger.Error(msg, zap.String("component_global_uuid", componentInfo.UUID), zap.Error(err)) logger.Error(c, msg, "component_global_uuid", componentInfo.UUID, "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,

View File

@ -11,7 +11,6 @@ import (
"modelRT/network" "modelRT/network"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.uber.org/zap"
) )
// CircuitDiagramLoadHandler define circuit diagram load process API // CircuitDiagramLoadHandler define circuit diagram load process API
@ -25,12 +24,11 @@ import (
// @Failure 400 {object} network.FailureResponse "request process failed" // @Failure 400 {object} network.FailureResponse "request process failed"
// @Router /model/diagram_load/{page_id} [get] // @Router /model/diagram_load/{page_id} [get]
func CircuitDiagramLoadHandler(c *gin.Context) { func CircuitDiagramLoadHandler(c *gin.Context) {
logger := logger.GetLoggerInstance()
pgClient := database.GetPostgresDBClient() pgClient := database.GetPostgresDBClient()
pageID, err := strconv.ParseInt(c.Query("page_id"), 10, 64) pageID, err := strconv.ParseInt(c.Query("page_id"), 10, 64)
if err != nil { if err != nil {
logger.Error("get pageID from url param failed", zap.Error(err)) logger.Error(c, "get pageID from url param failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -45,7 +43,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
topologicInfo, err := diagram.GetGraphMap(pageID) topologicInfo, err := diagram.GetGraphMap(pageID)
if err != nil { if err != nil {
logger.Error("get topologic data from set by pageID failed", zap.Error(err)) logger.Error(c, "get topologic data from set by pageID failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -66,7 +64,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
for _, componentUUID := range VerticeLink { for _, componentUUID := range VerticeLink {
component, err := database.QueryComponentByUUID(c, pgClient, componentUUID) component, err := database.QueryComponentByUUID(c, pgClient, componentUUID)
if err != nil { if err != nil {
logger.Error("get component id info from DB by uuid failed", zap.Error(err)) logger.Error(c, "get component id info from DB by uuid failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -81,7 +79,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
componentParams, err := diagram.GetComponentMap(component.ID) componentParams, err := diagram.GetComponentMap(component.ID)
if err != nil { if err != nil {
logger.Error("get component data from set by uuid failed", zap.Error(err)) logger.Error(c, "get component data from set by uuid failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -100,7 +98,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
rootVertexUUID := topologicInfo.RootVertex.String() rootVertexUUID := topologicInfo.RootVertex.String()
rootComponent, err := database.QueryComponentByUUID(c, pgClient, topologicInfo.RootVertex) rootComponent, err := database.QueryComponentByUUID(c, pgClient, topologicInfo.RootVertex)
if err != nil { if err != nil {
logger.Error("get component id info from DB by uuid failed", zap.Error(err)) logger.Error(c, "get component id info from DB by uuid failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -115,7 +113,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
rootComponentParam, err := diagram.GetComponentMap(rootComponent.ID) rootComponentParam, err := diagram.GetComponentMap(rootComponent.ID)
if err != nil { if err != nil {
logger.Error("get component data from set by uuid failed", zap.Error(err)) logger.Error(c, "get component data from set by uuid failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,

View File

@ -11,17 +11,15 @@ import (
"github.com/bitly/go-simplejson" "github.com/bitly/go-simplejson"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.uber.org/zap"
) )
// CircuitDiagramUpdateHandler define circuit diagram update process API // CircuitDiagramUpdateHandler define circuit diagram update process API
func CircuitDiagramUpdateHandler(c *gin.Context) { func CircuitDiagramUpdateHandler(c *gin.Context) {
logger := logger.GetLoggerInstance()
pgClient := database.GetPostgresDBClient() pgClient := database.GetPostgresDBClient()
var request network.CircuitDiagramUpdateRequest var request network.CircuitDiagramUpdateRequest
if err := c.ShouldBindJSON(&request); err != nil { if err := c.ShouldBindJSON(&request); err != nil {
logger.Error("unmarshal circuit diagram update info failed", zap.Error(err)) logger.Error(c, "unmarshal circuit diagram update info failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -33,7 +31,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
graph, err := diagram.GetGraphMap(request.PageID) graph, err := diagram.GetGraphMap(request.PageID)
if err != nil { if err != nil {
logger.Error("get topologic data from set by pageID failed", zap.Error(err)) logger.Error(c, "get topologic data from set by pageID failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -50,7 +48,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
for _, topologicLink := range request.TopologicLinks { for _, topologicLink := range request.TopologicLinks {
changeInfo, err := network.ParseUUID(topologicLink) changeInfo, err := network.ParseUUID(topologicLink)
if err != nil { if err != nil {
logger.Error("format uuid from string failed", zap.Error(err)) logger.Error(c, "format uuid from string failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -73,7 +71,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
logger.Error("update topologic info into DB failed", zap.Any("topologic_info", topologicChangeInfo), zap.Error(err)) logger.Error(c, "update topologic info into DB failed", "topologic_info", topologicChangeInfo, "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -90,7 +88,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
logger.Error("update topologic info failed", zap.Any("topologic_info", topologicChangeInfo), zap.Error(err)) logger.Error(c, "update topologic info failed", "topologic_info", topologicChangeInfo, "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -107,7 +105,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
for index, componentInfo := range request.ComponentInfos { for index, componentInfo := range request.ComponentInfos {
componentID, err := database.UpdateComponentIntoDB(c, tx, componentInfo) componentID, err := database.UpdateComponentIntoDB(c, tx, componentInfo)
if err != nil { if err != nil {
logger.Error("udpate component info into DB failed", zap.Error(err)) logger.Error(c, "udpate component info into DB failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -125,7 +123,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
err = database.UpdateModelIntoDB(c, tx, componentID, componentInfo.ComponentType, componentInfo.Params) err = database.UpdateModelIntoDB(c, tx, componentID, componentInfo.ComponentType, componentInfo.Params)
if err != nil { if err != nil {
logger.Error("udpate component model info into DB failed", zap.Error(err)) logger.Error(c, "udpate component model info into DB failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -143,7 +141,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
for _, componentInfo := range request.ComponentInfos { for _, componentInfo := range request.ComponentInfos {
paramsJSON, err := simplejson.NewJson([]byte(componentInfo.Params)) paramsJSON, err := simplejson.NewJson([]byte(componentInfo.Params))
if err != nil { if err != nil {
logger.Error("unmarshal component info by concurrent map failed", zap.String("component_params", componentInfo.Params), zap.Error(err)) logger.Error(c, "unmarshal component info by concurrent map failed", "component_params", componentInfo.Params, "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
@ -159,7 +157,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
componentMap, err := paramsJSON.Map() componentMap, err := paramsJSON.Map()
if err != nil { if err != nil {
logger.Error("format params json info to map failed", zap.Error(err)) logger.Error(c, "format params json info to map failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,

View File

@ -11,20 +11,18 @@ import (
"modelRT/network" "modelRT/network"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.uber.org/zap"
) )
// QueryRealTimeDataHandler define query real time data process API // QueryRealTimeDataHandler define query real time data process API
func QueryRealTimeDataHandler(c *gin.Context) { func QueryRealTimeDataHandler(c *gin.Context) {
var targetLevel constant.AlertLevel var targetLevel constant.AlertLevel
logger := logger.GetLoggerInstance()
alertManger := alert.GetAlertMangerInstance() alertManger := alert.GetAlertMangerInstance()
levelStr := c.Query("level") levelStr := c.Query("level")
level, err := strconv.Atoi(levelStr) level, err := strconv.Atoi(levelStr)
if err != nil { if err != nil {
logger.Error("convert alert level string to int failed", zap.Error(err)) logger.Error(c, "convert alert level string to int failed", "error", err)
resp := network.FailureResponse{ resp := network.FailureResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,

View File

@ -7,7 +7,6 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"go.uber.org/zap"
realtimedata "modelRT/real-time-data" realtimedata "modelRT/real-time-data"
) )
@ -19,11 +18,9 @@ var upgrader = websocket.Upgrader{
// RealTimeDataReceivehandler define real time data receive and process API // RealTimeDataReceivehandler define real time data receive and process API
func RealTimeDataReceivehandler(c *gin.Context) { func RealTimeDataReceivehandler(c *gin.Context) {
logger := logger.GetLoggerInstance()
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil) conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil { if err != nil {
logger.Error("upgrade http protocol to websocket protocal failed", zap.Error(err)) logger.Error(c, "upgrade http protocol to websocket protocal failed", "error", err)
return return
} }
defer conn.Close() defer conn.Close()
@ -31,17 +28,17 @@ func RealTimeDataReceivehandler(c *gin.Context) {
for { for {
messageType, p, err := conn.ReadMessage() messageType, p, err := conn.ReadMessage()
if err != nil { if err != nil {
logger.Error("read message from websocket connection failed", zap.Error(err)) logger.Error(c, "read message from websocket connection failed", "error", err)
respByte := processResponse(-1, "read message from websocket connection failed", nil) respByte := processResponse(-1, "read message from websocket connection failed", nil)
if len(respByte) == 0 { if len(respByte) == 0 {
logger.Error("process message from byte failed", zap.Error(err)) logger.Error(c, "process message from byte failed", "error", err)
continue continue
} }
err = conn.WriteMessage(messageType, respByte) err = conn.WriteMessage(messageType, respByte)
if err != nil { if err != nil {
logger.Error("write message to websocket connection failed", zap.Error(err)) logger.Error(c, "write message to websocket connection failed", "error", err)
continue continue
} }
continue continue
@ -50,17 +47,17 @@ func RealTimeDataReceivehandler(c *gin.Context) {
var request network.RealTimeDataReceiveRequest var request network.RealTimeDataReceiveRequest
err = jsoniter.Unmarshal([]byte(p), &request) err = jsoniter.Unmarshal([]byte(p), &request)
if err != nil { if err != nil {
logger.Error("unmarshal message from byte failed", zap.Error(err)) logger.Error(c, "unmarshal message from byte failed", "error", err)
respByte := processResponse(-1, "unmarshal message from byte failed", nil) respByte := processResponse(-1, "unmarshal message from byte failed", nil)
if len(respByte) == 0 { if len(respByte) == 0 {
logger.Error("process message from byte failed", zap.Error(err)) logger.Error(c, "process message from byte failed", "error", err)
continue continue
} }
err = conn.WriteMessage(messageType, respByte) err = conn.WriteMessage(messageType, respByte)
if err != nil { if err != nil {
logger.Error("write message to websocket connection failed", zap.Error(err)) logger.Error(c, "write message to websocket connection failed", "error", err)
continue continue
} }
continue continue
@ -74,13 +71,13 @@ func RealTimeDataReceivehandler(c *gin.Context) {
} }
respByte := processResponse(0, "success", payload) respByte := processResponse(0, "success", payload)
if len(respByte) == 0 { if len(respByte) == 0 {
logger.Error("process message from byte failed", zap.Error(err)) logger.Error(c, "process message from byte failed", "error", err)
continue continue
} }
err = conn.WriteMessage(messageType, respByte) err = conn.WriteMessage(messageType, respByte)
if err != nil { if err != nil {
logger.Error("write message to websocket connection failed", zap.Error(err)) logger.Error(c, "write message to websocket connection failed", "error", err)
continue continue
} }
} }

View File

@ -12,7 +12,6 @@ import (
"modelRT/logger" "modelRT/logger"
"github.com/panjf2000/ants/v2" "github.com/panjf2000/ants/v2"
"go.uber.org/zap"
) )
// AnchorRealTimePool define anchor param pool of real time data // AnchorRealTimePool define anchor param pool of real time data
@ -31,12 +30,11 @@ func AnchorPoolInit(concurrentQuantity int) (pool *ants.PoolWithFunc, err error)
// AnchorFunc defines func that process the real time data of component anchor params // AnchorFunc defines func that process the real time data of component anchor params
var AnchorFunc = func(poolConfig interface{}) { var AnchorFunc = func(poolConfig interface{}) {
var firstStart bool var firstStart bool
logger := logger.GetLoggerInstance()
alertManager := alert.GetAlertMangerInstance() alertManager := alert.GetAlertMangerInstance()
anchorChanConfig, ok := poolConfig.(config.AnchorChanConfig) anchorChanConfig, ok := poolConfig.(config.AnchorChanConfig)
if !ok { if !ok {
logger.Error("conversion component anchor chan type failed") logger.Error(anchorChanConfig.Ctx, "conversion component anchor chan type failed")
return return
} }
@ -56,12 +54,12 @@ var AnchorFunc = func(poolConfig interface{}) {
for _, value := range anchorRealTimeDatas { for _, value := range anchorRealTimeDatas {
anchorName, err := diagram.GetAnchorValue(componentID) anchorName, err := diagram.GetAnchorValue(componentID)
if err != nil { if err != nil {
logger.Error("can not get anchor value from map by uuid", zap.Int64("component_id", componentID), zap.Error(err)) logger.Error(anchorChanConfig.Ctx, "can not get anchor value from map by uuid", "component_id", componentID, "error", err)
continue continue
} }
if anchorName != anchorParaConfig.AnchorName { if anchorName != anchorParaConfig.AnchorName {
logger.Error("anchor name not equal param config anchor value", zap.String("map_anchor_name", anchorName), zap.String("param_anchor_name", anchorParaConfig.AnchorName)) logger.Error(anchorChanConfig.Ctx, "anchor name not equal param config anchor value", "map_anchor_name", anchorName, "param_anchor_name", anchorParaConfig.AnchorName)
continue continue
} }

View File

@ -10,21 +10,17 @@ import (
"modelRT/diagram" "modelRT/diagram"
"modelRT/logger" "modelRT/logger"
"modelRT/model" "modelRT/model"
"go.uber.org/zap"
) )
// ParseFunc defines func that parses the model data from postgres // ParseFunc defines func that parses the model data from postgres
var ParseFunc = func(parseConfig interface{}) { var ParseFunc = func(parseConfig interface{}) {
logger := logger.GetLoggerInstance()
modelParseConfig, ok := parseConfig.(config.ModelParseConfig) modelParseConfig, ok := parseConfig.(config.ModelParseConfig)
if !ok { if !ok {
logger.Error("conversion model parse config type failed") logger.Error(modelParseConfig.Ctx, "conversion model parse config type failed")
return return
} }
cancelCtx, cancel := context.WithTimeout(modelParseConfig.Context, 5*time.Second) cancelCtx, cancel := context.WithTimeout(modelParseConfig.Ctx, 5*time.Second)
defer cancel() defer cancel()
pgClient := database.GetPostgresDBClient() pgClient := database.GetPostgresDBClient()
@ -33,10 +29,10 @@ var ParseFunc = func(parseConfig interface{}) {
result := pgClient.WithContext(cancelCtx).Table(tableName).Where("component_id = ?", modelParseConfig.ComponentInfo.ID).Find(&unmarshalMap) result := pgClient.WithContext(cancelCtx).Table(tableName).Where("component_id = ?", modelParseConfig.ComponentInfo.ID).Find(&unmarshalMap)
if result.Error != nil { if result.Error != nil {
logger.Error("query component detail info failed", zap.Error(result.Error)) logger.Error(modelParseConfig.Ctx, "query component detail info failed", "error", result.Error)
return return
} else if result.RowsAffected == 0 { } else if result.RowsAffected == 0 {
logger.Error("query component detail info from table is empty", zap.String("table_name", tableName)) logger.Error(modelParseConfig.Ctx, "query component detail info from table is empty", "table_name", tableName)
return return
} }
@ -48,7 +44,7 @@ var ParseFunc = func(parseConfig interface{}) {
} }
diagram.StoreAnchorValue(modelParseConfig.ComponentInfo.ID, anchorName) diagram.StoreAnchorValue(modelParseConfig.ComponentInfo.ID, anchorName)
GetComponentChan(modelParseConfig.Context, modelParseConfig.ComponentInfo.ID) GetComponentChan(modelParseConfig.Ctx, modelParseConfig.ComponentInfo.ID)
uuid := modelParseConfig.ComponentInfo.GlobalUUID.String() uuid := modelParseConfig.ComponentInfo.GlobalUUID.String()
unmarshalMap["id"] = modelParseConfig.ComponentInfo.ID unmarshalMap["id"] = modelParseConfig.ComponentInfo.ID

View File

@ -8,7 +8,6 @@ import (
"modelRT/logger" "modelRT/logger"
"github.com/confluentinc/confluent-kafka-go/kafka" "github.com/confluentinc/confluent-kafka-go/kafka"
"go.uber.org/zap"
) )
// RealTimeDataComputer continuously processing real-time data from Kafka specified topics // RealTimeDataComputer continuously processing real-time data from Kafka specified topics
@ -17,9 +16,6 @@ func RealTimeDataComputer(ctx context.Context, consumerConfig kafka.ConfigMap, t
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
// get a logger
logger := logger.GetLoggerInstance()
// setup a channel to listen for interrupt signals // setup a channel to listen for interrupt signals
// TODO 将中断信号放到入参中 // TODO 将中断信号放到入参中
interrupt := make(chan struct{}, 1) interrupt := make(chan struct{}, 1)
@ -30,13 +26,13 @@ func RealTimeDataComputer(ctx context.Context, consumerConfig kafka.ConfigMap, t
// create a new consumer // create a new consumer
consumer, err := kafka.NewConsumer(&consumerConfig) consumer, err := kafka.NewConsumer(&consumerConfig)
if err != nil { if err != nil {
logger.Error("init kafka consume by config failed", zap.Any("config", consumerConfig), zap.Error(err)) logger.Error(ctx, "init kafka consume by config failed", "config", consumerConfig, "error", err)
} }
// subscribe to the topic // subscribe to the topic
err = consumer.SubscribeTopics(topics, nil) err = consumer.SubscribeTopics(topics, nil)
if err != nil { if err != nil {
logger.Error("subscribe to the topic failed", zap.Strings("topic", topics), zap.Error(err)) logger.Error(ctx, "subscribe to the topic failed", "topic", topics, "error", err)
} }
// start a goroutine to handle shutdown // start a goroutine to handle shutdown
@ -51,17 +47,17 @@ func RealTimeDataComputer(ctx context.Context, consumerConfig kafka.ConfigMap, t
msg, err := consumer.ReadMessage(timeoutDuration) msg, err := consumer.ReadMessage(timeoutDuration)
if err != nil { if err != nil {
if ctx.Err() == context.Canceled { if ctx.Err() == context.Canceled {
logger.Info("context canceled, stopping read loop") logger.Info(ctx, "context canceled, stopping read loop")
break break
} }
logger.Error("consumer read message failed", zap.Error(err)) logger.Error(ctx, "consumer read message failed", "error", err)
continue continue
} }
// TODO 使用 ants.pool处理 kafka 的订阅数据 // TODO 使用 ants.pool处理 kafka 的订阅数据
_, err = consumer.CommitMessage(msg) _, err = consumer.CommitMessage(msg)
if err != nil { if err != nil {
logger.Error("manual submission information failed", zap.Any("message", msg), zap.Error(err)) logger.Error(ctx, "manual submission information failed", "message", msg, "error", err)
} }
} }
} }

View File

@ -10,8 +10,6 @@ import (
"modelRT/logger" "modelRT/logger"
"modelRT/network" "modelRT/network"
"modelRT/pool" "modelRT/pool"
"go.uber.org/zap"
) )
// RealTimeDataChan define channel of real time data receive // RealTimeDataChan define channel of real time data receive
@ -23,8 +21,6 @@ func init() {
// ReceiveChan define func of real time data receive and process // ReceiveChan define func of real time data receive and process
func ReceiveChan(ctx context.Context) { func ReceiveChan(ctx context.Context) {
logger := logger.GetLoggerInstance()
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -34,13 +30,13 @@ func ReceiveChan(ctx context.Context) {
componentID := realTimeData.PayLoad.ComponentID componentID := realTimeData.PayLoad.ComponentID
component, err := diagram.GetComponentMap(componentID) component, err := diagram.GetComponentMap(componentID)
if err != nil { if err != nil {
logger.Error("query component info from diagram map by componet id failed", zap.Int64("component_id", componentID), zap.Error(err)) logger.Error(ctx, "query component info from diagram map by componet id failed", "component_id", componentID, "error", err)
continue continue
} }
componentType := component["component_type"].(int) componentType := component["component_type"].(int)
if componentType != constant.DemoType { if componentType != constant.DemoType {
logger.Error("can not process real time data of component type not equal DemoType", zap.Int64("component_id", componentID)) logger.Error(ctx, "can not process real time data of component type not equal DemoType", "component_id", componentID)
continue continue
} }

View File

@ -11,16 +11,11 @@ import (
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"go.uber.org/zap"
) )
var ( var rdb *redis.Client
log *zap.Logger
rdb *redis.Client
)
func init() { func init() {
log = zap.Must(zap.NewDevelopment())
rdb = redis.NewClient(&redis.Options{ rdb = redis.NewClient(&redis.Options{
Network: "tcp", Network: "tcp",
Addr: "192.168.2.104:30001", Addr: "192.168.2.104:30001",
@ -46,7 +41,6 @@ func TestRWLockRLockAndUnRLock(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a", Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a",
}) })
rwLocker.Logger = log
duration := 10 * time.Second duration := 10 * time.Second
// 第一次加读锁 // 第一次加读锁
@ -77,7 +71,6 @@ func TestRWLockReentrantRLock(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a", Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a",
}) })
rwLocker.Logger = log
duration := 10 * time.Second duration := 10 * time.Second
// 第一次加读锁 // 第一次加读锁
@ -125,7 +118,6 @@ func TestRWLockRefreshRLock(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a", Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a",
}) })
rwLocker.Logger = log
duration := 10 * time.Second duration := 10 * time.Second
// 第一次加读锁 // 第一次加读锁
@ -168,7 +160,6 @@ func TestRWLock2ClientRLock(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a", Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a",
}) })
rwLocker1.Logger = log
rwLocker2 := dl.GetRWLocker(rdb, &dl.RedissionLockConfig{ rwLocker2 := dl.GetRWLocker(rdb, &dl.RedissionLockConfig{
LockLeaseTime: 120, LockLeaseTime: 120,
@ -176,7 +167,6 @@ func TestRWLock2ClientRLock(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc5577", Token: "fd348a84-e07c-4a61-8c19-f753e6bc5577",
}) })
rwLocker2.Logger = log
duration := 10 * time.Second duration := 10 * time.Second
// locker1加读锁 // locker1加读锁
@ -227,7 +217,6 @@ func TestRWLock2CWith2DifTimeRLock(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a", Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a",
}) })
rwLocker1.Logger = log
rwLocker2 := dl.GetRWLocker(rdb, &dl.RedissionLockConfig{ rwLocker2 := dl.GetRWLocker(rdb, &dl.RedissionLockConfig{
LockLeaseTime: 30, LockLeaseTime: 30,
@ -235,7 +224,6 @@ func TestRWLock2CWith2DifTimeRLock(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc5577", Token: "fd348a84-e07c-4a61-8c19-f753e6bc5577",
}) })
rwLocker2.Logger = log
duration := 10 * time.Second duration := 10 * time.Second
// locker1加读锁 // locker1加读锁
@ -300,7 +288,6 @@ func TestRWLock2CWithTimeTransformRLock(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a", Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a",
}) })
rwLocker1.Logger = log
rwLocker2 := dl.GetRWLocker(rdb, &dl.RedissionLockConfig{ rwLocker2 := dl.GetRWLocker(rdb, &dl.RedissionLockConfig{
LockLeaseTime: 120, LockLeaseTime: 120,
@ -308,7 +295,6 @@ func TestRWLock2CWithTimeTransformRLock(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc5577", Token: "fd348a84-e07c-4a61-8c19-f753e6bc5577",
}) })
rwLocker2.Logger = log
duration := 10 * time.Second duration := 10 * time.Second
// locker1加读锁 // locker1加读锁
@ -366,7 +352,6 @@ func TestRWLockWLockAndUnWLock(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a", Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a",
}) })
rwLocker.Logger = log
duration := 10 * time.Second duration := 10 * time.Second
// 第一次加读锁 // 第一次加读锁
@ -397,7 +382,6 @@ func TestRWLockReentrantWLock(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a", Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a",
}) })
rwLocker.Logger = log
duration := 10 * time.Second duration := 10 * time.Second
// 第一次加写锁 // 第一次加写锁
@ -445,7 +429,6 @@ func TestRWLock2CWithRLockAndWLockFailed(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a", Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a",
}) })
rwLocker1.Logger = log
rwLocker2 := dl.GetRWLocker(rdb, &dl.RedissionLockConfig{ rwLocker2 := dl.GetRWLocker(rdb, &dl.RedissionLockConfig{
LockLeaseTime: 30, LockLeaseTime: 30,
@ -453,7 +436,6 @@ func TestRWLock2CWithRLockAndWLockFailed(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc5577", Token: "fd348a84-e07c-4a61-8c19-f753e6bc5577",
}) })
rwLocker2.Logger = log
duration := 10 * time.Second duration := 10 * time.Second
// locker1加读锁 // locker1加读锁
@ -485,7 +467,6 @@ func TestRWLock2CWithRLockAndWLockSucceed(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a", Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a",
}) })
rwLocker1.Logger = log
rwLocker2 := dl.GetRWLocker(rdb, &dl.RedissionLockConfig{ rwLocker2 := dl.GetRWLocker(rdb, &dl.RedissionLockConfig{
LockLeaseTime: 120, LockLeaseTime: 120,
@ -493,7 +474,7 @@ func TestRWLock2CWithRLockAndWLockSucceed(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc5577", Token: "fd348a84-e07c-4a61-8c19-f753e6bc5577",
}) })
rwLocker2.Logger = log
duration := 10 * time.Second duration := 10 * time.Second
// locker1加读锁 // locker1加读锁
err := rwLocker1.RLock(ctx, duration) err := rwLocker1.RLock(ctx, duration)
@ -538,7 +519,6 @@ func TestRWLock2CWithWLockAndRLock(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a", Token: "fd348a84-e07c-4a61-8c19-f753e6bc556a",
}) })
rwLocker1.Logger = log
rwLocker2 := dl.GetRWLocker(rdb, &dl.RedissionLockConfig{ rwLocker2 := dl.GetRWLocker(rdb, &dl.RedissionLockConfig{
LockLeaseTime: 30, LockLeaseTime: 30,
@ -546,7 +526,6 @@ func TestRWLock2CWithWLockAndRLock(t *testing.T) {
Key: "component", Key: "component",
Token: "fd348a84-e07c-4a61-8c19-f753e6bc5577", Token: "fd348a84-e07c-4a61-8c19-f753e6bc5577",
}) })
rwLocker2.Logger = log
duration := 10 * time.Second duration := 10 * time.Second
// locker1加写锁 // locker1加写锁