优化聊天消息广播异常问题
This commit is contained in:
parent
dc30119e55
commit
5eb2783750
@ -228,27 +228,31 @@ func (this *Client) ClusterBroadcast(ctx context.Context, servicePath string, se
|
||||
}
|
||||
this.clusterMu.RUnlock()
|
||||
l := len(clients)
|
||||
done := make(chan error, l)
|
||||
for _, v := range clients {
|
||||
go func(c client.XClient) {
|
||||
done <- c.Broadcast(ctx, serviceMethod, args, reply)
|
||||
}(v)
|
||||
}
|
||||
timeout := time.NewTimer(time.Minute)
|
||||
check:
|
||||
for {
|
||||
select {
|
||||
case err = <-done:
|
||||
l--
|
||||
if l == 0 || err != nil { // all returns or some one returns an error
|
||||
if l > 0 {
|
||||
done := make(chan error, l)
|
||||
for _, v := range clients {
|
||||
go func(c client.XClient) {
|
||||
done <- c.Broadcast(ctx, serviceMethod, args, reply)
|
||||
}(v)
|
||||
}
|
||||
timeout := time.NewTimer(time.Minute)
|
||||
check:
|
||||
for {
|
||||
select {
|
||||
case err = <-done:
|
||||
l--
|
||||
if l == 0 || err != nil { // all returns or some one returns an error
|
||||
break check
|
||||
}
|
||||
case <-timeout.C:
|
||||
err = errors.New(("timeout"))
|
||||
break check
|
||||
}
|
||||
case <-timeout.C:
|
||||
err = errors.New(("timeout"))
|
||||
break check
|
||||
}
|
||||
timeout.Stop()
|
||||
} else {
|
||||
err = errors.New("on found any service")
|
||||
}
|
||||
timeout.Stop()
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -127,8 +127,7 @@ 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) {
|
||||
|
||||
err = this.client.ClusterBroadcast(ctx, servicePath, serviceMethod, args, reply)
|
||||
err = this.service.ClusterBroadcast(ctx, servicePath, serviceMethod, args, reply)
|
||||
if err != nil && strings.Contains(err.Error(), "on found") {
|
||||
return this.client.ClusterBroadcast(ctx, servicePath, serviceMethod, args, reply)
|
||||
}
|
||||
|
@ -496,66 +496,71 @@ func (this *Service) clusterbroadcast(ctx context.Context, servicePath string, s
|
||||
}
|
||||
|
||||
l := len(addrs)
|
||||
done := make(chan error, l)
|
||||
for v, _ := range addrs {
|
||||
go func(addr string) {
|
||||
this.clientmutex.RLock()
|
||||
conn, ok := this.clients[addr]
|
||||
if !ok {
|
||||
done <- fmt.Errorf("on found clientaddr:%s", addr)
|
||||
if l > 0 {
|
||||
done := make(chan error, l)
|
||||
for v, _ := range addrs {
|
||||
go func(addr string) {
|
||||
this.clientmutex.RLock()
|
||||
conn, ok := this.clients[addr]
|
||||
if !ok {
|
||||
done <- fmt.Errorf("on found clientaddr:%s", addr)
|
||||
this.clientmutex.RUnlock()
|
||||
return
|
||||
}
|
||||
this.clientmutex.RUnlock()
|
||||
return
|
||||
}
|
||||
this.clientmutex.RUnlock()
|
||||
_call := new(client.Call)
|
||||
_call.ServicePath = servicePath
|
||||
_call.ServiceMethod = serviceMethod
|
||||
_call.Args = args
|
||||
_call.Reply = reply
|
||||
_call.Done = make(chan *client.Call, 10)
|
||||
this.send(ctx, conn, spath[0], serviceMethod, metadata, _call)
|
||||
seq, _ := ctx.Value(seqKey{}).(*uint64)
|
||||
select {
|
||||
case <-ctx.Done(): // cancel by context
|
||||
this.mutex.Lock()
|
||||
call := this.pending[*seq]
|
||||
delete(this.pending, *seq)
|
||||
this.mutex.Unlock()
|
||||
if call != nil {
|
||||
call.Error = ctx.Err()
|
||||
call.Done <- call
|
||||
}
|
||||
done <- ctx.Err()
|
||||
case call := <-_call.Done:
|
||||
err = call.Error
|
||||
meta := ctx.Value(share.ResMetaDataKey)
|
||||
if meta != nil && len(call.ResMetadata) > 0 {
|
||||
resMeta := meta.(map[string]string)
|
||||
for k, v := range call.ResMetadata {
|
||||
resMeta[k] = v
|
||||
_call := new(client.Call)
|
||||
_call.ServicePath = servicePath
|
||||
_call.ServiceMethod = serviceMethod
|
||||
_call.Args = args
|
||||
_call.Reply = reply
|
||||
_call.Done = make(chan *client.Call, 10)
|
||||
this.send(ctx, conn, spath[0], serviceMethod, metadata, _call)
|
||||
seq, _ := ctx.Value(seqKey{}).(*uint64)
|
||||
select {
|
||||
case <-ctx.Done(): // cancel by context
|
||||
this.mutex.Lock()
|
||||
call := this.pending[*seq]
|
||||
delete(this.pending, *seq)
|
||||
this.mutex.Unlock()
|
||||
if call != nil {
|
||||
call.Error = ctx.Err()
|
||||
call.Done <- call
|
||||
}
|
||||
resMeta[share.ServerAddress] = conn.RemoteAddr().String()
|
||||
done <- ctx.Err()
|
||||
case call := <-_call.Done:
|
||||
err = call.Error
|
||||
meta := ctx.Value(share.ResMetaDataKey)
|
||||
if meta != nil && len(call.ResMetadata) > 0 {
|
||||
resMeta := meta.(map[string]string)
|
||||
for k, v := range call.ResMetadata {
|
||||
resMeta[k] = v
|
||||
}
|
||||
resMeta[share.ServerAddress] = conn.RemoteAddr().String()
|
||||
}
|
||||
done <- nil
|
||||
}
|
||||
done <- nil
|
||||
}
|
||||
}(v)
|
||||
}
|
||||
timeout := time.NewTimer(time.Minute)
|
||||
check:
|
||||
for {
|
||||
select {
|
||||
case err = <-done:
|
||||
l--
|
||||
if l == 0 || err != nil { // all returns or some one returns an error
|
||||
}(v)
|
||||
}
|
||||
timeout := time.NewTimer(time.Minute)
|
||||
check:
|
||||
for {
|
||||
select {
|
||||
case err = <-done:
|
||||
l--
|
||||
if l == 0 || err != nil { // all returns or some one returns an error
|
||||
break check
|
||||
}
|
||||
case <-timeout.C:
|
||||
err = errors.New(("timeout"))
|
||||
break check
|
||||
}
|
||||
case <-timeout.C:
|
||||
err = errors.New(("timeout"))
|
||||
break check
|
||||
}
|
||||
timeout.Stop()
|
||||
} else {
|
||||
err = errors.New("on found any service")
|
||||
}
|
||||
timeout.Stop()
|
||||
return err
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
//发送远程调用请求
|
||||
|
@ -82,7 +82,7 @@ func (this *Chat) OnInstallComp() {
|
||||
//Event------------------------------------------------------------------------------------------------------------
|
||||
func (this *Chat) EventUserOffline(session comm.IUserSession) {
|
||||
if err := this.modelChat.removeCrossChannelMember(session); err != nil {
|
||||
this.Debug("EventUserOffline:", log.Field{"uid", session.GetUserId()}, log.Field{"err", err})
|
||||
this.Debug("EventUserOffline:", log.Field{Key: "uid", Value: session.GetUserId()}, log.Field{Key: "err", Value: err.Error()})
|
||||
}
|
||||
}
|
||||
|
||||
@ -151,7 +151,7 @@ func (this *Chat) SendWorldChat(msg *pb.DBChat) (code pb.ErrorCode) {
|
||||
} else {
|
||||
if _, err = this.service.AcrossClusterRpcGo(
|
||||
context.Background(),
|
||||
msg.Stag,
|
||||
this.GetCrossTag(),
|
||||
comm.Service_Worker,
|
||||
string(comm.Rpc_ModuleChatPushChat),
|
||||
msg,
|
||||
@ -182,7 +182,7 @@ func (this *Chat) SendUnionChat(msg *pb.DBChat) (code pb.ErrorCode) {
|
||||
} else {
|
||||
if _, err = this.service.AcrossClusterRpcGo(
|
||||
context.Background(),
|
||||
msg.Stag,
|
||||
this.GetCrossTag(),
|
||||
comm.Service_Worker,
|
||||
string(comm.Rpc_ModuleChatPushChat),
|
||||
msg,
|
||||
@ -257,7 +257,7 @@ func (this *Chat) SendSysChatToWorld(ctype comm.ChatSystemType, appenddata inter
|
||||
} else {
|
||||
if _, err = this.service.AcrossClusterRpcGo(
|
||||
context.Background(),
|
||||
msg.Stag,
|
||||
this.GetCrossTag(),
|
||||
comm.Service_Worker,
|
||||
string(comm.Rpc_ModuleChatPushChat),
|
||||
msg,
|
||||
|
Loading…
Reference in New Issue
Block a user