46d3f672bf
CI / changes (push) Successful in 1m30s
CI / ad (push) Successful in 31s
CI / admin (push) Successful in 24s
CI / bff (push) Successful in 31s
CI / chore (push) Successful in 27s
CI / equipment (push) Successful in 21s
CI / express (push) Successful in 23s
CI / factory (push) Successful in 21s
CI / message (push) Successful in 21s
CI / order (push) Successful in 22s
CI / payment (push) Successful in 20s
CI / percutaneous (push) Successful in 20s
CI / product (push) Successful in 20s
CI / record (push) Successful in 19s
CI / sale (push) Successful in 19s
CI / support (push) Successful in 20s
CI / task (push) Successful in 19s
CI / user (push) Successful in 21s
CI / wecom (push) Successful in 22s
102 lines
2.3 KiB
Go
102 lines
2.3 KiB
Go
package svc
|
|
|
|
import (
|
|
"fmt"
|
|
"lone-services/pkg/discovery"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/services/analysis/internal/config"
|
|
"net"
|
|
"strconv"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"github.com/zeromicro/go-zero/zrpc"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type rpcClientCacheEntry struct {
|
|
cli zrpc.Client
|
|
target string
|
|
expireAt time.Time
|
|
}
|
|
|
|
const rpcClientCacheTTL = 30 * time.Second
|
|
|
|
var (
|
|
rpcCacheLock sync.Mutex
|
|
rpcCache = make(map[string]*rpcClientCacheEntry)
|
|
)
|
|
|
|
func GetRpcClient(serviceName string) (zrpc.Client, error) {
|
|
if serviceName == utils.StringEmpty {
|
|
err := fmt.Errorf("rpc serviceName is empty")
|
|
logx.Error(err)
|
|
return nil, err
|
|
}
|
|
|
|
rpcCacheLock.Lock()
|
|
entry, ok := rpcCache[serviceName]
|
|
if ok && time.Now().Before(entry.expireAt) {
|
|
rpcCacheLock.Unlock()
|
|
return entry.cli, nil
|
|
}
|
|
delete(rpcCache, serviceName)
|
|
rpcCacheLock.Unlock()
|
|
|
|
inst, err := discovery.Pick(serviceName)
|
|
if err != nil {
|
|
err = fmt.Errorf("discovery pick %s failed: %w", serviceName, err)
|
|
logx.Error(err)
|
|
return nil, err
|
|
}
|
|
|
|
target := net.JoinHostPort(inst.IP, strconv.FormatUint(inst.Port, utils.NumberTen))
|
|
cli := zrpc.MustNewClient(zrpc.RpcClientConf{
|
|
Target: target,
|
|
Timeout: utils.RpcTimeOut,
|
|
})
|
|
|
|
rpcCacheLock.Lock()
|
|
rpcCache[serviceName] = &rpcClientCacheEntry{
|
|
cli: cli,
|
|
target: target,
|
|
expireAt: time.Now().Add(rpcClientCacheTTL),
|
|
}
|
|
rpcCacheLock.Unlock()
|
|
|
|
return cli, nil
|
|
}
|
|
|
|
type ServiceContext struct {
|
|
Config config.Config
|
|
DB *gorm.DB
|
|
Prefix string
|
|
UserSvcName string
|
|
ChoreSvcName string
|
|
EquipmentSvcName string
|
|
}
|
|
|
|
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
|
|
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")
|
|
}
|
|
equipmentSvc := utils.GetConfigString("services.equipment")
|
|
if equipmentSvc == utils.StringEmpty {
|
|
logx.Error("config services.equipment empty")
|
|
}
|
|
return &ServiceContext{
|
|
Config: c,
|
|
DB: db,
|
|
Prefix: utils.GetConfigString("mysql.prefix"),
|
|
UserSvcName: userSvc,
|
|
ChoreSvcName: choreSvc,
|
|
EquipmentSvcName: equipmentSvc,
|
|
}
|
|
}
|