上传网络优化和任务系统修复

This commit is contained in:
liwei 2023-07-10 16:57:16 +08:00
parent 574efd1a81
commit bf2f0eaef8
8 changed files with 86 additions and 47 deletions

View File

@ -3,6 +3,7 @@ package rpcx
import ( import (
"context" "context"
"strings" "strings"
"time"
"github.com/smallnest/rpcx/client" "github.com/smallnest/rpcx/client"
) )
@ -74,6 +75,14 @@ func (this *RPCX) UnregisterAll() (err error) {
// 同步调用 // 同步调用
func (this *RPCX) Call(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) { func (this *RPCX) Call(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) {
var (
cancel context.CancelFunc
)
if this.options.OutTime > 0 {
ctx, cancel = context.WithTimeout(ctx, time.Duration(this.options.OutTime)*time.Second)
defer cancel()
}
//先排查下 服务端是否存在连接对象 不存在 在使用客户端对象连接 //先排查下 服务端是否存在连接对象 不存在 在使用客户端对象连接
err = this.service.Call(ctx, servicePath, serviceMethod, args, reply) err = this.service.Call(ctx, servicePath, serviceMethod, args, reply)
if err != nil && strings.Contains(err.Error(), "on found") { if err != nil && strings.Contains(err.Error(), "on found") {
@ -84,6 +93,14 @@ func (this *RPCX) Call(ctx context.Context, servicePath string, serviceMethod st
// 广播调用 // 广播调用
func (this *RPCX) Broadcast(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) { func (this *RPCX) Broadcast(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) {
var (
cancel context.CancelFunc
)
if this.options.OutTime > 0 {
ctx, cancel = context.WithTimeout(ctx, time.Duration(this.options.OutTime)*time.Second)
defer cancel()
}
err = this.service.Broadcast(ctx, servicePath, serviceMethod, args, reply) err = this.service.Broadcast(ctx, servicePath, serviceMethod, args, reply)
if err != nil && strings.Contains(err.Error(), "on found") { if err != nil && strings.Contains(err.Error(), "on found") {
return this.client.Broadcast(ctx, servicePath, serviceMethod, args, reply) return this.client.Broadcast(ctx, servicePath, serviceMethod, args, reply)
@ -102,6 +119,14 @@ func (this *RPCX) Go(ctx context.Context, servicePath string, serviceMethod stri
// 跨服同步调用 // 跨服同步调用
func (this *RPCX) AcrossClusterCall(ctx context.Context, clusterTag string, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) { func (this *RPCX) AcrossClusterCall(ctx context.Context, clusterTag string, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) {
var (
cancel context.CancelFunc
)
if this.options.OutTime > 0 {
ctx, cancel = context.WithTimeout(ctx, time.Duration(this.options.OutTime)*time.Second)
defer cancel()
}
err = this.service.AcrossClusterCall(ctx, clusterTag, servicePath, serviceMethod, args, reply) err = this.service.AcrossClusterCall(ctx, clusterTag, servicePath, serviceMethod, args, reply)
if err != nil && strings.Contains(err.Error(), "on found") { if err != nil && strings.Contains(err.Error(), "on found") {
return this.client.AcrossClusterCall(ctx, clusterTag, servicePath, serviceMethod, args, reply) return this.client.AcrossClusterCall(ctx, clusterTag, servicePath, serviceMethod, args, reply)
@ -111,6 +136,14 @@ func (this *RPCX) AcrossClusterCall(ctx context.Context, clusterTag string, serv
// 跨集群 广播 // 跨集群 广播
func (this *RPCX) AcrossClusterBroadcast(ctx context.Context, clusterTag string, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) { func (this *RPCX) AcrossClusterBroadcast(ctx context.Context, clusterTag string, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) {
var (
cancel context.CancelFunc
)
if this.options.OutTime > 0 {
ctx, cancel = context.WithTimeout(ctx, time.Duration(this.options.OutTime)*time.Second)
defer cancel()
}
err = this.service.AcrossClusterBroadcast(ctx, clusterTag, servicePath, serviceMethod, args, reply) err = this.service.AcrossClusterBroadcast(ctx, clusterTag, servicePath, serviceMethod, args, reply)
if err != nil && strings.Contains(err.Error(), "on found") { if err != nil && strings.Contains(err.Error(), "on found") {
return this.client.AcrossClusterBroadcast(ctx, clusterTag, servicePath, serviceMethod, args, reply) return this.client.AcrossClusterBroadcast(ctx, clusterTag, servicePath, serviceMethod, args, reply)
@ -129,6 +162,14 @@ func (this *RPCX) AcrossClusterGo(ctx context.Context, clusterTag string, servic
// 全集群广播 // 全集群广播
func (this *RPCX) ClusterBroadcast(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) { func (this *RPCX) ClusterBroadcast(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) {
var (
cancel context.CancelFunc
)
if this.options.OutTime > 0 {
ctx, cancel = context.WithTimeout(ctx, time.Duration(this.options.OutTime)*time.Second)
defer cancel()
}
err = this.service.ClusterBroadcast(ctx, servicePath, serviceMethod, args, reply) err = this.service.ClusterBroadcast(ctx, servicePath, serviceMethod, args, reply)
if err != nil && strings.Contains(err.Error(), "on found") { if err != nil && strings.Contains(err.Error(), "on found") {
return this.client.ClusterBroadcast(ctx, servicePath, serviceMethod, args, reply) return this.client.ClusterBroadcast(ctx, servicePath, serviceMethod, args, reply)

View File

@ -114,7 +114,7 @@ func (this *Service) Call(ctx context.Context, servicePath string, serviceMethod
) )
seq := new(uint64) seq := new(uint64)
ctx = context.WithValue(ctx, seqKey{}, seq) ctx = context.WithValue(ctx, seqKey{}, seq)
if conn, done, err = this.call(ctx, this.options.ServiceTag, servicePath, serviceMethod, args, reply, make(chan *client.Call, 1)); err != nil { if conn, done, err = this.call(&ctx, this.options.ServiceTag, servicePath, serviceMethod, args, reply, make(chan *client.Call, 1)); err != nil {
return return
} }
select { select {
@ -150,7 +150,7 @@ func (this *Service) Broadcast(ctx context.Context, servicePath string, serviceM
// 异步调用 远程服务 // 异步调用 远程服务
func (this *Service) Go(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}, done chan *client.Call) (_call *client.Call, err error) { func (this *Service) Go(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}, done chan *client.Call) (_call *client.Call, err error) {
_, _call, err = this.call(ctx, this.options.ServiceTag, servicePath, serviceMethod, args, reply, done) _, _call, err = this.call(&ctx, this.options.ServiceTag, servicePath, serviceMethod, args, reply, done)
return return
} }
@ -162,7 +162,7 @@ func (this *Service) AcrossClusterCall(ctx context.Context, clusterTag string, s
) )
seq := new(uint64) seq := new(uint64)
ctx = context.WithValue(ctx, seqKey{}, seq) ctx = context.WithValue(ctx, seqKey{}, seq)
if conn, done, err = this.call(ctx, clusterTag, servicePath, serviceMethod, args, reply, make(chan *client.Call, 1)); err != nil { if conn, done, err = this.call(&ctx, clusterTag, servicePath, serviceMethod, args, reply, make(chan *client.Call, 1)); err != nil {
return return
} }
select { select {
@ -198,7 +198,7 @@ func (this *Service) AcrossClusterBroadcast(ctx context.Context, clusterTag stri
// 跨服 异步调用 远程服务 // 跨服 异步调用 远程服务
func (this *Service) AcrossClusterGo(ctx context.Context, clusterTag, servicePath string, serviceMethod string, args interface{}, reply interface{}, done chan *client.Call) (_call *client.Call, err error) { func (this *Service) AcrossClusterGo(ctx context.Context, clusterTag, servicePath string, serviceMethod string, args interface{}, reply interface{}, done chan *client.Call) (_call *client.Call, err error) {
_, _call, err = this.call(ctx, clusterTag, servicePath, serviceMethod, args, reply, done) _, _call, err = this.call(&ctx, clusterTag, servicePath, serviceMethod, args, reply, done)
return return
} }
@ -298,7 +298,7 @@ func (this *Service) RpcxShakeHands(ctx context.Context, args *ServiceNode, repl
} }
// 执行远程调用 // 执行远程调用
func (this *Service) call(ctx context.Context, clusterTag string, servicePath string, serviceMethod string, args interface{}, reply interface{}, done chan *client.Call) (conn net.Conn, _call *client.Call, err error) { func (this *Service) call(ctx *context.Context, clusterTag string, servicePath string, serviceMethod string, args interface{}, reply interface{}, done chan *client.Call) (conn net.Conn, _call *client.Call, err error) {
var ( var (
spath []string spath []string
clientaddr string clientaddr string
@ -317,7 +317,7 @@ func (this *Service) call(ctx context.Context, clusterTag string, servicePath st
ServiceMetaKey: this.metadata, ServiceMetaKey: this.metadata,
} }
spath = strings.Split(servicePath, "/") spath = strings.Split(servicePath, "/")
ctx = context.WithValue(ctx, share.ReqMetaDataKey, map[string]string{ *ctx = context.WithValue(*ctx, share.ReqMetaDataKey, map[string]string{
CallRoutRulesKey: servicePath, CallRoutRulesKey: servicePath,
ServiceAddrKey: "tcp@" + this.options.ServiceAddr, ServiceAddrKey: "tcp@" + this.options.ServiceAddr,
ServiceMetaKey: this.metadata, ServiceMetaKey: this.metadata,
@ -330,7 +330,7 @@ func (this *Service) call(ctx context.Context, clusterTag string, servicePath st
return return
} }
if clientaddr = selector.Select(ctx, spath[0], serviceMethod, args); clientaddr == "" { if clientaddr = selector.Select(*ctx, spath[0], serviceMethod, args); clientaddr == "" {
err = fmt.Errorf("on found servicePath:%s", servicePath) err = fmt.Errorf("on found servicePath:%s", servicePath)
return return
} }
@ -354,7 +354,7 @@ func (this *Service) call(ctx context.Context, clusterTag string, servicePath st
} }
} }
_call.Done = done _call.Done = done
this.send(ctx, conn, spath[0], serviceMethod, metadata, _call) this.send(*ctx, conn, spath[0], serviceMethod, metadata, _call)
return return
} }
@ -412,8 +412,9 @@ func (this *Service) broadcast(ctx context.Context, clusterTag string, servicePa
_call.Args = args _call.Args = args
_call.Reply = reply _call.Reply = reply
_call.Done = make(chan *client.Call, 10) _call.Done = make(chan *client.Call, 10)
seq := new(uint64)
ctx = context.WithValue(ctx, seqKey{}, seq)
this.send(ctx, conn, spath[0], serviceMethod, metadata, _call) this.send(ctx, conn, spath[0], serviceMethod, metadata, _call)
seq, _ := ctx.Value(seqKey{}).(*uint64)
select { select {
case <-ctx.Done(): // cancel by context case <-ctx.Done(): // cancel by context
this.mutex.Lock() this.mutex.Lock()

View File

@ -355,11 +355,9 @@ func (this *Agent) messageDistribution(msg *pb.UserMessage) (err error) {
} }
stime := time.Now() stime := time.Now()
// this.gateway.Debugf("----------3 agent:%s uId:%s MainType:%s SubType:%s ", this.sessionId, this.uId, msg.MainType, msg.SubType) // this.gateway.Debugf("----------3 agent:%s uId:%s MainType:%s SubType:%s ", this.sessionId, this.uId, msg.MainType, msg.SubType)
// ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
ctx := context.Background()
if len(serviceTag) == 0 { if len(serviceTag) == 0 {
// this.gateway.Debugf("----------4 agent:%s uId:%s MainType:%s SubType:%s ", this.sessionId, this.uId, msg.MainType, msg.SubType) // this.gateway.Debugf("----------4 agent:%s uId:%s MainType:%s SubType:%s ", this.sessionId, this.uId, msg.MainType, msg.SubType)
if err = this.gateway.Service().RpcCall(ctx, servicePath, string(comm.Rpc_GatewayRoute), req, reply); err != nil { if err = this.gateway.Service().RpcCall(context.Background(), servicePath, string(comm.Rpc_GatewayRoute), req, reply); err != nil {
this.gateway.Error("[UserResponse]", this.gateway.Error("[UserResponse]",
log.Field{Key: "uid", Value: this.uId}, log.Field{Key: "uid", Value: this.uId},
log.Field{Key: "serviceTag", Value: serviceTag}, log.Field{Key: "serviceTag", Value: serviceTag},
@ -374,7 +372,7 @@ func (this *Agent) messageDistribution(msg *pb.UserMessage) (err error) {
if msg.ServicePath != "" { //客户端是否制定目标服务器 /wroker/woker0 if msg.ServicePath != "" { //客户端是否制定目标服务器 /wroker/woker0
servicePath = msg.ServicePath servicePath = msg.ServicePath
} }
if err = this.gateway.Service().AcrossClusterRpcCall(ctx, serviceTag, servicePath, string(comm.Rpc_GatewayRoute), req, reply); err != nil { if err = this.gateway.Service().AcrossClusterRpcCall(context.Background(), serviceTag, servicePath, string(comm.Rpc_GatewayRoute), req, reply); err != nil {
this.gateway.Error("[UserResponse]", this.gateway.Error("[UserResponse]",
log.Field{Key: "uid", Value: this.uId}, log.Field{Key: "uid", Value: this.uId},
log.Field{Key: "serviceTag", Value: serviceTag}, log.Field{Key: "serviceTag", Value: serviceTag},

View File

@ -96,9 +96,8 @@ func (this *Parkour) AddMounts(session comm.IUserSession, mounts map[string]int3
// 匹配 // 匹配
func (this *Parkour) match(team *pb.DBParkour) (err error) { func (this *Parkour) match(team *pb.DBParkour) (err error) {
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
err = this.service.RpcCall( err = this.service.RpcCall(
ctx, context.Background(),
comm.Service_Mainte, comm.Service_Mainte,
string(comm.RPC_ParkourJoinMatch), string(comm.RPC_ParkourJoinMatch),
&pb.RPCParkourJoinMatchReq{Captainid: team.Captainid, Member: team.Member}, &pb.RPCParkourJoinMatchReq{Captainid: team.Captainid, Member: team.Member},

View File

@ -324,8 +324,7 @@ func (this *ParkourComp) match() {
} }
} }
ctx, _ := context.WithTimeout(context.Background(), time.Second*2) if err = this.service.RpcCall(context.Background(),
if err = this.service.RpcCall(ctx,
comm.Service_Worker, comm.Service_Worker,
string(comm.RPC_ParkourMatchSucc), string(comm.RPC_ParkourMatchSucc),
&pb.RPCParkourMatchSuccReq{Red: reduser, Bule: buleuser}, &pb.RPCParkourMatchSuccReq{Red: reduser, Bule: buleuser},

View File

@ -10,7 +10,6 @@ import (
"go_dreamfactory/pb" "go_dreamfactory/pb"
"go_dreamfactory/utils" "go_dreamfactory/utils"
"net/http" "net/http"
"time"
) )
type PayDeliveryResults struct { type PayDeliveryResults struct {
@ -63,9 +62,8 @@ func (this *Api_Comp) PayDelivery(c *engine.Context) {
log.Errorf("签名错误 sign:%s _sign:%s", sign, _sign) log.Errorf("签名错误 sign:%s _sign:%s", sign, _sign)
return return
} }
ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
if err := this.module.service.RpcCall( if err := this.module.service.RpcCall(
ctx, context.TODO(),
comm.Service_Worker, comm.Service_Worker,
string(comm.Rpc_ModulePayDelivery), string(comm.Rpc_ModulePayDelivery),
payreq, payreq,

View File

@ -32,6 +32,9 @@ func (this *apiComp) Info(session comm.IUserSession, req *pb.WTaskInfoReq) (errd
if progress, errdata = this.module.fishtask(session, wtask, false); errdata != nil { if progress, errdata = this.module.fishtask(session, wtask, false); errdata != nil {
return return
} }
if progress, errdata = this.module.pushtaskprogress(session, wtask, false); errdata != nil {
return
}
session.SendMsg(string(this.module.GetType()), "info", &pb.WTaskInfoResp{Info: wtask, Accepts: progress}) session.SendMsg(string(this.module.GetType()), "info", &pb.WTaskInfoResp{Info: wtask, Accepts: progress})
if err = this.module.modelwtask.updateUserWTasks(session.GetUserId(), wtask); err != nil { if err = this.module.modelwtask.updateUserWTasks(session.GetUserId(), wtask); err != nil {
errdata = &pb.ErrorData{ errdata = &pb.ErrorData{

View File

@ -467,7 +467,7 @@ func (this *WTask) inquireActivations(session comm.IUserSession, wtask *pb.DBWTa
//有新任务接取 //有新任务接取
if ispush && changeActiva { if ispush && changeActiva {
session.SendMsg(string(this.GetType()), "activations", &pb.WTaskActivationsChangePush{Activations: wtask.Activations}) session.SendMsg(string(this.GetType()), "activationschange", &pb.WTaskActivationsChangePush{Activations: wtask.Activations})
} }
if changeAccept { if changeAccept {