feat: analysis user auth and ws
This commit is contained in:
@@ -21,7 +21,7 @@ type rpcClientCacheEntry struct {
|
||||
expireAt time.Time
|
||||
}
|
||||
|
||||
const rpcClientCacheTTL = 30 * time.Second //缓存过期时间
|
||||
const rpcClientCacheTTL = 30 * time.Second
|
||||
|
||||
var (
|
||||
rpcCacheLock sync.Mutex
|
||||
@@ -35,15 +35,13 @@ func GetRpcClient(serviceName string) (zrpc.Client, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cacheKey := serviceName
|
||||
|
||||
rpcCacheLock.Lock()
|
||||
entry, ok := rpcCache[cacheKey]
|
||||
entry, ok := rpcCache[serviceName]
|
||||
if ok && time.Now().Before(entry.expireAt) {
|
||||
rpcCacheLock.Unlock()
|
||||
return entry.cli, nil
|
||||
}
|
||||
delete(rpcCache, cacheKey)
|
||||
delete(rpcCache, serviceName)
|
||||
rpcCacheLock.Unlock()
|
||||
|
||||
inst, err := discovery.Pick(serviceName)
|
||||
@@ -54,14 +52,13 @@ func GetRpcClient(serviceName string) (zrpc.Client, error) {
|
||||
}
|
||||
|
||||
target := net.JoinHostPort(inst.IP, strconv.FormatUint(inst.Port, utils.NumberTen))
|
||||
|
||||
cli := zrpc.MustNewClient(zrpc.RpcClientConf{
|
||||
Target: target,
|
||||
Timeout: utils.RpcTimeOut, //rpc调用超时5s
|
||||
Timeout: utils.RpcTimeOut,
|
||||
})
|
||||
|
||||
rpcCacheLock.Lock()
|
||||
rpcCache[cacheKey] = &rpcClientCacheEntry{
|
||||
rpcCache[serviceName] = &rpcClientCacheEntry{
|
||||
cli: cli,
|
||||
target: target,
|
||||
expireAt: time.Now().Add(rpcClientCacheTTL),
|
||||
@@ -75,23 +72,24 @@ type ServiceContext struct {
|
||||
Config config.Config
|
||||
DB *gorm.DB
|
||||
Prefix string
|
||||
UserSvcName string
|
||||
ChoreSvcName string
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
|
||||
//启动仅读取配置,不建立rpc连接 多个服务就多个
|
||||
ChoreSvc := utils.GetConfigString("services.chore")
|
||||
|
||||
if ChoreSvc == utils.StringEmpty {
|
||||
userSvc := utils.GetConfigString("services.user")
|
||||
if userSvc == utils.StringEmpty {
|
||||
logx.Error("config services.user empty")
|
||||
}
|
||||
choreSvc := utils.GetConfigString("services.chore")
|
||||
if choreSvc == utils.StringEmpty {
|
||||
logx.Error("config services.chore empty")
|
||||
}
|
||||
|
||||
svcCtx := &ServiceContext{
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
DB: db,
|
||||
Prefix: utils.GetConfigString("mysql.prefix"),
|
||||
ChoreSvcName: ChoreSvc,
|
||||
UserSvcName: userSvc,
|
||||
ChoreSvcName: choreSvc,
|
||||
}
|
||||
|
||||
return svcCtx
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user